| 1 | # ex:ts=8 | ||||||||||
| 2 | |||||||||||
| 3 | package WWW::Scraper::ISBN::TWSrbook_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.01'; | |||||||||
| 10 | |||||||||||
| 11 | #-------------------------------------------------------------------------- | ||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | WWW::Scraper::ISBN::TWSrbook_Driver - Search driver for TWSrbook'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 TWSrbook'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 | |||||||||||
| 37 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | ########################################################################### | ||||||||||
| 40 | #Constants # | ||||||||||
| 41 | ########################################################################### | ||||||||||
| 42 | |||||||||||
| 43 | 1 | ▶ | use constant QUERY => 'http://www.srbook.com.tw/web/showbook.dox?id=%s'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | #-------------------------------------------------------------------------- | ||||||||||
| 46 | |||||||||||
| 47 | ########################################################################### | ||||||||||
| 48 | #Inheritence # | ||||||||||
| 49 | ########################################################################### | ||||||||||
| 50 | |||||||||||
| 51 | 1 | @ISA = qw(WWW::Scraper::ISBN::Driver); | |||||||||
| 52 | |||||||||||
| 53 | ########################################################################### | ||||||||||
| 54 | #Interface Functions # | ||||||||||
| 55 | ########################################################################### | ||||||||||
| 56 | |||||||||||
| 57 | =head1 METHODS | ||||||||||
| 58 | |||||||||||
| 59 | =over 4 | ||||||||||
| 60 | |||||||||||
| 61 | =item C<search()> | ||||||||||
| 62 | |||||||||||
| 63 | Creates a query string, then passes the appropriate form fields to the Srbook | ||||||||||
| 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 Books website. | ||||||||||
| 82 | |||||||||||
| 83 | =back | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 1 | ▶ | sub search { | ||||||||
|
Subroutine
search: called CC=3 SCAR=17.3
Pod
documented
| |||||||||||
| 88 | 1 | my $self = shift; | |||||||||
| 89 | 1 | my $isbn = shift; | |||||||||
| 90 | 1 | $self->found(0); | |||||||||
| 91 | 1 | $self->book(undef); | |||||||||
| 92 | |||||||||||
| 93 | 1 | my $url = sprintf(QUERY, $isbn); | |||||||||
| 94 | 1 | my $mechanize = WWW::Mechanize->new(); | |||||||||
| 95 | 1 | $mechanize->get( $url ); | |||||||||
| 96 | 0 | ▶ | return undef unless($mechanize->success()); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | # The Search Results page | ||||||||||
| 99 | 0 | my $template = <<END; | |||||||||
| 100 | <STRONG>[% title %]</STRONG>[% ... %] | ||||||||||
| 101 | §@ªÌ¡G[% ... %]<FONT size=2> [% author %]µÛ</FONT>[% ... %] | ||||||||||
| 102 | ISBN½X¡G</FONT><FONT size=2> [% isbn %]</FONT>[% ... %] | ||||||||||
| 103 | ¥Xª©ªÀ¡G[% ... %]<FONT size=2> [% publisher %]</FONT>[% ... %] | ||||||||||
| 104 | ¶¼Æ¡G[% ... %]<FONT size=2> [% pages %]</FONT>[% ... %] | ||||||||||
| 105 | ©w»ù¡G[% ... %]<FONT size=2> [% price_list %]</FONT>[% ... %] | ||||||||||
| 106 | ¯S»ù¡G[% ... %]<FONT size=4 color=red>[% price_sell %]</FONT>[% ... %] | ||||||||||
| 107 | ¥Xª©¦~¤ë¡G[% ... %]<FONT size=2> [% pubdate %] </FONT> | ||||||||||
| 108 | END | ||||||||||
| 109 | |||||||||||
| 110 | 0 | my $extract = Template::Extract->new; | |||||||||
| 111 | 0 | my $data = $extract->extract($template, $mechanize->content()); | |||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | return $self->handler("Could not extract data from TWSrbook result page.") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | unless(defined $data); | ||||||||||
| 115 | |||||||||||
| 116 | 0 | $data->{price_sell} =~ s/^\$\s*(\d+)\s*$/$1/; | |||||||||
| 117 | |||||||||||
| 118 | my $bk = { | ||||||||||
| 119 | 'isbn' => $data->{isbn}, | ||||||||||
| 120 | 'title' => $data->{title}, | ||||||||||
| 121 | 'author' => $data->{author}, | ||||||||||
| 122 | 'pages' => $data->{pages}, | ||||||||||
| 123 | 'book_link' => $mechanize->uri(), | ||||||||||
| 124 | 'image_link' => "http://www.srbook.com.tw/web/show_pic_s.dox?id=$isbn", | ||||||||||
| 125 | 'pubdate' => $data->{pubdate}, | ||||||||||
| 126 | 'publisher' => $data->{publisher}, | ||||||||||
| 127 | 'price_list' => $data->{price_list}, | ||||||||||
| 128 | 'price_sell' => $data->{price_sell}, | ||||||||||
| 129 | 0 | }; | |||||||||
| 130 | |||||||||||
| 131 | 0 | $self->book($bk); | |||||||||
| 132 | 0 | $self->found(1); | |||||||||
| 133 | 0 | return $self->book; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 1 | 1; | |||||||||
| 137 | __END__ | ||||||||||