| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # ex:ts=8 | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package WWW::Scraper::ISBN::TWBooks_Driver; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 1012 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 6 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 5 | use vars qw($VERSION @ISA); | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 245 |  | 
| 9 |  |  |  |  |  |  | $VERSION = '0.02'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =head1 NAME | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | WWW::Scraper::ISBN::TWBooks_Driver - Search driver for TWBooks' 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 TWBooks' 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 |  |  |  |  | 23 |  | 
| 34 | 1 |  |  | 1 |  | 1707 | use WWW::Mechanize; | 
|  | 1 |  |  |  |  | 336345 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 35 | 1 |  |  | 1 |  | 1018 | use Template::Extract; | 
|  | 1 |  |  |  |  | 691 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 36 | 1 |  |  | 1 |  | 705 | use Text::Iconv; | 
|  | 1 |  |  |  |  | 10626 |  | 
|  | 1 |  |  |  |  | 446 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | ########################################################################### | 
| 39 |  |  |  |  |  |  | #Constants                                                                # | 
| 40 |  |  |  |  |  |  | ########################################################################### | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  | 1 |  | 16 | use constant	QUERY	=> 'http://search.books.com.tw/exep/prod_search.php?cat=001&key=%s'; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 1240 |  | 
| 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 Books | 
| 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 |  |  |  |  |  |  | pages | 
| 73 |  |  |  |  |  |  | book_link | 
| 74 |  |  |  |  |  |  | image_link | 
| 75 |  |  |  |  |  |  | pubdate | 
| 76 |  |  |  |  |  |  | publisher | 
| 77 |  |  |  |  |  |  | price_list | 
| 78 |  |  |  |  |  |  | price_sell | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | The book_link and image_link refer back to the Books website. | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =back | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =cut | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub search { | 
| 87 | 1 |  |  | 1 | 1 | 1499 | my $self = shift; | 
| 88 | 1 |  |  |  |  | 3 | my $isbn = shift; | 
| 89 | 1 |  |  |  |  | 10 | $self->found(0); | 
| 90 | 1 |  |  |  |  | 23 | $self->book(undef); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 1 |  |  |  |  | 14 | my $url = sprintf(QUERY, $isbn); | 
| 93 | 1 |  |  |  |  | 9 | my $mechanize = WWW::Mechanize->new(); | 
| 94 | 1 |  |  |  |  | 39947 | $mechanize->get( $url ); | 
| 95 | 1 | 50 |  |  |  | 1380824 | return undef unless($mechanize->success()); | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # The Search Results page | 
| 98 | 1 |  |  |  |  | 34 | my $template = < | 
| 99 |  |  |  |  |  |  |  | [% ... %] | 
| 100 |  |  |  |  |  |  | image=[% image_link %]&width=[% ... %] | 
| 101 |  |  |  |  |  |  |  
| 102 |  |  |  |  |  |  |  |   
 
| 103 |  |  |  |  |  |  |  |  |  
| 104 |  |  |  |  |  |  | [% title %][% ... %] |  
| 105 |  |  |  |  |  |  | 106 |  |  |  |  |  |  | 107 |  |  |  |  |  |  | [% pubdate %][% ... %] |  
| 108 |  |  |  |  |  |  | [% price_list %][% ... %] |  
| 109 |  |  |  |  |  |  | [% price_sell %][% ... %] |  
| 110 |  |  |  |  |  |  | / [% pages %] / [% ... %] |  
| 111 |  |  |  |  |  |  | ISBN[% ... %][% isbn %] |  
| 112 |  |  |  |  |  |  | END |  
| 113 |  |  |  |  |  |  |  |  
| 114 | 1 |  |  |  |  | 13 | my $extract = Template::Extract->new; |  
| 115 | 1 |  |  |  |  | 102753 | my $data = $extract->extract($template, $mechanize->content()); |  
| 116 |  |  |  |  |  |  |  |  
| 117 | 1 | 50 |  |  |  | 16872 | return $self->handler("Could not extract data from TWBooks result page.") |  
| 118 |  |  |  |  |  |  | unless(defined $data); |  
| 119 |  |  |  |  |  |  |  |  
| 120 | 0 |  |  |  |  |  | $data->{image_link} =~ m/(\d+).jpg/; |  
| 121 | 0 |  |  |  |  |  | my $tmp = $1; |  
| 122 |  |  |  |  |  |  |  |  
| 123 | 0 |  |  |  |  |  | $data->{pages} =~ s/(\d+).*/$1/; |  
| 124 |  |  |  |  |  |  |  |  
| 125 | 0 |  |  |  |  |  | my $conv = Text::Iconv->new("utf-8", "big5"); |  
| 126 |  |  |  |  |  |  |  |  
| 127 | 0 |  |  |  |  |  | my $bk = { |  
| 128 |  |  |  |  |  |  | 'isbn'		=> $data->{isbn}, |  
| 129 |  |  |  |  |  |  | 'title'		=> $conv->convert($data->{title}), |  
| 130 |  |  |  |  |  |  | 'author'	=> $conv->convert($data->{author}), |  
| 131 |  |  |  |  |  |  | 'pages'		=> $data->{pages}, |  
| 132 |  |  |  |  |  |  | 'book_link'	=> "http://www.books.com.tw/exep/prod/booksfile.php?item=$tmp", |  
| 133 |  |  |  |  |  |  | 'image_link'	=> $data->{image_link}, |  
| 134 |  |  |  |  |  |  | 'pubdate'	=> $conv->convert($data->{pubdate}), |  
| 135 |  |  |  |  |  |  | 'publisher'	=> $conv->convert($data->{publisher}), |  
| 136 |  |  |  |  |  |  | 'price_list'	=> $data->{price_list}, |  
| 137 |  |  |  |  |  |  | 'price_sell'	=> $data->{price_sell}, |  
| 138 |  |  |  |  |  |  | }; |  
| 139 |  |  |  |  |  |  |  |  
| 140 | 0 |  |  |  |  |  | $self->book($bk); |  
| 141 | 0 |  |  |  |  |  | $self->found(1); |  
| 142 | 0 |  |  |  |  |  | return $self->book; |  
| 143 |  |  |  |  |  |  | } |  
| 144 |  |  |  |  |  |  |  |  
| 145 |  |  |  |  |  |  | 1; |  
| 146 |  |  |  |  |  |  | __END__ |  |