updated git and svn scripts
[xrzperl.git] / sinabook_search
blobc6de1396c5044864a84d0bf606af04b6a11a7344
1 #!/usr/bin/perl -w
2 ###APPNAME: sinabook_search
3 ###APPAUTHOR: xiaoranzzz
4 ###APPDATE: Tue Mar 11 16:56:49 2008
5 ###APPVER: 0.1
6 ###APPDESC: [cmd] default as sinabook_justtext
7 ###APPUSAGE: <keyword> [cmd]
8 ###APPEXAMPLE: sinabook_search 我的名字
9 ###APPOPTION:
10 use strict;
11 use URI::Escape;
12 use HTML::TreeBuilder;
13 use Data::Dumper;
15 $ARGV[0]="-h" unless(@ARGV);
16 foreach(@ARGV){
17 exit(system("plhelp",$0,@ARGV)) if($_ eq "-h" || $_ eq "--help");
20 my $keyword=shift;
21 print "searching club.book.sina.com.cn for \"$keyword\" ...\n";
23 my $cmd=shift;
24 $cmd = "sinabook_justtext" unless($cmd);
26 $keyword=`echo $keyword | iconv -f utf8 -t gb2312 -c`;
27 chomp($keyword);
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");
32 my @tree;
33 $tree[0] = HTML::TreeBuilder->new();
34 my $pages=1;
35 while(<FI>) {
36 $tree[0]->parse($_);
37 my @match = $_ =~ /(\/booksearch\/booksearch\.php\?page=)([0-9]+)(\&[^\"\' ]+)/;
38 if(@match) {
39 $pages = $match[1];
42 close FI;
43 $tree[0]->eof;
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>);
49 close FI;
50 $tree[$i-1]->eof;
53 sub gettext(@) {
54 my @text;
55 foreach my $child(@_) {
56 next if(ref $child);
57 push(@text,$child) if($child);
59 return @text;
62 sub selectBook(@) {
63 my @books = @_;
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};
68 print "=" x 40,"\n";
69 print "Your choice (0 to quit): ";
70 my $result;
71 until($result) {
72 $result = <STDIN>;
73 chomp($result);
74 if($result =~ /^\d$/) {
75 return $result-1 if($result >=0 && $result <=@books);
76 $result=0;
78 print "\rInvalid input,Select again: ";
82 my @books=();
83 foreach my $tree(@tree) {
84 foreach my $div($tree->look_down("_tag","div","class","des")) {
85 my %book;
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];
93 unless($book{href}) {
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);
100 push(@books,\%book);
102 $tree->delete();
105 if(!@books) {
106 print "Empty result!\n";
107 exit 0;
109 else {
110 my $idx = selectBook(@books);
111 exit 0 if($idx == -1);
112 print("Selected ",$books[$idx]{href},"\n");
113 system($cmd,$books[$idx]{href});