您的位置:首页 > 其它

下载百度排行榜音乐的程序(Perl)

2012-09-23 11:21 309 查看
下载百度排行榜音乐的程序(Perl)

#!/usr/bin/perl
use FindBin '$Bin';
use URI::Escape;
use lib $Bin;
use lib "$Bin/../conf/";
use Conf;

my %g_conf =
(
task =>
{

top100 =>
{
list => "/data/share/code/trunk/perl/get_baidu_mp3/conf/top100_xml.txt",
store_path => "/data/share/code/trunk/perl/get_baidu_mp3/music/top100/",
},

top500 =>
{
list => "/data/share/code/trunk/perl/get_baidu_mp3/conf/top500_xml.txt",
store_path => "/data/share/code/trunk/perl/get_baidu_mp3/music/top500/",
},

},

download_thread_cnt => 20,
);

sub init {
foreach my $key (keys %{$g_conf{'task'}})
{
my $store_path = $g_conf{'task'}{$key}{'store_path'};
`mkdir -p $store_path`;
}
}

sub main {
init();
foreach my $key (keys %{$g_conf{'task'}})
{
#step 1: get list
my $html = $g_conf{'task'}{$key}{'list'};
open FV,$html;
my @line_arr = <FV>;
my $contents = join ("\n", @line_arr);
my @idx;
my $title;
while ($contents =~ /\<data\>\<id\>(\d+)\<\/id\>\<name\>(\S+)\<\/name\>/g )
{
push @idx, $1;
push @title, $2;
}

#step 2: download
my $download_thread_cnt = $g_conf{'download_thread_cnt'};
my @download_cmd = ();
foreach my $i (1 .. scalar(@idx))
{
my $index = $idx[$i];
my $title = $title[$i];
my $down_url;
my $down_name;
my ($song,$art);
if ($title =~ /(\S+)\$\$(\S+)\$\$\$\$/) {
$song = $1;
$art = $2;
}
$down_name = $g_conf{'task'}{$key}{'store_path'} . $art . "_" . $song . ".mp3";
my $url = "http://box.zhangmen.baidu.com/x?op=12&count=1&mtype=1&title=$song\\\$\\\$$art\\\$\\\$\\\$\\\$&url=";
my $xml = `curl "$url"`;
if ($xml =~ /\<p2p\><hash>(\w+)<\/hash\>\<url\>\<\!\[CDATA\[(\S+)\]\]\>/) {
$down_url = $2;
if (length($down_url) < 10) {
next;
}
#print "$download_cmd\n";
push @download_cmd, "wget -nv  \"$down_url\" -O \"$down_name\"";
}
if ($i % $download_thread_cnt == 0 or $i == scalar(@idx)) {
my $real_proc_cnt = scalar(@download_cmd);
foreach my $do_key (@download_cmd) {
my $pid = fork();
if ($pid == 0)
{
system($do_key);
#print $do_key, "\n";
exit 0;
}
}
while ($real_proc_cnt >0)
{
wait();
$real_proc_cnt --;
}
@download_cmd = ();
}
}
}

}

main();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息