blib/lib/WWW/Scraper/ISBN/TWTenlong_Driver.pm

filter: Statementstmt 70.2% 33 / 47 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 70.4% 43 / 61 CC 3 SCAR 9.5
CC
3
Cov
65%
search
6.8
BEGIN
1.0
BEGIN
1.0
CRAP 2.6
↑ Summary
1 # ex:ts=8
2  
3 package WWW::Scraper::ISBN::TWTenlong_Driver;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $VERSION = '0.01';
10  
11 #--------------------------------------------------------------------------
12  
13 =head1 NAME
14  
15 WWW::Scraper::ISBN::TWTenlong_Driver - Search driver for TWTenlong's online catalog.
16  
17 =head1 SYNOPSIS
18  
19 See parent class documentation (L<WWW::Scraper::ISBN::Driver>)
20  
21 =head1 DESCRIPTION
22  
23 Searches for book information from the TWTenlong's online catalog.
24  
25 =cut
26  
27 #--------------------------------------------------------------------------
28  
29 ###########################################################################
30 #Library Modules #
31 ###########################################################################
32  
33 1 ▶use WWW::Scraper::ISBN::Driver;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use Template::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 ###########################################################################
40 #Constants #
41 ###########################################################################
42  
43 1 ▶use constant TENLONG => 'http://www.tenlong.com.tw';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 #--------------------------------------------------------------------------
46  
47 ###########################################################################
48 #Inheritence #
49 ###########################################################################
50  
51 1 @ISA = qw(WWW::Scraper::ISBN::Driver);
52  
53 ###########################################################################
54 #Interface Functions #
55 ###########################################################################
56  
57 =head1 METHODS
58  
59 =over 4
60  
61 =item C<search()>
62  
63 Creates a query string, then passes the appropriate form fields to the Tenlong
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 Tenlong website.
82  
83 =back
84  
85 =cut
86  
87 1 ▶sub search {
Subroutine search: called CC=3 SCAR=19.2
Pod documented
88 1 my $self = shift;
89 1 my $isbn = shift;
90 1 $self->found(0);
91 1 $self->book(undef);
92  
93 1 my $mechanize = WWW::Mechanize->new();
94 1 $mechanize->get(TENLONG);
95  
96 0 $mechanize->submit_form(
97 form_name => 'BookSearchForm',
98 fields => {
99 fKeyword => $isbn,
100 },
101 );
102  
103 # The Search Results page
104 0 my $template = <<END;
105 ÃöÁä¦r¬d¸ßµ²ªG[% ... %]<a href="[% book %]">
106 END
107  
108 0 my $extract = Template::Extract->new;
109 0 my $data = $extract->extract($template, $mechanize->content());
110  
111 0 ▶ return $self->handler("Could not extract data from TWTenlong result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
112 unless(defined $data);
113  
114 0 my $book = $data->{book};
115 0 $mechanize->get($book);
116  
117 0 $template = <<END;
118 <!-- InstanceBeginEditable name="Edit1" -->[% ... %]
119 <font size="4"><b>[% title %]</b>[% ... %]
120 &nbsp;by [% author %]</td>[% ... %]
121 <div align="center"><img src="[% image_link %]"[% ... %]
122 ISBN :[% ... %]&nbsp;[% isbn %]</td>[% ... %]
123 ¥Xª©°Ó :[% ... %]&nbsp;[% publisher %]</td>[% ... %]
124 ¥Xª©¤é´Á :[% ... %]&nbsp;[% pubdate %]</td>[% ... %]
125 ­¶¼Æ :[% ... %]&nbsp;[% pages %]</td>[% ... %]
126 ©w»ù :[% ... %]">[% price_list %]</font>[% ... %]
127 °â»ù :[% ... %]">[% price_sell %]</font>
128 END
129  
130 0 $data = $extract->extract($template, $mechanize->content());
131  
132 0 ▶ return $self->handler("Could not extract data from TWTenlong result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
133 unless(defined $data);
134  
135 my $bk = {
136 'isbn' => $data->{isbn},
137 'title' => $data->{title},
138 'author' => $data->{author},
139 'pages' => $data->{pages},
140 'book_link' => TENLONG.$book,
141 'image_link' => TENLONG."/BookSearch/".$data->{image_link},
142 'pubdate' => $data->{pubdate},
143 'publisher' => $data->{publisher},
144 'price_list' => $data->{price_list},
145 'price_sell' => $data->{price_sell},
146 0 };
147  
148 0 $self->book($bk);
149 0 $self->found(1);
150 0 return $self->book;
151 }
152  
153 1 1;
154 __END__
↑ Summary