Peertube-dl/lib/Peertube/DL/URLHandler.pm

50 lines
1.7 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 $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 },
);
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 ); };
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';
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;