Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -wT
- use strict;
- use warnings;
- use diagnostics;
- use experimental 'smartmatch';
- use utf8;
- use open ':std', ':encoding(UTF-8)';
- use LWP::UserAgent;
- use IO::Socket::SSL qw();
- use Mojo::DOM;
- use File::Slurp;
- my $startUrl = $ARGV[0];
- my $endDepth = $ARGV[1];
- my $delay = $ARGV[2] || "100";
- my $outputFileName = $ARGV[3] || "playlist.m3u";
- if (!$startUrl || !$endDepth ) {
- print qq| Usage: gen.pl URL Depth delay Filename\n|;
- print qq| Depth is how deep to descend while recursing trough the directories\n|;
- print qq| Delay is the time between requests in ms (Default: 100ms)\n|;
- print qq| FileName to write the generated playlist (Default: playlist.m3u)\n|;
- exit 1;
- }
- if (-e $outputFileName) {
- print qq|$outputFileName already exists\n|;
- exit 1;
- }
- write_file($outputFileName,"#EXTM3U\n");
- my %seen;
- my @mediaFiles;
- my $ua = LWP::UserAgent->new;
- #Accept celf Signed Certificates.
- $ua->ssl_opts(
- SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
- verify_hostname => 0
- );
- my @ns_headers = (
- 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0',
- 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
- 'Accept-Language' =>'en-US,en;q=0.5',
- 'Accept-encoding' => 'gzip, deflate, br',
- 'dnt' => '1',
- 'upgrade-insecure-requests' => '1' );
- #file extensions we want
- my @allowFormats = (
- qr/\.mov$/,
- qr/\.mp4$/,
- qr/\.avi$/,
- qr/\.mkv$/
- );
- my $base = URI->new($startUrl);
- my $baseDepth = $startUrl =~ tr/\///;
- crawl( $startUrl, $endDepth || 5);
- exit 0;
- sub crawl {
- my $crawlUrl = shift;
- my $depthLimit = shift;
- if ( $seen{$crawlUrl} ) {return 0;}
- else { $seen{$crawlUrl} = $crawlUrl; }
- my $maxDepth = 0;
- $maxDepth += ($baseDepth + $depthLimit);
- my $currentDepth = $crawlUrl =~ tr/\///;
- if ($currentDepth >= $maxDepth) { return 0; }
- else {
- my @links = parseHtml( grabHtml($crawlUrl) );
- foreach my $link ( @links ){
- my $nextLink = URI->new_abs($link, $base->as_string)->as_string;
- my $nextLinkDepth = $nextLink =~ tr/\///;
- if ($nextLinkDepth < $baseDepth){ next; }
- if ( isFolder($nextLink) ) {
- crawl($nextLink,$depthLimit);
- }
- elsif( hasWantedExt($nextLink) ) {
- append_file($outputFileName,'#EXTINF:-1,'.urlDecode($link)."\n$nextLink\n");
- }
- }
- }
- }
- sub hasWantedExt {
- my $link = shift;
- if ($link ~~ @allowFormats) { return 1 }
- return 0;
- }
- sub isFolder {
- my $url = shift;
- if ($url =~ /\/\Z/) { return 1; }
- return 0;
- }
- sub grabHtml {
- my $url = shift;
- my $response = $ua->get($url, @ns_headers);
- die "$url error: ", $response->status_line unless $response->is_success;
- return $response->decoded_content;
- }
- sub parseHtml {
- my $pageData = shift;
- my $dom = Mojo::DOM->new($pageData);
- my @links;
- for my $e ($dom->find('a[href]')->each) { push (@links,$e->{href}); }
- return @links;
- }
- sub urlDecode {
- my $input = shift;
- $input =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;
- return $input;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement