2 ###APPNAME: sinabook_search
3 ###APPAUTHOR: xiaoranzzz
4 ###APPDATE: Tue Mar 11 16:56:49 2008
6 ###APPDESC: [cmd] default as sinabook_justtext
7 ###APPUSAGE: <keyword> [cmd]
8 ###APPEXAMPLE: sinabook_search 我的名字
12 use HTML
::TreeBuilder
;
15 $ARGV[0]="-h" unless(@ARGV);
17 exit(system("plhelp",$0,@ARGV)) if($_ eq "-h" || $_ eq "--help");
21 print "searching club.book.sina.com.cn for \"$keyword\" ...\n";
24 $cmd = "sinabook_justtext" unless($cmd);
26 $keyword=`echo $keyword | iconv -f utf8 -t gb2312 -c`;
28 $keyword = uri_escape
($keyword);
29 my $URL="http://club.book.sina.com.cn/booksearch/booksearch.php?key1=&k=&textfield=&bookuser=&c=112&col=%B6%C1%CA%E9&item=writingname&area=hotkey&kw=$keyword";
31 open FI
,"-|","netcat '$URL' | iconv -f gb2312 -t utf8" or die("Unable to fork netcat | iconv\n");
33 $tree[0] = HTML
::TreeBuilder
->new();
37 my @match = $_ =~ /(\/booksearch\
/booksearch\.php\?page=)([0-9]+)(\&[^\"\' ]+)/;
45 for(my $i=2;$i<=$pages;$i++) {
46 $tree[$i-1] = HTML
::TreeBuilder
->new();
47 open FI
,"-|","netcat '$URL&page=$i' | iconv -f gb2312 -t utf8" or die("Unable to fork netcat | iconv\n");
48 $tree[$i-1]->parse($_) while(<FI
>);
55 foreach my $child(@_) {
57 push(@text,$child) if($child);
64 print "=" x
40,"\n\n";
65 for(my $i=0;$i<@books;$i++) {
66 printf "%-2d. %s (%s)\n",$i + 1,$books[$i]{title
},$books[$i]{author
};
69 print "Your choice (0 to quit): ";
74 if($result =~ /^\d$/) {
75 return $result-1 if($result >=0 && $result <=@books);
78 print "\rInvalid input,Select again: ";
83 foreach my $tree(@tree) {
84 foreach my $div($tree->look_down("_tag","div","class","des")) {
86 foreach my $node($div->look_down("_tag",qr/^(a|p)$/i)) {
87 $node->normalize_content;
88 if((!$book{author
}) and $node->tag() eq "p") {
89 my @text=gettext
($node->content_list);
90 $book{author
} = $text[0] if(@text);
91 $book{about
} = $text[1];
94 next unless($node->tag() eq "a");
95 $book{href
}=$node->attr("href");
96 my @text=gettext
($node->content_list);
97 $book{title
} = $text[0] if(@text);
106 print "Empty result!\n";
110 my $idx = selectBook
(@books);
111 exit 0 if($idx == -1);
112 print("Selected ",$books[$idx]{href
},"\n");
113 system($cmd,$books[$idx]{href
});