blib/lib/WWW/Scraper/ISBN/TWPchome_Driver.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 31 | 49 | 63.2 |
branch | 0 | 6 | 0.0 |
condition | n/a | ||
subroutine | 9 | 9 | 100.0 |
pod | 1 | 1 | 100.0 |
total | 41 | 65 | 63.0 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # ex:ts=8 | ||||||
2 | |||||||
3 | package WWW::Scraper::ISBN::TWPchome_Driver; | ||||||
4 | |||||||
5 | 1 | 1 | 852 | use strict; | |||
1 | 2 | ||||||
1 | 42 | ||||||
6 | 1 | 1 | 6 | use warnings; | |||
1 | 2 | ||||||
1 | 38 | ||||||
7 | |||||||
8 | 1 | 1 | 6 | use vars qw($VERSION @ISA); | |||
1 | 10 | ||||||
1 | 77 | ||||||
9 | $VERSION = '0.01'; | ||||||
10 | |||||||
11 | #-------------------------------------------------------------------------- | ||||||
12 | |||||||
13 | =head1 NAME | ||||||
14 | |||||||
15 | WWW::Scraper::ISBN::TWPchome_Driver - Search driver for TWPchome'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 TWPchome's online catalog. | ||||||
24 | |||||||
25 | =cut | ||||||
26 | |||||||
27 | #-------------------------------------------------------------------------- | ||||||
28 | |||||||
29 | ########################################################################### | ||||||
30 | #Library Modules # | ||||||
31 | ########################################################################### | ||||||
32 | |||||||
33 | 1 | 1 | 7 | use WWW::Scraper::ISBN::Driver; | |||
1 | 2 | ||||||
1 | 27 | ||||||
34 | 1 | 1 | 1421 | use WWW::Mechanize; | |||
1 | 167990 | ||||||
1 | 37 | ||||||
35 | 1 | 1 | 819 | use Template::Extract; | |||
1 | 627 | ||||||
1 | 25 | ||||||
36 | |||||||
37 | 1 | 1 | 401810 | use Data::Dumper; | |||
1 | 8210 | ||||||
1 | 106 | ||||||
38 | |||||||
39 | ########################################################################### | ||||||
40 | #Constants # | ||||||
41 | ########################################################################### | ||||||
42 | |||||||
43 | 1 | 1 | 12 | use constant QUERY => 'http://ec2.pchome.com.tw/adm/search.htm?search_word=202&search_prod=008&getkey=%s'; | |||
1 | 2 | ||||||
1 | 540 | ||||||
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 Pchome | ||||||
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 Pchome website. | ||||||
82 | |||||||
83 | =back | ||||||
84 | |||||||
85 | =cut | ||||||
86 | |||||||
87 | sub search { | ||||||
88 | 1 | 1 | 1 | 1882 | my $self = shift; | ||
89 | 1 | 6 | my $isbn = shift; | ||||
90 | 1 | 9 | $self->found(0); | ||||
91 | 1 | 24 | $self->book(undef); | ||||
92 | |||||||
93 | 1 | 16 | my $url = sprintf(QUERY, $isbn); | ||||
94 | 1 | 10 | my $mechanize = WWW::Mechanize->new(); | ||||
95 | 1 | 19123 | $mechanize->get($url); | ||||
96 | 0 | 0 | return undef unless($mechanize->success()); | ||||
97 | |||||||
98 | # The Search Results page | ||||||
99 | 0 | my $template = < | |||||
100 | 商品名稱[% ... %] | ||||||
101 | END | ||||||
102 | |||||||
103 | 0 | my $extract = Template::Extract->new; | |||||
104 | 0 | my $data = $extract->extract($template, $mechanize->content()); | |||||
105 | |||||||
106 | 0 | 0 | return $self->handler("Could not extract data from TWPchome result page.") | ||||
107 | unless(defined $data); | ||||||
108 | |||||||
109 | 0 | $data->{book} =~ s/^\.\.\///; | |||||
110 | 0 | my $book = "http://ec2.pchome.com.tw/".$data->{book}; | |||||
111 | |||||||
112 | 0 | $mechanize->get($book); | |||||
113 | |||||||
114 | 0 | $template = < | |||||
115 | [% ... %] | ||||||
116 | [% title %][% ... %] | ||||||
117 | [% ... %] | ||||||
118 | [% ... %] | ||||||
119 | [% ... %] | ||||||
120 | 作者:[% author %] [% ... %] |
||||||
121 | 出版社:[% publisher %] [% ... %] |
||||||
122 | 頁數:[% pages %] [% ... %] |
||||||
123 | 初版日:[% pubdate %] [% ... %] |
||||||
124 | ISBN:[% isbn %] [% ... %] |
||||||
125 | 定價[% ... %] |
||||||
126 | 特價[% ... %]'>[% price_sell %] | ||||||
127 | END | ||||||
128 | |||||||
129 | 0 | $data = $extract->extract($template, $mechanize->content()); | |||||
130 | |||||||
131 | 0 | 0 | return $self->handler("Could not extract data from TWPchome result page.") | ||||
132 | unless(defined $data); | ||||||
133 | |||||||
134 | 0 | $data->{title} =~ s/^\s*//; | |||||
135 | 0 | $data->{title} =~ s/\s*$//; | |||||
136 | 0 | $data->{price_list} =~ s/\$//; | |||||
137 | |||||||
138 | 0 | my $bk = { | |||||
139 | 'isbn' => $data->{isbn}, | ||||||
140 | 'title' => $data->{title}, | ||||||
141 | 'author' => $data->{author}, | ||||||
142 | 'pages' => $data->{pages}, | ||||||
143 | 'book_link' => $book, | ||||||
144 | 'image_link' => $data->{image_link}, | ||||||
145 | 'pubdate' => $data->{pubdate}, | ||||||
146 | 'publisher' => $data->{publisher}, | ||||||
147 | 'price_list' => $data->{price_list}, | ||||||
148 | 'price_sell' => $data->{price_sell}, | ||||||
149 | }; | ||||||
150 | |||||||
151 | 0 | $self->book($bk); | |||||
152 | 0 | $self->found(1); | |||||
153 | 0 | return $self->book; | |||||
154 | } | ||||||
155 | |||||||
156 | 1; | ||||||
157 | __END__ |