| blib/lib/WWW/Scraper/ISBN/TWKingstone_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::TWKingstone_Driver; | |||||||||||||
| 4 | ||||||||||||||
| 5 | 1 | 1 | 1153 | use strict; | ||||||||||
| 1 | 3 | |||||||||||||
| 1 | 41 | |||||||||||||
| 6 | 1 | 1 | 6 | use warnings; | ||||||||||
| 1 | 2 | |||||||||||||
| 1 | 41 | |||||||||||||
| 7 | ||||||||||||||
| 8 | 1 | 1 | 6 | use vars qw($VERSION @ISA); | ||||||||||
| 1 | 12 | |||||||||||||
| 1 | 146 | |||||||||||||
| 9 | $VERSION = '0.02'; | |||||||||||||
| 10 | ||||||||||||||
| 11 | #-------------------------------------------------------------------------- | |||||||||||||
| 12 | ||||||||||||||
| 13 | =head1 NAME | |||||||||||||
| 14 | ||||||||||||||
| 15 | WWW::Scraper::ISBN::TWKingstone_Driver - Search driver for TWKingstone'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 TWKingstone's online catalog. | |||||||||||||
| 24 | ||||||||||||||
| 25 | =cut | |||||||||||||
| 26 | ||||||||||||||
| 27 | #-------------------------------------------------------------------------- | |||||||||||||
| 28 | ||||||||||||||
| 29 | ########################################################################### | |||||||||||||
| 30 | #Library Modules # | |||||||||||||
| 31 | ########################################################################### | |||||||||||||
| 32 | ||||||||||||||
| 33 | 1 | 1 | 8 | use WWW::Scraper::ISBN::Driver; | ||||||||||
| 1 | 1 | |||||||||||||
| 1 | 28 | |||||||||||||
| 34 | 1 | 1 | 1547 | use WWW::Mechanize; | ||||||||||
| 1 | 188974 | |||||||||||||
| 1 | 43 | |||||||||||||
| 35 | 1 | 1 | 930 | use Template::Extract; | ||||||||||
| 1 | 692 | |||||||||||||
| 1 | 24 | |||||||||||||
| 36 | 1 | 1 | 797 | use Text::Iconv; | ||||||||||
| 1 | 3106 | |||||||||||||
| 1 | 71 | |||||||||||||
| 37 | ||||||||||||||
| 38 | ########################################################################### | |||||||||||||
| 39 | #Constants # | |||||||||||||
| 40 | ########################################################################### | |||||||||||||
| 41 | ||||||||||||||
| 42 | 1 | 1 | 9 | use constant QUERY => 'http://search.kingstone.com.tw/Result.asp?SE_Type=ISBN&k=%s'; | ||||||||||
| 1 | 2 | |||||||||||||
| 1 | 494 | |||||||||||||
| 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 Kingstone | |||||||||||||
| 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 | price_sell | |||||||||||||
| 78 | ||||||||||||||
| 79 | The book_link and image_link refer back to the Kingstone website. | |||||||||||||
| 80 | ||||||||||||||
| 81 | =back | |||||||||||||
| 82 | ||||||||||||||
| 83 | =cut | |||||||||||||
| 84 | ||||||||||||||
| 85 | sub search { | |||||||||||||
| 86 | 1 | 1 | 1 | 1858 | my $self = shift; | |||||||||
| 87 | 1 | 2 | my $isbn = shift; | |||||||||||
| 88 | 1 | 11 | $self->found(0); | |||||||||||
| 89 | 1 | 24 | $self->book(undef); | |||||||||||
| 90 | ||||||||||||||
| 91 | 1 | 14 | my $url = sprintf(QUERY, $isbn); | |||||||||||
| 92 | 1 | 10 | my $mechanize = WWW::Mechanize->new(); | |||||||||||
| 93 | 1 | 16834 | $mechanize->get($url); | |||||||||||
| 94 | 0 | 0 | return undef unless($mechanize->success()); | |||||||||||
| 95 | ||||||||||||||
| 96 | # The Search Results page | |||||||||||||
| 97 | 0 | my $template = < | ||||||||||||
| 98 | ||||||||||||||
| 99 | [% ... %] | |||||||||||||
| 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 TWKingstone result page.") | |||||||||||
| 107 | unless(defined $data); | |||||||||||||
| 108 | ||||||||||||||
| 109 | 0 | my $book_link = $data->{book_link}; | ||||||||||||
| 110 | 0 | $mechanize->get($book_link); | ||||||||||||
| 111 | ||||||||||||||
| 112 | 0 | my $content = $mechanize->content(); | ||||||||||||
| 113 | 0 | $content =~ /(table width="980" border="0" align="center" .*form name="form2")/s; | ||||||||||||
| 114 | 0 | $content = Text::Iconv->new("utf-8", "big5")->convert($1); | ||||||||||||
| 115 | ||||||||||||||
| 116 | 0 | $template = < | ||||||||||||
| 117 | | |||||||||||||
| 118 | [% title %][% ... %] | |||||||||||||
| 119 | 作 者:[% ... %]>[% author %][% ... %] | |||||||||||||
| 120 | 出版社:[% ... %]>[% publisher %][% ... %] | |||||||||||||
| 121 | ISBN:[% isbn %] [% ... %] |
|||||||||||||
| 122 | 出版日:[% pubdate %] | [% ... %]|||||||||||||
| 123 | 定 價:[% price_list %] 元 [% ... %] |
|||||||||||||
| 124 | 特 價:<[% ... %] [% price_sell %]元 | |||||||||||||
| 125 | END | |||||||||||||
| 126 | ||||||||||||||
| 127 | 0 | $data = $extract->extract($template, $content); | ||||||||||||
| 128 | ||||||||||||||
| 129 | 0 | 0 | return $self->handler("Could not extract data from TWKingstone result page.") | |||||||||||
| 130 | unless(defined $data); | |||||||||||||
| 131 | ||||||||||||||
| 132 | 0 | $data->{pubdate} =~ s/[ \n\r\t]*//g; | ||||||||||||
| 133 | ||||||||||||||
| 134 | 0 | my $bk = { | ||||||||||||
| 135 | 'isbn' => $data->{isbn}, | |||||||||||||
| 136 | 'title' => $data->{title}, | |||||||||||||
| 137 | 'author' => $data->{author}, | |||||||||||||
| 138 | 'book_link' => $book_link, | |||||||||||||
| 139 | 'image_link' => "http://www.kingstone.com.tw".$data->{image_link}, | |||||||||||||
| 140 | 'pubdate' => $data->{pubdate}, | |||||||||||||
| 141 | 'publisher' => $data->{publisher}, | |||||||||||||
| 142 | 'price_list' => $data->{price_list}, | |||||||||||||
| 143 | 'price_sell' => $data->{price_sell}, | |||||||||||||
| 144 | }; | |||||||||||||
| 145 | ||||||||||||||
| 146 | 0 | $self->book($bk); | ||||||||||||
| 147 | 0 | $self->found(1); | ||||||||||||
| 148 | 0 | return $self->book; | ||||||||||||
| 149 | } | |||||||||||||
| 150 | ||||||||||||||
| 151 | 1; | |||||||||||||
| 152 | __END__ |