豆瓣电台

摘自瀚海星云bbs:http://bbs.ustc.edu.cn/cgi/bbstcon?board=Linux&file=M.1286547291.A

针对网速慢的用户以及豆瓣电台的改动做了些修改~:D

dbfm.pl
#!/usr/bin/perl -w 
use LWP; 
use HTTP::Cookies; 
use JSON; 
use Encode; 
use Term::ANSIColor; 
#use Smart::Comments; 
 
my $time = 0; 
$SIG{INT} = "quit"; 
 
my $b = LWP::UserAgent->new; 
$b->cookie_jar(HTTP::Cookies->new); 
 
my $form = { 
    form_email=>'your_id',   ###----- 你的帐号-----###
	form_password=>'your_passwd',          ###----- 你的密码 ----### 
    redir=>"/service/account/?return_to=http%3A%2F%2Fdouban.fm&mode=checkid_setup", 
}; 
 
#-------login the douban. 
my $login = $b->post('http://www.douban.com/login', $form); 
my $_c = $b->get('http://douban.fm/radio'); 
my $uid ='2650910'; 
if($_c->content=~/flashvars\s+=\s+\{uid:'(\d+)'/){ 
    $uid=$1;  #-- Get the uid of your account. 
} 
#-------douban cache folder. 
my $USER_DIR = "$ENV{HOME}/.douban"; 
mkdir $USER_DIR unless -d $USER_DIR; 
 
&play_radio(); 
 
#-------play the mp3. 
sub play_radio{ 
    while(1) { 
        my $mp3 = $b->get('http://douban.fm/j/mine/playlist?r=' .rand(). '&type=n&sid=&uid='.$uid .'&channel=1' );#这里的channel参数是新增的,取值0-9 ~:P
        my $result = JSON->new->utf8(0)->decode($mp3->content); 
        $result  = $result->{song}; 
        for my $hash(@$result){ 
             print colored $hash->{'title'}." ", 'yellow'; 
             print colored ("$hash->{'albumtitle'}",'blue'),'  '; 
             print colored "$hash->{'artist'}\n",'green'; 
             print colored "$hash->{'picture'}\n$hash->{'url'}\n", 'white'; 
             #######--如果不需要下载封面图片显示在conky上,以下2行不需要用---############### 
             download_cover($hash); 
             log2file("[Playing] ",$hash->{'title'},"\t(",$hash->{'artist'},")\n"); 
             #######-- 
			 #qx {wget "$hash->{'url'}" -O /tmp/dbfm 2>/dev/null && mplayer /tmp/dbfm 2>/dev/null};
                         #这一行我换成带缓存的mplayer调用,单纯的管道在网速不行的时候不太给力~:)
			 qx {wget "$hash->{'url'}" -O - 2>/dev/null |mplayer -cache 1024 -  2>/dev/null };
        } 
    } 
} 
 
sub quit{ 
    qx {killall -9 mplayer}; 
    if (time()-$time<3){ 
        print "exiting...\n"; 
        log2file("[Stopped]\n"); 
        exit 1; 
    }  
    print "Press Ctrl+C again in 3 seconds to exit...\n"; 
    select(undef,undef,undef,1); 
    $time = time(); 
    # $SIG{INT} = "quit"; 
} 
 
#######--如果不需要下载封面图片显示在conky上,以下都不需要用---############### 
sub log2file{ 
    my $logfile="/tmp/douban.log"; 
    open LOG, "+>>/tmp/douban.log" or return -1; 
    print LOG @_; 
    close LOG; 
    return 0; 
} 
sub download_cover{ 
    my $hash = shift; 
    my $covername = $hash->{albumtitle} . "_cover.jpg"; 
    my $artist = $hash->{'artist'}; 
    $covername = &file_trim ($covername); 
    $artist = &file_trim ($artist); 
    mkdir "$USER_DIR/$artist" if ! -d "$USER_DIR/$artist"; 
 
    my $file = "$USER_DIR/$artist/$covername"; 
    if ($hash->{'picture'}|| $hash->{'picture'}=~/^http.+jpg$/){ 
 
        if ( ! -s $file ){ 
            $b->get($hash->{'picture'},':content_file' =>"$file") or return -1; 
        } 
        unlink "/tmp/douban_cover.jpg"; 
        symlink "$file","/tmp/douban_cover.jpg"; 
    } 
    return 0; 
} 
 
sub file_trim{ 
    my $file = shift; 
    $file = decode('utf8',$file); 
    $file =~ s/['"\\\/<>\?\!:\|\*]/ /g; 
    $file =~ s/^\s+//; 
    $file =~ s/\s+$//; 
    $file = encode('utf8',$file); 
    $file; 
} 
 
__END__