File Coverage

blib/lib/WWW/Scraper/ISBN/TWSanmin_Driver.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


[% ... %] [% ... %] [% ... %]
line stmt bran cond sub pod time code
1             # ex:ts=8
2              
3             package WWW::Scraper::ISBN::TWSanmin_Driver;
4              
5 1     1   700 use strict;
  1         2  
  1         47  
6 1     1   4 use warnings;
  1         2  
  1         30  
7              
8 1     1   7 use vars qw($VERSION @ISA);
  1         9  
  1         82  
9             $VERSION = '0.02';
10              
11             #--------------------------------------------------------------------------
12              
13             =head1 NAME
14              
15             WWW::Scraper::ISBN::TWSanmin_Driver - Search driver for TWSanmin'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 TWSanmin's online catalog.
24              
25             =cut
26              
27             #--------------------------------------------------------------------------
28              
29             ###########################################################################
30             #Library Modules #
31             ###########################################################################
32              
33 1     1   6 use WWW::Scraper::ISBN::Driver;
  1         1  
  1         30  
34 1     1   1105 use WWW::Mechanize;
  1         166521  
  1         48  
35 1     1   984 use Template::Extract;
  1         605  
  1         24  
36 1     1   431 use Text::Iconv;
  0            
  0            
37              
38             ###########################################################################
39             #Constants #
40             ###########################################################################
41              
42             use constant QUERY => 'http://www.sanmin.com.tw/page-qsearch.asp?ct=search_isbn1&qu=%s';
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 Sanmin
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             book_link
73             image_link
74             pubdate
75             publisher
76             price_list
77             price_sell
78              
79             The book_link and image_link refer back to the Sanmin website.
80              
81             =back
82              
83             =cut
84              
85             sub search {
86             my $self = shift;
87             my $isbn = shift;
88             $self->found(0);
89             $self->book(undef);
90              
91             my $url = sprintf(QUERY, $isbn);
92             my $mechanize = WWW::Mechanize->new();
93             $mechanize->get($url);
94             return undef unless($mechanize->success());
95              
96             my $conv = Text::Iconv->new("utf-8", "big5");
97             my $content = $mechanize->content();
98             $content =~ /(table width="98%" align="center" bgcolor=#99CCFF.*-PRICE-)/s;
99             $content = $conv->convert($1);
100              
101             my $template = <
102             ALT="[% title %]">[% ... %]
103            
104             I S B N[% ... %][% isbn %][% ... %]
105             作 者[% ... %][% author %][% ... %]
106             出版社[% ... %][% publisher %] 
107             出版日[% ... %][% pubdate %]
108             原 價[% ... %][% price_list %]元
109             特 價[% ... %][% price_sell %]<
110             END
111              
112             my $extract = Template::Extract->new;
113             my $data = $extract->extract($template, $content);
114              
115             return $self->handler("Could not extract data from TWSanmin result page.")
116             unless(defined $data);
117              
118             $data->{title} =~ s/(.*)(-.*\d+) *$/$1/;
119             $data->{pubdate} =~ s/[ \n\r\t]+//g;
120             $data->{author} = join('', map { $conv->convert(chr($_)) if ($_ =~ /\d+/) } split(/[&#;]/, $data->{author}));
121             $data->{publisher} = join('', map { $conv->convert(chr($_)) if ($_ =~ /\d+/) } split(/[&#;]/, $data->{publisher}));
122              
123             my $bk = {
124             'isbn' => $data->{isbn},
125             'title' => $data->{title},
126             'author' => $data->{author},
127             'book_link' => $mechanize->uri()->as_string,
128             'image_link' => "http://www.sanmin.com.tw/".$data->{image_link},
129             'pubdate' => $data->{pubdate},
130             'publisher' => $data->{publisher},
131             'price_list' => $data->{price_list},
132             'price_sell' => $data->{price_sell},
133             };
134              
135             $self->book($bk);
136             $self->found(1);
137             return $self->book;
138             }
139              
140             1;
141             __END__