File Coverage

blib/lib/WWW/Scraper/ISBN/TWYlib_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::TWYlib_Driver;
4              
5 1     1   799 use strict;
  1         3  
  1         34  
6 1     1   6 use warnings;
  1         2  
  1         35  
7              
8 1     1   5 use vars qw($VERSION @ISA);
  1         10  
  1         70  
9             $VERSION = '0.01';
10              
11             #--------------------------------------------------------------------------
12              
13             =head1 NAME
14              
15             WWW::Scraper::ISBN::TWYlib_Driver - Search driver for TWYlib'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 TWYlib's online catalog.
24              
25             =cut
26              
27             #--------------------------------------------------------------------------
28              
29             ###########################################################################
30             #Library Modules #
31             ###########################################################################
32              
33 1     1   7 use WWW::Scraper::ISBN::Driver;
  1         1  
  1         22  
34 1     1   612 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://tsearch.ylib.com/tsearch/tp.asp?query=%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 Ylib
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             ean
72             title
73             author
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 Ylib 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             書名[% ... %]
101             END
102              
103             my $extract = Template::Extract->new;
104             my $data = $extract->extract($template, $mechanize->content());
105              
106             return $self->handler("Could not extract data from TWYlib result page.")
107             unless(defined $data);
108              
109             my $book = $data->{book};
110             $mechanize->get($book);
111              
112             $template = <
113             [% ... %]
114             [% title %][% ... %]
115             [% ... %]
116            
117             [% ... %]
118             [% author %] 著[% ... %]
119             初版:[% pubdate %]• 出版:[% publisher %]
[% ... %]
120             頁數:[% pages %]頁[% ... %]
121             ISBN:[% isbn %] . EAN:[% ean %]

[% ... %]
122             定價:[% price_list %]元[% ... %]
123             優惠價:[% ... %][% price_sell %]
124             END
125              
126             $data = $extract->extract($template, $mechanize->content());
127              
128             return $self->handler("Could not extract data from TWYlib result page.")
129             unless(defined $data);
130              
131             $data->{ean} =~ s/\s+$//;
132             $data->{author} =~ s/\/著//;
133             $data->{pubdate} =~ s/\s+$//;
134             $data->{publisher} =~ s/\s+$//;
135             $data->{price_sell} =~ s/^\s+//;
136              
137             my $bk = {
138             'isbn' => $data->{isbn},
139             'ean' => $data->{ean},
140             'title' => $data->{title},
141             'author' => $data->{author},
142             'book_link' => "http://www.ylib.com/search/".$book,
143             'image_link' => "http://www.ylib.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             $self->book($bk);
151             $self->found(1);
152             return $self->book;
153             }
154              
155             1;
156             __END__