#!/usr/bin/perl use warnings; use strict; use LWP::Simple; use Encode qw/encode decode/; # http://music.sogou.com/song/newtop_1.html ############################################################ use constant URL => "http://music.sogou.com/song/newtop_1.html"; use constant MAX => 20; ############################################################ if (! (-d "down_mp3")) { mkdir "down_mp3"; } if (! (-d "down_mp3")) { print "cannot create directory: down_mp3\n"; exit; } print "#### get top list... from ", URL, "\n"; my $resp = get( URL ); my @songs; my $i = 0; while ($resp =~ m/width="153"(.*?)width="83"/gsm and $i < MAX) { my $tmp = $1; my ($t, $a); $tmp =~ m/title="([^"]+)"/; $t = encode("utf8", $1); $tmp =~ m/showSinger=t>([^<]+)<\//; $a = encode("utf8", $1); print "$a - $t\n"; push @songs, {"artist" => $a, "title" => $t}; $i++; } print "############################################################\n"; foreach (@songs) { my $a = $_->{artist}; my $t = $_->{title}; if ((not -e "down_mp3/$a - $t.mp3.aria2") and (-e "down_mp3/$a - $t.mp3" )) { print "# File exits already:\t$a - $t.mp3\n"; next; } print "#### get the url of $a - $t.mp3 ...\n"; my $u = &get_best_url($_); if (defined $u) { &down($_, $u); } else { print "######## failed to get the url of $a - $t.mp3 ...\n"; } } exit; sub down { my $a = $_[0]->{artist}; my $t = $_[0]->{title}; my $l = $_[1]; if ( -e "down_mp3/$a - $t.mp3.aria2") { print "# Continue download: $a - $t.mp3\n"; system("aria2c", "-q", "-o", "down_mp3/$a - $t.mp3", $l); } else { print "# Now downloading:\t$a - $t.mp3\n"; system("aria2c", "-q", "-o", "down_mp3/$a - $t.mp3", $l); } } sub get_best_url { my $aa = $_[0]->{artist}; my $t = $_[0]->{title}; my ($r, $u); my @items; my @links; my $retry = 3; do { $r = get("http://mp3.sogou.com/music.so?query=" . encode("gbk", decode("utf8", "$aa $t"))); $retry--; } while ((! defined $r) and $retry); @items = ($r =~ /(.*?)class="spd"/gms); foreach (@items) { $_ = encode("utf8", $_); my ($tmp_title, $tmp_artist, $tmp_url, $tmp_size); $_ =~ /songname.*?>([^<]+)([^<]+)(.*?)(.{3})<\/td>/; next if not ($1 =~ /mp3/i); push @links, {"size" => "$tmp_size", "link" => "$tmp_url"}; } return undef if ((scalar @links) == 0); @links = reverse sort {$a->{size} <=> $b->{size}} @links; foreach (@links) { $r = get($_->{link}); $r =~ /url=(http:[^"']+)/g; $u = $1; last if (defined $u); } return $u; }