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

filter: Statementstmt 67.2% 39 / 58 Branchbran 25.0% 2 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 67.1% 51 / 76 CC 5 SCAR 16.2
CC
5
Cov
62%
search
11.6
BEGIN
1.0
BEGIN
1.0
CRAP 5.1
↑ Summary
1 # ex:ts=8
2  
3 package WWW::Scraper::ISBN::TWCwbook_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::TWCwbook_Driver - Search driver for TWCwbook'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 TWCwbook'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 1 ▶use Text::Iconv;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ###########################################################################
39 #Constants #
40 ###########################################################################
41  
42 1 ▶use constant QUERY => "http://www.cwbook.com.tw/search/result1.jsp?field=2&keyWord=%s";
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 Cwbook
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  
78 The book_link and image_link refer back to the Cwbook website.
79  
80 =back
81  
82 =cut
83  
84 1 ▶sub search {
Subroutine search: called CC=5 SCAR=24.5
Pod documented
85 1 my $self = shift;
86 1 my $isbn = shift;
87 1 $self->found(0);
88 1 $self->book(undef);
89  
90 1 my $url = sprintf(QUERY, $isbn);
91 1 my $mechanize = WWW::Mechanize->new();
92 1 $mechanize->get($url);
93 1 ▶ return undef unless($mechanize->success());
Branch: 1 branch
BranchTrueFalseTotal
unless $mechanize->success 0 1 1
94  
95 1 my $template = <<END;
96 <input type="checkbox" name="productID" value="[% bookid %]">
97 END
98  
99 1 my $extract = Template::Extract->new;
100 1 my $data = $extract->extract($template, $mechanize->content());
101  
102 1 ▶ return $self->handler("Could not extract data from TWCwbook result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 0 1
103 unless(defined $data);
104  
105 0 $url = "http://www.cwbook.com.tw/common/book.jsp?productID=$data->{bookid}";
106 0 $mechanize->get($url);
107 0 ▶ return undef unless($mechanize->success());
Branch: 1 branch
BranchTrueFalseTotal
unless $mechanize->success 0 0 0
108  
109 0 $template = <<END;
110 <div id="main" class="product">[% ... %]
111 <h2>[% title %]</h2>[% ... %]
112 <div class="block"><img src="[% image_link %]" ></div>[% ... %]
113 <li class="author">[% author %]</li>[% ... %]
114 <li class="publisher">[% publisher %]</li>[% ... %]
115 <li class="pubdate">[% pubdate %]</li>[% ... %]
116 <li class="price1">[% price_list %]</li>
117 END
118  
119 0 $data = $extract->extract($template, $mechanize->content());
120 0 ▶ return $self->handler("Could not extract data from TWCwbook result page.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
121 unless(defined $data);
122  
123 0 my $conv = Text::Iconv->new("utf-8", "big5");
124 0 $data->{title} = $conv->convert($data->{title});
125 0 $data->{title} =~ s/[ \r\t]//g;
126 0 $data->{author} = $conv->convert($data->{author});
127 0 $data->{author} =~ s/§@ªÌ¡G(.*)/$1/;
128 0 $data->{publisher} = $conv->convert($data->{publisher});
129 0 $data->{publisher} =~ s/¥Xª©ªÀ¡G(.*)/$1/;
130 0 $data->{pubdate} =~ s/\D*(\d+\/\d+\/\d+).*/$1/;
131 0 $data->{price_list} =~ s/\D+(\d+).*/$1/;
132  
133 my $bk = {
134 'isbn' => $isbn,
135 'title' => $data->{title},
136 'author' => $data->{author},
137 'book_link' => $url,
138 'image_link' => "http://www.cwbook.com.tw".$data->{image_link},
139 'pubdate' => $data->{pubdate},
140 'publisher' => $data->{publisher},
141 'price_list' => $data->{price_list},
142 0 };
143  
144 0 $self->book($bk);
145 0 $self->found(1);
146 0 return $self->book;
147 }
148  
149 1 1;
150 __END__
↑ Summary