File Coverage

blib/lib/WWW/Scraper/ISBN/TWSilkbook_Driver.pm
Criterion Covered Total %
statement 31 53 58.4
branch 0 6 0.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 41 69 59.4


line stmt bran cond sub pod time code
1             # ex:ts=8
2              
3             package WWW::Scraper::ISBN::TWSilkbook_Driver;
4              
5 1     1   830 use strict;
  1         2  
  1         36  
6 1     1   7 use warnings;
  1         2  
  1         37  
7              
8 1     1   6 use vars qw($VERSION @ISA);
  1         12  
  1         77  
9             $VERSION = '0.02';
10              
11             #--------------------------------------------------------------------------
12              
13             =head1 NAME
14              
15             WWW::Scraper::ISBN::TWSilkbook_Driver - Search driver for TWSilkbook' online catalog.
16              
17             =head1 SYNOPSIS
18              
19             See parent class documentation (L)
20              
21             =head1 DESCRIPTION
22              
23             Searches for book information from the TWSilkbook' online catalog.
24              
25             =cut
26              
27             #--------------------------------------------------------------------------
28              
29             ###########################################################################
30             #Library Modules #
31             ###########################################################################
32              
33 1     1   6 use WWW::Scraper::ISBN::Driver;
  1         3  
  1         27  
34 1     1   1528 use WWW::Mechanize;
  1         177762  
  1         44  
35 1     1   999 use Template::Extract;
  1         729  
  1         27  
36 1     1   989 use Text::Iconv;
  1         3514  
  1         69  
37              
38             ###########################################################################
39             #Constants #
40             ###########################################################################
41              
42 1     1   10 use constant QUERY => 'http://www.silkbook.com/function/Search_List_Book.asp?item=5&text=%s';
  1         3  
  1         609  
43              
44             #--------------------------------------------------------------------------
45              
46             ###########################################################################
47             #Inheritence #
48             ###########################################################################
49              
50             @ISA = qw(WWW::Scraper::ISBN::Driver);
51              
52             ###########################################################################
53             #Interface Functions #
54             ###########################################################################
55              
56             =head1 METHODS
57              
58             =over 4
59              
60             =item C
61              
62             Creates a query string, then passes the appropriate form fields to the Silkbook
63             server.
64              
65             The returned page should be the correct catalog page for that ISBN. If not the
66             function returns zero and allows the next driver in the chain to have a go. If
67             a valid page is returned, the following fields are returned via the book hash:
68              
69             isbn
70             title
71             author
72             pages
73             book_link
74             image_link
75             pubdate
76             publisher
77             price_list
78             price_sell
79              
80             The book_link and image_link refer back to the Silkbook website.
81              
82             =back
83              
84             =cut
85              
86             sub search {
87 1     1 1 1350 my $self = shift;
88 1         3 my $isbn = shift;
89 1         10 $self->found(0);
90 1         25 $self->book(undef);
91              
92 1         14 my $url = sprintf(QUERY, $isbn);
93 1         8 my $mechanize = WWW::Mechanize->new();
94 1         18149 $mechanize->get($url);
95 0 0         return undef unless($mechanize->success());
96              
97             # The Search Results page
98 0           my $template = <
99            
100            
101             END
102              
103 0           my $extract = Template::Extract->new;
104 0           my $data = $extract->extract($template, $mechanize->content());
105              
106 0 0         return $self->handler("Could not extract data from TWSilkbook result page.")
107             unless(defined $data);
108              
109 0           my $book = $data->{book};
110 0           $mechanize->get($book);
111              
112 0           $template = <
113            
114             [% title %]
115            
[% author %]
116             [% publisher %]
[% ... %]
117            
[% pubdate %]
[% ... %]
118             [% pages %][% ... %]
119             [% price_list %][% ... %]
120             [% ... %][% price_sell %]
121             END
122              
123 0           $data = $extract->extract($template, $mechanize->content());
124              
125 0 0         return $self->handler("Could not extract data from TWSilkbook result page.")
126             unless(defined $data);
127              
128 0           my $conv = Text::Iconv->new("utf-8", "big5");
129 0           $data->{title} = $conv->convert($data->{title});
130 0           $data->{author} = $conv->convert($data->{author});
131 0           $data->{author} =~ s/^.*ĄG(\S+) .*$/$1/s;
132 0           $data->{publisher} = $conv->convert($data->{publisher});
133 0           $data->{publisher} =~ s/^.*ĄG(.*)$/$1/;
134 0           $data->{pubdate} = $conv->convert($data->{pubdate});
135 0           $data->{pubdate} =~ s/^.*ĄG(.*)$/$1/;
136              
137 0           my $bk = {
138             'isbn' => $isbn,
139             'title' => $data->{title},
140             'author' => $data->{author},
141             'pages' => $data->{pages},
142             'book_link' => $book,
143             'image_link' => "http://www.silkbook.com".$data->{image_link},
144             'pubdate' => $data->{pubdate},
145             'publisher' => $data->{publisher},
146             'price_list' => $data->{price_list},
147             'price_sell' => $data->{price_sell},
148             };
149              
150 0           $self->book($bk);
151 0           $self->found(1);
152 0           return $self->book;
153             }
154              
155             1;
156             __END__