分类:
2006-04-23 14:44:29
xiaosuo@gentux perl $ ./baidulrc -h Usage: baidulrc [options] MusicName. -l get the lyrics list. -p num set the page to n, default 1. -n num set the number to n. -d download the special lyrics. -x output the content in XML format. -h show this help page. |
#!/usr/bin/perl # # Copyright (C) xiaosuo # License GPL2 or above # use strict; use warnings; use URI; use HTML::TreeBuilder; require LWP::UserAgent; use Getopt::Std; sub get_musics { my $tree = $_[0] || die "No tree"; my @musics; foreach my $div ( $tree->look_down('_tag', 'div', sub{ return 0 unless $_[0]->attr('class'); return 0 unless($_[0]->attr('class') =~ /BlueBG/); })){ my ($title) = ($div->as_text =~ /歌曲:(.*)/); push @musics, $title; } return @musics; } sub get_actors { my $tree = $_[0] || die "No tree"; my @actors; foreach my $div ( $tree->look_down('_tag', 'div', sub{ return 0 unless $_[0]->attr('style'); return 0 unless $_[0]->attr('style') =~ /padding-top:10px;padding-left:15px/; })){ if($div->as_text =~ /歌手:(.*) 专辑:(.*)/){ push @actors, $1; }elsif($div->as_text =~ /歌手:(.*)/){ push @actors, $1; }else{ push @actors, $div->as_text; } } return @actors; } sub get_links { my $tree = $_[0] || die "No tree"; my @links; foreach my $div ( $tree->look_down('_tag', 'div', sub{ return 0 unless $_[0]->attr('class'); return 0 unless $_[0]->attr('class') =~ /unnamed3/; })){ if(my ($a) = ($div->look_down('_tag', 'a', sub{ return 0 unless $_[0]->as_text; return 0 unless $_[0]->as_text =~ /LRC歌词/; }))){ push @links, $a->attr('href'); }else{ push @links, ''; } } return @links; } sub get_page_num { my $tree = $_[0] || die "No tree"; my $num = 1; if(my ($div) = ( $tree->look_down('_tag', 'div', sub{ return 0 unless $_[0]->attr('class'); return 0 unless $_[0]->attr('class') =~ /pg/; }))){ foreach my $a ($div->look_down('_tag', 'a', sub{ return 0 unless $_[0]->as_text; })){ if($a->as_text =~ /\[(.*)\]/){ $num = int($1); } } } return $num; } # Usage: baidu sub usage { print " Usage: baidulrc [options] MusicName.\n"; print " -l get the lyrics list.\n"; print " -p num set the page to n, default 1.\n"; print " -n num set the number to n.\n"; print " -d download the special lyrics.\n"; print " -x output the content in XML format.\n"; print " -h show this help page.\n"; } # parse the options. my %opts; if(!getopts("lp:n:dxh", \%opts)){ usage(); exit(1); } if($opts{"h"}){ usage(); exit(0); } if($#ARGV != 0){ usage(); exit(1); } my $music_name = $ARGV[0]; my $page_number = 0; my $music_number = -1; if($opts{"p"} && $opts{"p"} > 1){ $page_number = ($opts{"p"} - 1) * 10; } if($opts{"n"}){ if($opts{"n"} < 1 || $opts{"n"} > 10){ print "The lyrics number must between 0 and 9.\n"; exit(1); } $music_number = $opts{"n"} - 1; } # get the lyrics list my $uri = URI->new(''); $uri->query_form( # 'f' => 'ms', 'rn' => '10', 'tn' => 'baidump3lyric', 'ct' => '150994944', 'word' => $music_name, 'lm' => '-1', 'z' => '0', 'cl' => '3', 'sn' => '', 'cm' => '1', 'sc' => '1', 'bu' => '', 'pn' => "$page_number" ); my $ua = LWP::UserAgent->new; my $response = $ua->get($uri->as_string) || die("get $uri->as_string failed.\n"); my $tree = HTML::TreeBuilder->new; if($response->is_success){ $tree->parse_content($response->content) || die("parse failed.\n"); }else{ die $response->status_line; } my @links = get_links $tree; if($opts{"d"}){ # download the lyrics if($music_number < 0){ print "You must give the music mumber.\n"; usage(); exit(1); } if($links[$music_number] eq ""){ print "There is no LRC lyrics for this music, try the others.\n"; exit(1); } my $fua = LWP::UserAgent->new; my $fres = $fua->get($links[$music_number]) || die("get $links[$music_number] failed.\n"); if(!$fres->is_success){ print "get $links[$music_number] failed.\n"; exit(1); } if($opts{"x"}){ print "\n"; print " } foreach my $line (split("\n", $fres->content)){ if($line =~ /^\[.*:.*\].*$/){ print $line . "\n"; } } if($opts{"x"}){ print " } exit 0; } my @musics = get_musics $tree; my @actors = get_actors $tree; my $num = get_page_num $tree; # output the list if($opts{"x"}){ print "\n"; print " for(my $i = 0; $i <= $#musics; $i ++){ print " print " print " print "$links[$i]\n"; print " } print " print " }else{ for(my $i = 0; $i <= $#musics; $i ++){ print $i + 1 . "\t $musics[$i] - $actors[$i]\n" } print "Total page number: $num.\n"; } # free the memory and exit $tree->delete; exit(0); |