2020-12-26 03:04:37 +01:00
#!/usr/bin/env perl
use feature 'say';
use strict;
use warnings;
use LWP::UserAgent;
use URI::Encode;
use JSON;
use Data::Dumper;
my $ua = Peertube::UserAgent->new( timeout => 10 );
$ua->agent('Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0');
my $response = $ua->get( $ARGV[0] );
my %handlers = (
gocdn => { reg => qr/gocdn\.html#/, subr => \&Peertube::Downloaders::gocdn },
animeid => { reg => qr/animeid\.to\/streaming\.php\?/, subr => \&Peertube::Downloaders::animeid },
);
2020-12-26 03:52:11 +01:00
die "No url passed" unless @ARGV;
2020-12-26 05:01:48 +01:00
my $handled = 0;
2020-12-26 03:04:37 +01:00
for my $x ( keys %handlers ) {
if ( $response->decoded_content =~ m/$handlers{$x}{reg}/ ) {
eval { $handlers{$x}{subr}->( $ua, $response ); };
if ($@) {
say $@;
} else {
last;
2020-12-26 05:01:48 +01:00
$handled = 1;
2020-12-26 03:04:37 +01:00
}
}
}
2020-12-26 05:01:48 +01:00
say "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;
2020-12-26 03:04:37 +01:00
package Peertube::Downloaders {
sub gocdn {
my $ua = shift;
my $response = shift;
my ($id) = $response->decoded_content =~ /gocdn\.html#(.+?)"/;
die "Id undefined" if !defined $id;
my $gocdn =
$ua->get( 'https://streamium.xyz/gocdn.php?v=' . Peertube::Utils::uri_encode($id) )->decoded_content;
my $google_url = JSON::decode_json($gocdn)->{file};
my ($filename) = $ARGV[0] =~ s/^.*\///gr . '.mp4';
$ua->get( $google_url, ':content_file' => $filename );
say $filename;
}
sub animeid {
my $ua = shift;
my $response = shift;
my @order = ( 'id', 'title', 'refer' );
my $url = $ARGV[0];
my $url_ajax = 'https://animeid.to/ajax.php?';
my ($get_params) = $response->decoded_content =~ /animeid\.to\/streaming\.php\?(.*?)"/;
$get_params = {
map {
my ( $key, $value ) = /^(.*?)=(.*)$/;
( $key, Peertube::Utils::uri_decode($value) )
} split '&',
$get_params
};
say "Got from $url params for ajax:";
print Data::Dumper::Dumper $get_params;
say 'Adding passed url as refer.';
$get_params->{refer} = $url;
my $filename = $get_params->{title};
$url_ajax .= join '&',
map { join '=', ( $_ => Peertube::Utils::uri_encode( $get_params->{$_} ) ) } @order;
say 'Getting video location from: ' . $url_ajax;
my $ajax_data = $ua->get(
$url_ajax,
Referer => $url,
Host => 'animeid.to',
'X-Requested-With' => 'XMLHttpRequest',
)->decoded_content;
say "Decoding json... $ajax_data";
$ajax_data = JSON::decode_json $ajax_data;
die 'No video source found.' if ( !defined $ajax_data->{source} );
my $download_url = $ajax_data->{source}[0]{file} // die "No url found.";
my $extension = $ajax_data->{source}[0]{type} // die "No extension found.";
$filename .= ".$extension";
say "Downloading video from $download_url...";
my $video_response = $ua->get(
$download_url,
Referer => $url,
Host => 'animeid.to',
);
if ( $video_response->is_success ) {
open my $fh, '>', $filename;
$fh->print( $video_response->decoded_content );
close $fh;
say $filename;
} else {
die 'Video download failed because: ' . $video_response->status_line;
}
}
}
package Peertube::UserAgent {
use parent 'LWP::UserAgent';
my $last_progress_time;
sub redirect_ok {
1;
}
sub progress {
my $self = shift;
my $status = shift;
my $response = shift;
if ( !defined $last_progress_time || time > $last_progress_time + 1 ) {
$last_progress_time = time;
if ( $response->isa('HTTP::Response') && $status =~ /^\d/ ) {
say $response->base . ': ' . int( $status * 100 ) . '%';
}
}
}
}
package Peertube::Utils {
sub uri_encode {
return map { URI::Encode::uri_encode($_) =~ s/ /+/rg } @_;
}
sub uri_decode {
return map { URI::Encode::uri_decode($_) =~ s/\+/ /rg } @_;
}
}