2020-12-29 02:29:42 +01:00
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 ( ) ;
2021-01-06 07:53:32 +01:00
my $ options = shift ;
2021-01-04 04:38:21 +01:00
$ ua - > set_redirect_ok ( 1 ) ;
my $ response = $ ua - > get ( $ url_origen ) ;
my % handlers = (
2020-12-29 02:29:42 +01:00
gocdn = > { reg = > qr/gocdn\.html#/ , subr = > \ & Peertube::DL::Downloaders:: gocdn } ,
animeid = > { reg = > qr/animeid\.to\/ streaming \ . php \ ? / , subr = > \ & Peertube::DL::Downloaders:: animeid } ,
2021-01-04 04:38:21 +01:00
kjanime = > {
reg = > qr/kjanime - Anime en formato ligero y HQ - kjanime/ ,
2021-01-06 07:53:32 +01:00
subr = > \ & Peertube::DL::Downloaders:: kjanime ,
2021-01-04 04:38:21 +01:00
} ,
kjanime_ch = > {
reg = > qr/Link de descarga . kjanime/ ,
2021-01-06 07:53:32 +01:00
subr = > \ & Peertube::DL::Downloaders:: kjanime_ch ,
} ,
youtube = > {
reg = > qr/ytInitialPlayerResponse = \{/ ,
subr = > \ & Peertube::DL::Downloaders:: youtube ,
2021-01-04 04:38:21 +01:00
} ,
2020-12-29 02:29:42 +01:00
) ;
2021-01-04 04:38:21 +01:00
$ ua - > set_redirect_ok ( 0 ) ;
2020-12-29 02:29:42 +01:00
my $ handled = 0 ;
my $ download_data ;
for my $ x ( keys % handlers ) {
if ( $ response - > decoded_content =~ m/$handlers{$x}{reg}/ ) {
2021-01-06 07:53:32 +01:00
eval { $ download_data = $ handlers { $ x } { subr } - > ( $ ua , $ response , $ options ) ; } ;
2020-12-29 02:29:42 +01:00
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' ;
2021-01-06 07:53:32 +01:00
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 an arrayref." unless ref $ download_data - > { formats } eq 'ARRAY' ;
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 ;
}
2021-01-04 04:38:21 +01:00
}
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 } ;
2020-12-29 02:29:42 +01:00
return $ download_data ;
}
sub generateUA {
2021-01-04 04:38:21 +01:00
my $ ua = Peertube::DL::UserAgent - > new ( timeout = > 10 , ) ;
2020-12-29 02:29:42 +01:00
$ ua - > agent ( 'Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0' ) ;
$ ua ;
}
1 ;