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

filter: Statementstmt 64.7% 33 / 51 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 68.2% 43 / 63 CC 2 SCAR 4.5
CC
2
Cov
62%
search
3.8
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
↑ Summary
1 # ex:ts=8
2  
3 package WWW::Scraper::ISBN::TWEslitebooks_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.02';
10  
11 #--------------------------------------------------------------------------
12  
13 =head1 NAME
14  
15 WWW::Scraper::ISBN::TWEslitebooks_Driver - Search driver for TWEslitebooks' 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 TWEslitebooks' 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 1 ▶use Text::Iconv;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ###########################################################################
39 #Constants #
40 ###########################################################################
41  
42 1 ▶use constant ESLITEBOOKS => 'http://www.eslitebooks.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 #--------------------------------------------------------------------------
45  
46 ###########################################################################
47 #Inheritence #
48 ###########################################################################
49  
50 1 @ISA = qw(WWW::Scraper::ISBN::Driver);
51  
52 ###########################################################################
53 #Interface Functions #
54 ###########################################################################
55  
56 =head1 METHODS
57  
58 =over 4
59  
60 =item C<search()>
61  
62 Creates a query string, then passes the appropriate form fields to the Eslitebooks
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 ean
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 Eslitebooks website.
82  
83 =back
84  
85 =cut
86  
87 1 ▶sub search {
Subroutine search: called CC=2 SCAR=13.4
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(ESLITEBOOKS);
95  
96 0 $mechanize->submit_form(
97 form_name => 'frmbas',
98 fields => {
99 query => $isbn,
100 },
101 );
102  
103 0 my $book_link = $mechanize->uri;
104  
105 0 my $conv = Text::Iconv->new("utf-8", "big5");
106 0 my $content = $mechanize->content();
107 0 $content =~ /(<td valign="middle" align="center" height="180" style="border-bottom: #a0a882.*meta)/s;
108 0 $content = $conv->convert($1);
109  
110 0 my $template = <<END;
111 <td valign="middle" align="center" height="180" style="border-bottom: #a0a882 [% ... %]
112 <img hspace="5" src="[% image_link %]" [% ... %]
113 <iframe src='[% iframe %]'[% ... %]
114 s10lh14ls1b'>[% title %]</div>[% ... %]
115 s9lh14ls1>§@ªÌ¢A[% ... %]>[% author %]<[% ... %]
116 ¥Xª©ªÀ¢A[% ... %]>[% publisher %]<[% ... %]
117 ¥Xª©¤é´Á¢A [% pubdate %]<[% ... %]
118 EAN¢A[% ean %]<br>[% ... %]
119 ­¶¼Æ¢A[% pages %]<br>
120 END
121  
122 0 my $extract = Template::Extract->new;
123 0 my $data = $extract->extract($template, $content);
124  
125 0 ▶ return $self->handler("Could not extract data from TWEslitebooks result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
126 unless(defined $data);
127  
128 0 $mechanize->get($data->{iframe});
129  
130 0 my $tmp = $conv->convert($mechanize->content());
131 0 $tmp =~ /©w»ù.*>(\d+).*ºô¸ô»ù.*>(\d+)/s;
132 0 my ($price_list, $price_sell) = ($1, $2);
133  
134 my $bk = {
135 'isbn' => $isbn,
136 'ean' => $data->{ean},
137 'title' => $data->{title},
138 'author' => $data->{author},
139 'pages' => $data->{pages},
140 'book_link' => "$book_link",
141 'image_link' => ESLITEBOOKS.$data->{image_link},
142 'pubdate' => $data->{pubdate},
143 'publisher' => $data->{publisher},
144 0 'price_list' => $price_list,
145 'price_sell' => $price_sell,
146 };
147  
148 0 $self->book($bk);
149 0 $self->found(1);
150 0 return $self->book;
151 }
152  
153 1 1;
154 __END__
↑ Summary