File Coverage

blib/lib/WWW/Scraper/ISBN/TWCwbook_Driver.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # ex:ts=8
2              
3             package WWW::Scraper::ISBN::TWCwbook_Driver;
4              
5 1     1   2433 use strict;
  1         3  
  1         55  
6 1     1   8 use warnings;
  1         1  
  1         37  
7              
8 1     1   6 use vars qw($VERSION @ISA);
  1         12  
  1         109  
9             $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)
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     1   6 use WWW::Scraper::ISBN::Driver;
  1         1  
  1         148  
34 1     1   518 use WWW::Mechanize;
  0            
  0            
35             use Template::Extract;
36             use Text::Iconv;
37              
38             ###########################################################################
39             #Constants #
40             ###########################################################################
41              
42             use constant QUERY => "http://www.cwbook.com.tw/search/result1.jsp?field=2&keyWord=%s";
43              
44             #--------------------------------------------------------------------------
45              
46             ###########################################################################
47             #Inheritence #
48             ###########################################################################
49              
50             @ISA = qw(WWW::Scraper::ISBN::Driver);
51              
52             ###########################################################################
53             #Interface Functions #
54             ###########################################################################
55              
56             =head1 METHODS
57              
58             =over 4
59              
60             =item C
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             sub search {
85             my $self = shift;
86             my $isbn = shift;
87             $self->found(0);
88             $self->book(undef);
89              
90             my $url = sprintf(QUERY, $isbn);
91             my $mechanize = WWW::Mechanize->new();
92             $mechanize->get($url);
93             return undef unless($mechanize->success());
94              
95             my $template = <
96            
97             END
98              
99             my $extract = Template::Extract->new;
100             my $data = $extract->extract($template, $mechanize->content());
101              
102             return $self->handler("Could not extract data from TWCwbook result page.")
103             unless(defined $data);
104              
105             $url = "http://www.cwbook.com.tw/common/book.jsp?productID=$data->{bookid}";
106             $mechanize->get($url);
107             return undef unless($mechanize->success());
108              
109             $template = <
110            
[% ... %]
111            

[% title %]

[% ... %]
112            
[% ... %]
113            
  • [% author %]
  • [% ... %]
    114            
  • [% publisher %]
  • [% ... %]
    115            
  • [% pubdate %]
  • [% ... %]
    116            
  • [% price_list %]
  • 117             END
    118              
    119             $data = $extract->extract($template, $mechanize->content());
    120             return $self->handler("Could not extract data from TWCwbook result page.")
    121             unless(defined $data);
    122              
    123             my $conv = Text::Iconv->new("utf-8", "big5");
    124             $data->{title} = $conv->convert($data->{title});
    125             $data->{title} =~ s/[ \r\t]//g;
    126             $data->{author} = $conv->convert($data->{author});
    127             $data->{author} =~ s/§@ªÌ¡G(.*)/$1/;
    128             $data->{publisher} = $conv->convert($data->{publisher});
    129             $data->{publisher} =~ s/¥Xª©ªÀ¡G(.*)/$1/;
    130             $data->{pubdate} =~ s/\D*(\d+\/\d+\/\d+).*/$1/;
    131             $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             };
    143              
    144             $self->book($bk);
    145             $self->found(1);
    146             return $self->book;
    147             }
    148              
    149             1;
    150             __END__