Advertisement
Guest User

YandexMusic parser

a guest
Jul 16th, 2012
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.47 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use LWP::UserAgent;
  4. use HTTP::Cookies;
  5. use HTTP::Response;
  6. use Digest::MD5 qw(md5_hex);
  7.  
  8. # Соль зашитая в js. Используется при генерации ссылок на mp3 файлы.
  9. $salt = "XGRlBW9FXlekgbPrRHuSiA";
  10.  
  11. $ua=LWP::UserAgent->new;
  12. $ua->agent("Mozilla/5.0 (compatible; Firefox 13.0.1; Windows 7)");
  13. $ua->cookie_jar(HTTP::Cookies->new(file=>"cookies.dat", autosave=>1));
  14.  
  15. print "Insert URL - address: ";
  16. chomp ($url = <STDIN>);
  17. $url =~ s/#!/fragment/;
  18.  
  19. $response = $ua->get($url);
  20. $page = $response->content;
  21.  
  22. $page =~ s/&quot;/"/g;
  23. $page =~ s/{/\n{/g;
  24.  
  25. foreach (split(/\n/,$page)) {
  26.  
  27.     if (/id":"(?<id>\d+)".*storage_dir":"(?<storage_dir>[0-9a-f.]+).*title":"(?<title>[^"]+)/) {
  28.         foreach (@id) {
  29.             if ($+{id} == $_) {
  30.                 $flag = 1;
  31.             }
  32.         }
  33.         if (!$flag) {
  34.             push (@id, $+{id});
  35.             push (@storage_dir, $+{storage_dir});
  36.             push (@title, $+{title});
  37.         }
  38.         $flag = 0;  
  39.     }
  40.  
  41.     if (/"artist":"(?<artist>[^"]+).*album":"(?<album>[^"]+)/) {
  42.         $artist = $+{artist};
  43.         $album = $+{album};
  44.     }
  45.  
  46. }
  47.  
  48. mkdir "${artist}_-_$album", 0755 or die "Cannot create directory with album name: $!";
  49. chdir "${artist}_-_$album" or die "Cannot change directory to ${artist}_-_$album: $!";
  50.  
  51. foreach (@storage_dir) {
  52.         push (@links, "http://storage.music.yandex.ru/get/$_/2.xml");
  53. }
  54.  
  55. foreach (@links) {
  56.  
  57.         $response = $ua->get(@links[$counter]);
  58.     $url = $response->content; 
  59.  
  60.         $url =~ s/\n//gs;
  61.         $url =~ /="(?<mp3url>[^\"]+mp3)/;
  62.         $shit = $+{mp3url};
  63.         @links[$counter] =~ s/2.xml/$shit/;
  64.         @links[$counter] =~ s/get/download-info/;
  65.    
  66.     $response = $ua->get(@links[$counter]);
  67.     $url = $response->content;
  68.         $url =~ /<host>(?<host>.*)<\/host>/;
  69.         $host = $+{host};
  70.         $url =~ /<path>(?<path>.*)<\/path>/;
  71.         $path = $+{path};
  72.         $url =~ /<ts>(?<ts>.*)<\/ts>/;
  73.         $ts = $+{ts};
  74.         $url =~ /<s>(?<ss>.*)<\/s>/;
  75.         $ss = $+{ss};
  76.    
  77.         $path =~ /^.(.*)/;
  78.         $md5 = md5_hex($salt.$1.$ss);
  79.  
  80.         @finish[$counter] = "http://$host/get-mp3/$md5/".$ts.$path."?track-id=@id[$counter]&from=service";
  81.  
  82.     printf "%-60s", "Downloading @title[$counter].mp3 file:";
  83.    
  84.     $response = $ua->get("@finish[$counter]",
  85.         ':content_file' => "@title[$counter]"
  86.         );
  87.  
  88.     if ($response->is_success) {
  89.         print "Done!\n";
  90.     } else {
  91.         print "Error!\n";
  92.     }
  93.  
  94.         $counter++;
  95. }
  96.  
  97. chdir "../" or die "Cannot change directory to previous: $!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement