Peertube-dl/bin/peertube-dl

70 lines
2.0 KiB
Perl
Executable File

#!/usr/bin/env perl
use feature 'say';
use strict;
use warnings;
use Getopt::Long::Descriptive;
use Peertube::DL::URLHandler;
binmode STDOUT, ':utf8';
my ( $opt, $usage ) = describe_options(
'peertube-dl %o <url>',
[ 'recurse|r', 'Recursive in reproduction lists.', { default => 0 } ],
[ 'format|f=s', 'Choose format by id.', ],
[],
[ 'help|h', 'Show this help.', { shortcircuit => 1 } ],
);
print( $usage->text ), exit if $opt->help;
my $recurse = $opt->recurse;
my $format = $opt->format;
die "No url passed" unless @ARGV;
my $download_data =
Peertube::DL::URLHandler::getDownloadDataFromURL( $ARGV[0], { defined $format ? ( format => $format ) : () } );
my $ua = Peertube::DL::URLHandler::generateUA();
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;
}
} elsif ( defined $download_data->{options}{list_formats} && $download_data->{options}{list_formats} ) {
say 'Listing formats available.';
delete $download_data->{options};
say "The available formats are: @{[Data::Dumper::Dumper $download_data]}";
exit 0;
} 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;
}