Advertisement
mightyroot

Perl youtube-dl from RadioTileParser

Feb 9th, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.10 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # Script uses track's on air name saved in /tmp/nowplaying.log' by my "Python radio title
  3. # parser" - http://pastebin.com/1tZRnQgD
  4.  
  5. use HTML::TreeBuilder::XPath;
  6. use LWP::UserAgent;
  7. use URI::Encode;
  8.  
  9. open FILE, "/tmp/nowplaying.log" or die $!;
  10. my $track_name = <FILE>;
  11. print "Now on air: $track_name\n";
  12. $track_name =~ s/\ /+/g;
  13.  
  14. my $uri     = URI::Encode->new( { encode_reserved => 0 } );
  15. my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0");
  16. my $req = HTTP::Request->new(GET => 'https://www.google.com.ua/search?hl=en&q='.$track_name);
  17. my $res = $ua->request($req);
  18. my $tree = HTML::TreeBuilder::XPath->new_from_content($res->content);
  19.  
  20. foreach my $titles ($tree->findnodes(q{//li/table/tr/td/h3/a})) {
  21.     $i++;
  22.     #print "\n+++ $i Tile: ";
  23.     #print $titles->as_text;
  24.     $title[$i] = $titles->as_text;
  25. }
  26. $i=0;
  27.  
  28. foreach my $times ($tree->findnodes(q{//li/table/tr/td/div/a/div[not (img)]})) {
  29.     $i++;
  30.     #print "  Time: ";
  31.     #print $times->as_text;
  32.     $timer = $times->as_text;
  33.     @timer = split(/ /, $timer);
  34.     $time[$i] = $timer[1];
  35. }
  36.  
  37. $i=0;
  38.  
  39. foreach my $links ($tree->findnodes(q{//li/table/tr/td/h3/a/@href})) {
  40.     $i++;
  41.     $link_raw = $links->getValue;
  42.     $link_dec = $uri->decode($link_raw);
  43.     @link_dec = split(/\?v=/,$link_dec);
  44.     @link_dec = split(/&sa/,$link_dec[1]);
  45.     print $link_dec[0];
  46.     $link[$i] = "http://www.youtube.com/watch?v=".$link_dec[0];
  47. }
  48. $i=0;
  49. $tree->delete;
  50.  
  51. print "\n0 -> Open in browser for searching...\n";
  52. foreach (@title){
  53.     $i++;
  54.     if ($title[$i]){
  55.         print "\n$i -> $title[$i], $time[$i]\n    $link[$i] \n";
  56.     }
  57. }
  58.  
  59. print "\nSelect number of track to download:\n";
  60. $select =  <STDIN>;
  61. chomp ($select);
  62.  
  63. if ($select eq "0") {
  64.     system('/usr/bin/google-chrome --user-data-dir=/home/admin/ "https://www.google.com.ua/search?hl=en&q='.$track_name.'" 2>/dev/null');
  65.     exit;
  66.  }
  67. if (!$select or $select >= $i or $select !~ m/\d/ ){
  68.     print "\nWrong number. Exiting...\n";
  69.     exit;
  70. } else {
  71.     system('cd /ftp && /usr/sbin/youtube-dl -t --extract-audio --audio-format=mp3 '.$link[$select]);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement