File Coverage

blib/lib/WWW/Scraper/ISBN/TWSoidea_Driver.pm
Criterion Covered Total %
statement 30 44 68.1
branch 0 4 0.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 40 58 68.9


line stmt bran cond sub pod time code
1             # ex:ts=8
2              
3             package WWW::Scraper::ISBN::TWSoidea_Driver;
4              
5 1     1   798 use strict;
  1         2  
  1         36  
6 1     1   6 use warnings;
  1         1  
  1         34  
7              
8 1     1   6 use vars qw($VERSION @ISA);
  1         9  
  1         77  
9             $VERSION = '0.01';
10              
11             #--------------------------------------------------------------------------
12              
13             =head1 NAME
14              
15             WWW::Scraper::ISBN::TWSoidea_Driver - Search driver for TWSoidea'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 TWSoidea'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         2  
  1         23  
34 1     1   1297 use WWW::Mechanize;
  1         201954  
  1         49  
35 1     1   984 use Template::Extract;
  1         1026  
  1         34  
36              
37 1     1   1006 use Data::Dumper;
  1         5840  
  1         83  
38              
39             ###########################################################################
40             #Constants #
41             ###########################################################################
42              
43 1     1   10 use constant SOIDEA => 'http://www.soidea.com.tw/soidea/';
  1         2  
  1         309  
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 Soidea
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             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 Soidea website.
81              
82             =back
83              
84             =cut
85              
86             sub search {
87 1     1 1 1077 my $self = shift;
88 1         5 my $isbn = shift;
89 1         6 $self->found(0);
90 1         22 $self->book(undef);
91              
92 1         15 my $mechanize = WWW::Mechanize->new();
93 1         15148 $mechanize->get(SOIDEA);
94              
95 0           $mechanize->submit_form(
96             form_name => 'EinForm0',
97             fields => {
98             Type => 1,
99             Select_option => 'ISBN',
100             textfield => $isbn,
101             },
102             );
103              
104             # The Search Results page
105 0           my $template = <
106             查詢結果共[% ... %]
107             END
108              
109 0           my $extract = Template::Extract->new;
110 0           my $data = $extract->extract($template, $mechanize->content());
111              
112 0 0         return $self->handler("Could not extract data from TWSoidea result page.")
113             unless(defined $data);
114              
115 0           my $book = SOIDEA.$data->{book};
116 0           $mechanize->get($book);
117              
118 0           $template = <
119             [% ... %]
120             [% ... %]
121             [% title %][% ... %]
122             作者:[% author %]
[% ... %]
123             定價 [% price_list %]元
[% ... %]
124             優惠價 [% price_sell %]元[% ... %]
125             出版社/代理商:[% publisher %]
[% ... %]
126             出版/製造日期:[% pubdate %]
[% ... %]
127             ISBN:[% isbn %]
[% ... %]
128             END
129              
130 0           $data = $extract->extract($template, $mechanize->content());
131              
132 0 0         return $self->handler("Could not extract data from TWSoidea result page.")
133             unless(defined $data);
134              
135 0           my $bk = {
136             'isbn' => $data->{isbn},
137             'title' => $data->{title},
138             'author' => $data->{author},
139             'book_link' => $mechanize->uri(),
140             'image_link' => $data->{image_link},
141             'pubdate' => $data->{pubdate},
142             'publisher' => $data->{publisher},
143             'price_list' => $data->{price_list},
144             'price_sell' => $data->{price_sell},
145             };
146              
147 0           $self->book($bk);
148 0           $self->found(1);
149 0           return $self->book;
150             }
151              
152             1;
153             __END__