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

filter: Statementstmt 64.1% 34 / 53 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 63.7% 44 / 69 CC 4 SCAR 15.1
CC
4
Cov
58%
search
11.6
BEGIN
1.0
BEGIN
1.0
CRAP 4.5
↑ Summary
1 # ex:ts=8
2  
3 package WWW::Scraper::ISBN::TWYlib_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::TWYlib_Driver - Search driver for TWYlib'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 TWYlib'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 QUERY => 'http://tsearch.ylib.com/tsearch/tp.asp?query=%s';
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 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 1 ▶sub search {
Subroutine search: called CC=4 SCAR=24.5
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 $url = sprintf(QUERY, $isbn);
94 1 my $mechanize = WWW::Mechanize->new();
95 1 $mechanize->get($url);
96 0 ▶ return undef unless($mechanize->success());
Branch: 1 branch
BranchTrueFalseTotal
unless $mechanize->success 0 0 0
97  
98 # The Search Results page
99 0 my $template = <<END;
100 ®Ñ¦W[% ... %]<A HREF='[% book %]'>
101 END
102  
103 0 my $extract = Template::Extract->new;
104 0 my $data = $extract->extract($template, $mechanize->content());
105  
106 0 ▶ return $self->handler("Could not extract data from TWYlib result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
107 unless(defined $data);
108  
109 0 my $book = $data->{book};
110 0 $mechanize->get($book);
111  
112 0 $template = <<END;
113 <!-- ®ÑÄy¸Ô²Ó¸ê®Æ -->[% ... %]
114 <font color="#333333"><b>[% title %]</b>[% ... %]
115 <!--®ÑÄy«Ê­± -->[% ... %]
116 <IMG SRC="[% image_link %]" [% ... %]
117 <!-- ®ÑÄy¸Ô²Ó¸ê®Æ -->[% ... %]
118 <A HREF="[% ... %]">[% author %]</A> µÛ[% ... %]
119 ªìª©¡G[% pubdate %]¡E ¥Xª©¡G[% publisher %]<BR>[% ... %]
120 ­¶¼Æ¡G[% pages %]­¶[% ... %]
121 ISBN¡G[% isbn %] ¡D EAN¡G[% ean %]</p>[% ... %]
122 ©w»ù¡G[% price_list %]¤¸[% ... %]
123 Àu´f»ù¡G[% ... %]<B>[% price_sell %]</B>
124 END
125  
126 0 $data = $extract->extract($template, $mechanize->content());
127  
128 0 ▶ return $self->handler("Could not extract data from TWYlib result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
129 unless(defined $data);
130  
131 0 $data->{ean} =~ s/\s+$//;
132 0 $data->{author} =~ s/\/µÛ//;
133 0 $data->{pubdate} =~ s/\s+$//;
134 0 $data->{publisher} =~ s/\s+$//;
135 0 $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 0 };
149  
150 0 $self->book($bk);
151 0 $self->found(1);
152 0 return $self->book;
153 }
154  
155 1 1;
156 __END__
↑ Summary