| 1 | # ex:ts=8 | ||
| 2 | |||
| 3 | package WWW::Scraper::ISBN::TWSanmin_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 | $VERSION = '0.02'; | ||
| 10 | |||
| 11 | #-------------------------------------------------------------------------- | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | WWW::Scraper::ISBN::TWSanmin_Driver - Search driver for TWSanmin'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 TWSanmin'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 | 0 | ▶ | use Text::Iconv; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 37 | |||
| 38 | ########################################################################### | ||
| 39 | #Constants # | ||
| 40 | ########################################################################### | ||
| 41 | |||
| 42 | use constant QUERY => 'http://www.sanmin.com.tw/page-qsearch.asp?ct=search_isbn1&qu=%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<search()> | ||
| 61 | |||
| 62 | Creates a query string, then passes the appropriate form fields to the Sanmin | ||
| 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 Sanmin website. | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub search { | ||
| 86 | my $self = shift; | ||
| 87 | my $isbn = shift; | ||
| 88 | $self->found(0); | ||
| 89 | $self->book(undef); | ||
| 90 | |||
| 91 | my $url = sprintf(QUERY, $isbn); | ||
| 92 | my $mechanize = WWW::Mechanize->new(); | ||
| 93 | $mechanize->get($url); | ||
| 94 | return undef unless($mechanize->success()); | ||
| 95 | |||
| 96 | my $conv = Text::Iconv->new("utf-8", "big5"); | ||
| 97 | my $content = $mechanize->content(); | ||
| 98 | $content =~ /(table width="98%" align="center" bgcolor=#99CCFF.*-PRICE-)/s; | ||
| 99 | $content = $conv->convert($1); | ||
| 100 | |||
| 101 | my $template = <<END; | ||
| 102 | ALT="[% title %]">[% ... %] | ||
| 103 | <img src="[% image_link %]"[% ... %] | ||
| 104 | I S B N[% ... %]<B>[% isbn %]</B>[% ... %] | ||
| 105 | §@¡@ªÌ[% ... %]<a href[% ... %]>[% author %]</a>[% ... %] | ||
| 106 | ¥Xª©ªÀ[% ... %]<td width="58%">[% publisher %] </td>[% ... %] | ||
| 107 | ¥Xª©¤é[% ... %]<td>[% pubdate %]</td>[% ... %] | ||
| 108 | ì¡@»ù[% ... %]<td>[% price_list %]¤¸</td>[% ... %] | ||
| 109 | ¯S¡@»ù[% ... %]<font color="#FF0000">[% price_sell %]< | ||
| 110 | END | ||
| 111 | |||
| 112 | my $extract = Template::Extract->new; | ||
| 113 | my $data = $extract->extract($template, $content); | ||
| 114 | |||
| 115 | return $self->handler("Could not extract data from TWSanmin result page.") | ||
| 116 | unless(defined $data); | ||
| 117 | |||
| 118 | $data->{title} =~ s/(.*)(¡Ð.*\d+) *$/$1/; | ||
| 119 | $data->{pubdate} =~ s/[ \n\r\t]+//g; | ||
| 120 | $data->{author} = join('', map { $conv->convert(chr($_)) if ($_ =~ /\d+/) } split(/[&#;]/, $data->{author})); | ||
| 121 | $data->{publisher} = join('', map { $conv->convert(chr($_)) if ($_ =~ /\d+/) } split(/[&#;]/, $data->{publisher})); | ||
| 122 | |||
| 123 | my $bk = { | ||
| 124 | 'isbn' => $data->{isbn}, | ||
| 125 | 'title' => $data->{title}, | ||
| 126 | 'author' => $data->{author}, | ||
| 127 | 'book_link' => $mechanize->uri()->as_string, | ||
| 128 | 'image_link' => "http://www.sanmin.com.tw/".$data->{image_link}, | ||
| 129 | 'pubdate' => $data->{pubdate}, | ||
| 130 | 'publisher' => $data->{publisher}, | ||
| 131 | 'price_list' => $data->{price_list}, | ||
| 132 | 'price_sell' => $data->{price_sell}, | ||
| 133 | }; | ||
| 134 | |||
| 135 | $self->book($bk); | ||
| 136 | $self->found(1); | ||
| 137 | return $self->book; | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||
| 141 | __END__ | ||