From 88000603556955b433e3dd388638592e4b6ffe67 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Mon, 4 Jan 2021 04:38:21 +0100 Subject: [PATCH] Adding support to kjanime. --- bin/peertube-dl | 54 ++++++++++++++++++++++++++------ cpanfile | 1 + lib/Peertube/DL/Downloaders.pm | 56 ++++++++++++++++++++++++++++++++++ lib/Peertube/DL/URLHandler.pm | 32 +++++++++++++++---- lib/Peertube/DL/Utils.pm | 6 +++- 5 files changed, 133 insertions(+), 16 deletions(-) diff --git a/bin/peertube-dl b/bin/peertube-dl index 217092f..41b54cf 100755 --- a/bin/peertube-dl +++ b/bin/peertube-dl @@ -5,20 +5,56 @@ use feature 'say'; use strict; use warnings; +use Getopt::Long::Descriptive; + use Peertube::DL::URLHandler; +my ( $opt, $usage ) = describe_options( + 'peertube-dl %o ', + [ 'recurse|r', 'Recursive in reproduction lists.', { default => 0 } ], + [], + [ 'help|h', 'Show this help.', { shortcircuit => 1 } ], +); + +print( $usage->text ), exit if $opt->help; + +my $recurse = $opt->recurse; + die "No url passed" unless @ARGV; my $download_data = Peertube::DL::URLHandler::getDownloadDataFromURL( $ARGV[0] ); -my $filename = $download_data->{filename}; -my $url = $download_data->{url}; -my $ua = Peertube::DL::URLHandler::generateUA(); -my $response = $ua->get($url); -die "Cannot retrieve video data" unless $response->is_success; +my $ua = Peertube::DL::URLHandler::generateUA(); -my $content = $response->decoded_content; +if ( defined $download_data->{options}{list} && $download_data->{options}{list} ) { + my $urls = $download_data->{urls}; + if ($recurse) { + for my $url (@$urls) { + say "Handling nested url: $url"; + my $url_download_data = Peertube::DL::URLHandler::getDownloadDataFromURL( $url ); + downloadVideo( $ua, $url_download_data ); + } + } else { + say "The urls are:\n" . join "\n", @$urls; + } -open my $fh, '>', $filename or die "Cannot open $filename"; -$fh->print( $response->decoded_content ) or die "Cannot write to $filename"; -close $fh; +} else { + downloadVideo( $ua, $download_data ); +} + +sub downloadVideo { + my $ua = shift; + my $download_data = shift; + my $filename = $download_data->{filename}; + my $url = $download_data->{url}; + my $response = $ua->get($url); + + die "Cannot retrieve video data" unless $response->is_success; + + my $content = $response->decoded_content; + + say "Writing into $filename."; + open my $fh, '>', $filename or die "Cannot open $filename"; + $fh->print( $response->decoded_content ) or die "Cannot write to $filename"; + close $fh; +} diff --git a/cpanfile b/cpanfile index 3bc5edc..6fd4c14 100644 --- a/cpanfile +++ b/cpanfile @@ -6,3 +6,4 @@ requires 'Mojolicious::Lite'; requires 'Test::Most'; requires 'Test::MockObject'; requires 'Mojo::Server::Hypnotoad'; +requires 'Getopt::Long::Descriptive'; diff --git a/lib/Peertube/DL/Downloaders.pm b/lib/Peertube/DL/Downloaders.pm index 98ea693..af92afb 100644 --- a/lib/Peertube/DL/Downloaders.pm +++ b/lib/Peertube/DL/Downloaders.pm @@ -8,6 +8,62 @@ use feature 'say'; use JSON; use Peertube::DL::Utils; use Data::Dumper; +use Mojo::DOM; + +sub kjanime { + my $ua = shift; + my $response = shift; + my $dom = Mojo::DOM->new( $response->decoded_content ); + my $hotlink_span = $dom->find('.spoikj .ddserver')->grep( + sub { + my $i = shift; + $i->text =~ /HotLink/; + } + )->first; + my ($k_poi) = $hotlink_span->attr('onclick') =~ /getKpoi\((.*)\)/; + $k_poi =~ s/'//g; + my ( $t, $s, $l ) = ( split /,/, $k_poi ); + my $url_get_php = + 'https://kjanime.net/link/get.php?t=' + . Peertube::DL::Utils::uri_encode($t) . '&s=' + . Peertube::DL::Utils::uri_encode($s) . '&l=' + . Peertube::DL::Utils::uri_encode($l); + my $get_php_response = $ua->get( + $url_get_php, + 'X-Requested-With' => 'XMLHttpRequest', + ); + $dom = Mojo::DOM->new( $get_php_response->decoded_content ); + my $links = [ map { $_->attr('href') =~ s/^http:\/\//https:\/\//r } @{ $dom->find('a')->to_array } ]; + return { options => { list => 1 }, urls => $links }; +} + +sub kjanime_ch { + my $ua = shift; + my $response = shift; + my ($id) = $response->filename; + say $id; + my $param = join '', map { Peertube::DL::Utils::uri_decode("%$_") } unpack( "(A2)*", $id ); + say $param; + +# FORMAT: KJA://70714B6E314943383135712B72582B70713768797A4B6E4D734D474775773D3D/1 +# REQUEST: GET https://hotlink-api.kjanime.net/dl/serie.php?id=70714B6E314943383135712B72582B70713768797A4B6E4D734D474775773D3D&ep=1 + $param =~ s/^KJA:\/\///; + my ( $real_id, $ep ) = split '/', $param; + my $url_serie_php = + 'https://hotlink-api.kjanime.net/dl/serie.php?id=' + . Peertube::DL::Utils::uri_encode($real_id) . '&ep=' + . Peertube::DL::Utils::uri_encode($ep); + say $url_serie_php; + my $response_serie_php = $ua->get( + $url_serie_php, + 'X-Requested-With' => 'XMLHttpRequest', + ); + my $download_data = JSON::decode_json( $response_serie_php->decoded_content ); + say Data::Dumper::Dumper $download_data; + $download_data->{filename} = delete $download_data->{name}; + $download_data->{url} = delete $download_data->{link}; + return $download_data; +} sub gocdn { my $ua = shift; diff --git a/lib/Peertube/DL/URLHandler.pm b/lib/Peertube/DL/URLHandler.pm index aae6a9d..ce63e47 100644 --- a/lib/Peertube/DL/URLHandler.pm +++ b/lib/Peertube/DL/URLHandler.pm @@ -11,12 +11,22 @@ use Peertube::DL::Downloaders; sub getDownloadDataFromURL { my $url_origen = shift; my $ua = Peertube::DL::URLHandler::generateUA(); - my $response = $ua->get($url_origen); - my %handlers = ( + $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 + }, + , ); - + $ua->set_redirect_ok(0); my $handled = 0; my $download_data; for my $x ( keys %handlers ) { @@ -36,13 +46,23 @@ sub getDownloadDataFromURL { 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}; + 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; + } + 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 ); + 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; } diff --git a/lib/Peertube/DL/Utils.pm b/lib/Peertube/DL/Utils.pm index 1f61b44..31d37f2 100644 --- a/lib/Peertube/DL/Utils.pm +++ b/lib/Peertube/DL/Utils.pm @@ -8,7 +8,11 @@ use feature 'say'; use URI::Encode; sub uri_encode { - return map { URI::Encode::uri_encode($_) =~ s/ /+/rg } @_; + if (wantarray) { + return map { URI::Encode::uri_encode($_) =~ s/ /+/rg } @_; + } else { + return URI::Encode::uri_encode( $_[0] ) =~ s/ /+/rg; + } } sub uri_decode {