Advertisement
Guest User

Open Directory to M3u

a guest
May 17th, 2019
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.00 KB | None | 0 0
  1. #!/usr/bin/perl -wT
  2.  
  3. use strict;
  4. use warnings;
  5. use diagnostics;
  6. use experimental 'smartmatch';
  7.  
  8. use utf8;
  9. use open ':std', ':encoding(UTF-8)';
  10. use LWP::UserAgent;
  11. use IO::Socket::SSL qw();
  12. use Mojo::DOM;
  13. use File::Slurp;
  14.  
  15. my $startUrl = $ARGV[0];
  16. my $endDepth = $ARGV[1];
  17. my $delay = $ARGV[2] || "100";
  18. my $outputFileName = $ARGV[3] || "playlist.m3u";
  19.  
  20.  
  21. if (!$startUrl || !$endDepth ) {
  22.     print qq|   Usage: gen.pl URL Depth delay Filename\n|;
  23.     print qq|   Depth is how deep to descend while recursing trough the directories\n|;
  24.     print qq|   Delay is the time between requests in ms (Default: 100ms)\n|;
  25.     print qq|   FileName to write the generated playlist (Default: playlist.m3u)\n|;
  26.     exit 1;
  27. }
  28.  
  29. if (-e $outputFileName) {
  30.     print qq|$outputFileName already exists\n|;
  31.     exit 1;
  32. }
  33. write_file($outputFileName,"#EXTM3U\n");
  34.  
  35. my %seen;
  36. my @mediaFiles;
  37. my $ua = LWP::UserAgent->new;
  38.  
  39.  
  40. #Accept celf Signed Certificates.
  41. $ua->ssl_opts(
  42.     SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
  43.     verify_hostname => 0
  44. );
  45. my @ns_headers = (
  46.     'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0',
  47.     'Accept'    => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  48.     'Accept-Language' =>'en-US,en;q=0.5',
  49.     'Accept-encoding' => 'gzip, deflate, br',
  50.     'dnt' => '1',
  51.     'upgrade-insecure-requests' => '1' );
  52.  
  53. #file extensions we want
  54. my @allowFormats = (
  55.         qr/\.mov$/,
  56.         qr/\.mp4$/,
  57.         qr/\.avi$/,
  58.         qr/\.mkv$/
  59.         );
  60.  
  61. my $base = URI->new($startUrl);
  62. my $baseDepth = $startUrl =~ tr/\///;
  63.  
  64. crawl( $startUrl, $endDepth || 5);
  65.  
  66. exit 0;
  67.  
  68. sub crawl {
  69.     my $crawlUrl = shift;
  70.     my $depthLimit = shift;
  71.  
  72.     if ( $seen{$crawlUrl} ) {return 0;}
  73.     else { $seen{$crawlUrl} = $crawlUrl; }
  74.  
  75.     my $maxDepth = 0;
  76.     $maxDepth += ($baseDepth + $depthLimit);
  77.  
  78.     my $currentDepth = $crawlUrl =~ tr/\///;
  79.    
  80.     if ($currentDepth >= $maxDepth) { return 0; }
  81.     else {
  82.         my @links = parseHtml( grabHtml($crawlUrl) );
  83.         foreach my $link ( @links ){
  84.  
  85.             my $nextLink = URI->new_abs($link, $base->as_string)->as_string;
  86.             my $nextLinkDepth = $nextLink =~ tr/\///;
  87.             if ($nextLinkDepth < $baseDepth){ next; }
  88.             if ( isFolder($nextLink) ) {
  89.                 crawl($nextLink,$depthLimit);
  90.             }
  91.             elsif( hasWantedExt($nextLink) ) {
  92.                 append_file($outputFileName,'#EXTINF:-1,'.urlDecode($link)."\n$nextLink\n");
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. sub hasWantedExt {
  99.     my $link = shift;
  100.     if ($link ~~ @allowFormats) { return 1 }
  101.     return 0;
  102. }
  103.  
  104. sub isFolder {
  105.     my $url = shift;
  106.     if ($url =~ /\/\Z/) { return 1; }
  107.     return 0;
  108. }
  109.  
  110. sub grabHtml {
  111.     my $url = shift;
  112.     my $response = $ua->get($url, @ns_headers);
  113.     die "$url error: ", $response->status_line unless $response->is_success;
  114.     return $response->decoded_content;
  115. }
  116.  
  117. sub parseHtml {
  118.     my $pageData = shift;
  119.     my $dom = Mojo::DOM->new($pageData);
  120.     my @links;
  121.     for my $e ($dom->find('a[href]')->each) { push (@links,$e->{href}); }
  122.     return @links;
  123. }
  124.  
  125. sub urlDecode {
  126.     my $input = shift;
  127.     $input =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;
  128.     return $input;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement