Peertube-dl/lib/Peertube/DL/URLHandler.pm
2021-01-13 09:13:27 +01:00

87 lines
3.4 KiB
Perl

package Peertube::DL::URLHandler;
use strict;
use warnings;
use feature 'say';
use Peertube::DL::UserAgent;
use Peertube::DL::Downloaders;
sub getDownloadDataFromURL {
my $url_origen = shift;
my $ua = Peertube::DL::URLHandler::generateUA();
my $options = shift;
$ua->set_redirect_ok(1);
my $response = $ua->get($url_origen);
my %handlers = (
gocdn => { reg => qr/gocdn\.html#/, subr => \&Peertube::DL::Downloaders::gocdn },
animeid => { reg => qr/animeid\.to\/streaming\.php\?/, subr => \&Peertube::DL::Downloaders::animeid },
kjanime => {
reg => qr/kjanime - Anime en formato ligero y HQ - kjanime/,
subr => \&Peertube::DL::Downloaders::kjanime,
},
kjanime_ch => {
reg => qr/Link de descarga . kjanime/,
subr => \&Peertube::DL::Downloaders::kjanime_ch,
},
youtube => {
reg => qr/ytInitialPlayerResponse = \{/,
subr => \&Peertube::DL::Downloaders::youtube,
},
);
$ua->set_redirect_ok(0);
my $handled = 0;
my $download_data;
for my $x ( keys %handlers ) {
if ( $response->decoded_content =~ m/$handlers{$x}{reg}/ ) {
eval { $download_data = $handlers{$x}{subr}->( $ua, $response, $options ); };
if ($@) {
warn $@;
} else {
$handled = 1;
last;
}
}
}
die
"The url could not be handled, post a issue on https://gitea.sergiotarxz.freemyip.com/sergiotarxz/Peertube-dl/issues telling me the url which does not allow you to download the video so I add support for it."
unless $handled;
die "Download data not defined" unless defined $download_data;
die "Download data not hashref" unless ref($download_data) eq 'HASH';
if ( defined $download_data->{options} ) {
if ( defined $download_data->{options}{list}
&& $download_data->{options}{list} )
{
say 'Reproduction list detected.';
die 'No url list.'
unless defined $download_data->{urls};
die 'Urls is not an array'
unless ref $download_data->{urls} eq 'ARRAY';
return $download_data;
}
if ( defined $download_data->{options}{list_formats} && $download_data->{options}{list_formats} ) {
say 'List of formats retrieved.';
die "No title." unless defined $download_data->{title};
die "No description." unless defined $download_data->{description};
die "No formats available." unless defined $download_data->{formats};
die "Formats is not a hash." unless ref $download_data->{formats} eq 'HASH';
say "The video title is $download_data->{title}.";
say "The video description is $download_data->{description}.";
say "The available formats are: @{[Data::Dumper::Dumper $download_data->{formats}]}.";
return $download_data;
}
}
die "Filename not defined" unless exists $download_data->{filename} && $download_data->{filename};
die "Download url not defined" unless exists $download_data->{url} && defined $download_data->{url};
return $download_data;
}
sub generateUA {
my $ua = Peertube::DL::UserAgent->new( timeout => 10, );
$ua->agent('Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0');
$ua;
}
1;