File Coverage

blib/lib/WWW/Scraper/ISBN/TWSrbook_Driver.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # ex:ts=8
2              
3             package WWW::Scraper::ISBN::TWSrbook_Driver;
4              
5 1     1   775 use strict;
  1         2  
  1         30  
6 1     1   5 use warnings;
  1         2  
  1         29  
7              
8 1     1   5 use vars qw($VERSION @ISA);
  1         9  
  1         60  
9             $VERSION = '0.01';
10              
11             #--------------------------------------------------------------------------
12              
13             =head1 NAME
14              
15             WWW::Scraper::ISBN::TWSrbook_Driver - Search driver for TWSrbook's 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 TWSrbook's online catalog.
24              
25             =cut
26              
27             #--------------------------------------------------------------------------
28              
29             ###########################################################################
30             #Library Modules #
31             ###########################################################################
32              
33 1     1   5 use WWW::Scraper::ISBN::Driver;
  1         1  
  1         18  
34 1     1   480 use WWW::Mechanize;
  0            
  0            
35             use Template::Extract;
36              
37             use Data::Dumper;
38              
39             ###########################################################################
40             #Constants #
41             ###########################################################################
42              
43             use constant QUERY => 'http://www.srbook.com.tw/web/showbook.dox?id=%s';
44              
45             #--------------------------------------------------------------------------
46              
47             ###########################################################################
48             #Inheritence #
49             ###########################################################################
50              
51             @ISA = qw(WWW::Scraper::ISBN::Driver);
52              
53             ###########################################################################
54             #Interface Functions #
55             ###########################################################################
56              
57             =head1 METHODS
58              
59             =over 4
60              
61             =item C
62              
63             Creates a query string, then passes the appropriate form fields to the Srbook
64             server.
65              
66             The returned page should be the correct catalog page for that ISBN. If not the
67             function returns zero and allows the next driver in the chain to have a go. If
68             a valid page is returned, the following fields are returned via the book hash:
69              
70             isbn
71             title
72             author
73             pages
74             book_link
75             image_link
76             pubdate
77             publisher
78             price_list
79             price_sell
80              
81             The book_link and image_link refer back to the Books website.
82              
83             =back
84              
85             =cut
86              
87             sub search {
88             my $self = shift;
89             my $isbn = shift;
90             $self->found(0);
91             $self->book(undef);
92              
93             my $url = sprintf(QUERY, $isbn);
94             my $mechanize = WWW::Mechanize->new();
95             $mechanize->get( $url );
96             return undef unless($mechanize->success());
97              
98             # The Search Results page
99             my $template = <
100             [% title %][% ... %]
101             作者:[% ... %] [% author %]著[% ... %]
102             ISBN碼: [% isbn %][% ... %]
103             出版社:[% ... %] [% publisher %][% ... %]
104             頁數:[% ... %] [% pages %][% ... %]
105             定價:[% ... %] [% price_list %][% ... %]
106             特價:[% ... %][% price_sell %][% ... %]
107             出版年月:[% ... %] [% pubdate %]
108             END
109              
110             my $extract = Template::Extract->new;
111             my $data = $extract->extract($template, $mechanize->content());
112              
113             return $self->handler("Could not extract data from TWSrbook result page.")
114             unless(defined $data);
115              
116             $data->{price_sell} =~ s/^\$\s*(\d+)\s*$/$1/;
117              
118             my $bk = {
119             'isbn' => $data->{isbn},
120             'title' => $data->{title},
121             'author' => $data->{author},
122             'pages' => $data->{pages},
123             'book_link' => $mechanize->uri(),
124             'image_link' => "http://www.srbook.com.tw/web/show_pic_s.dox?id=$isbn",
125             'pubdate' => $data->{pubdate},
126             'publisher' => $data->{publisher},
127             'price_list' => $data->{price_list},
128             'price_sell' => $data->{price_sell},
129             };
130              
131             $self->book($bk);
132             $self->found(1);
133             return $self->book;
134             }
135              
136             1;
137             __END__