#!/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 => 10; ############################################################ 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}; 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 (! (-d "down_mp3")) { mkdir "down_mp3"; } if (! (-d "down_mp3")) { print "cannot create directory: down_mp3\n"; exit; } { my @f = glob "down_mp3/*.aria2"; foreach (@f) { unlink substr $_, 0, -6; unlink $_; } } 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); } elsif ( -e "down_mp3/$a - $t.mp3") { print "# File exits already:\t$a - $t.mp3\n"; return; } 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}; print "aaaaa\t$aa - $t\n"; 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 =~ /(