| 1 | package WWW::Search::KacurCZ; | ||||||||||
| 2 | |||||||||||
| 3 | # Pragmas. | ||||||||||
| 4 | 3 | ▶ | use base qw(WWW::Search); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # Modules. | ||||||||||
| 9 | 3 | ▶ | use Encode qw(decode_utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Text::Iconv; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use Web::Scraper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | # Constants. | ||||||||||
| 16 | 3 | Readonly::Scalar our $MAINTAINER => 'Michal Spacek <skim@cpan.org>'; | |||||||||
| 17 | 3 | Readonly::Scalar my $KACUR_CZ => 'http://kacur.cz/'; | |||||||||
| 18 | 3 | Readonly::Scalar my $KACUR_CZ_ACTION1 => '/search.asp?doIt=search&menu=675&'. | |||||||||
| 19 | 'kategorie=&nazev=&rok=&dosearch=Vyhledat'; | ||||||||||
| 20 | |||||||||||
| 21 | # Version. | ||||||||||
| 22 | 3 | our $VERSION = 0.01; | |||||||||
| 23 | |||||||||||
| 24 | # Setup. | ||||||||||
| 25 | sub native_setup_search { | ||||||||||
| 26 | 0 | ▶ | my ($self, $query) = @_; | ||||||||
|
Subroutine
native_setup_search: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 27 | $self->{'_def'} = scraper { | ||||||||||
| 28 | process '//div[@class="productItemX"]', 'books[]' => scraper { | ||||||||||
| 29 | 0 | ▶ | process '//div/h3/a', 'title' => 'TEXT'; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 30 | 0 | process '//div/h3/a', 'url' => '@href'; | |||||||||
| 31 | 0 | process '//img', 'cover_url' => '@src'; | |||||||||
| 32 | 0 | process '//p', 'author_publisher[]' => 'TEXT'; | |||||||||
| 33 | 0 | process '//span[@class="price"]', 'price' => 'TEXT'; | |||||||||
| 34 | 0 | return; | |||||||||
| 35 | 0 | ▶ | }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 36 | 0 | return; | |||||||||
| 37 | 0 | }; | |||||||||
| 38 | 0 | $self->{'_query'} = $query; | |||||||||
| 39 | 0 | return 1; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | # Get data. | ||||||||||
| 43 | sub native_retrieve_some { | ||||||||||
| 44 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
native_retrieve_some: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 45 | |||||||||||
| 46 | # Query. | ||||||||||
| 47 | 0 | my $i1 = Text::Iconv->new('utf-8', 'windows-1250'); | |||||||||
| 48 | 0 | my $query = $i1->convert(decode_utf8($self->{'_query'})); | |||||||||
| 49 | |||||||||||
| 50 | # Get content. | ||||||||||
| 51 | 0 | my $ua = LWP::UserAgent->new( | |||||||||
| 52 | 'agent' => "WWW::Search::KacurCZ/$VERSION", | ||||||||||
| 53 | ); | ||||||||||
| 54 | 0 | my $response = $ua->get($KACUR_CZ.$KACUR_CZ_ACTION1."&autor=$query"); | |||||||||
| 55 | |||||||||||
| 56 | # Process. | ||||||||||
| 57 | 0 | ▶ | if ($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | my $i2 = Text::Iconv->new('windows-1250', 'utf-8'); | |||||||||
| 59 | 0 | my $content = $i2->convert($response->content); | |||||||||
| 60 | |||||||||||
| 61 | # Get books structure. | ||||||||||
| 62 | 0 | my $books_hr = $self->{'_def'}->scrape($content); | |||||||||
| 63 | |||||||||||
| 64 | # Process each book. | ||||||||||
| 65 | 0 | foreach my $book_hr (@{$books_hr->{'books'}}) { | |||||||||
| 66 | 0 | _fix_url($book_hr, 'url'); | |||||||||
| 67 | 0 | _fix_url($book_hr, 'cover_url'); | |||||||||
| 68 | $book_hr->{'author'} | ||||||||||
| 69 | 0 | = $book_hr->{'author_publisher'}->[0]; | |||||||||
| 70 | 0 | $book_hr->{'author'} =~ s/\N{U+00A0}$//ms; | |||||||||
| 71 | $book_hr->{'publisher'} | ||||||||||
| 72 | 0 | = $book_hr->{'author_publisher'}->[1]; | |||||||||
| 73 | 0 | $book_hr->{'publisher'} =~ s/\N{U+00A0}$//ms; | |||||||||
| 74 | 0 | delete $book_hr->{'author_publisher'}; | |||||||||
| 75 | ($book_hr->{'old_price'}, $book_hr->{'price'}) | ||||||||||
| 76 | 0 | = split m/\s*\*\s*/ms, $book_hr->{'price'}; | |||||||||
| 77 | 0 | push @{$self->{'cache'}}, $book_hr; | |||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | return; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # Fix URL to absolute path. | ||||||||||
| 85 | sub _fix_url { | ||||||||||
| 86 | 0 | ▶ | my ($book_hr, $url) = @_; | ||||||||
|
Subroutine
_fix_url: not called CC=2 SCAR=17.9
| |||||||||||
| 87 | 0 | ▶ | if (exists $book_hr->{$url}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | $book_hr->{$url} = $KACUR_CZ.$book_hr->{$url}; | |||||||||
| 89 | } | ||||||||||
| 90 | 0 | return; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 3 | 1; | |||||||||
| 94 | |||||||||||
| 95 | __END__ | ||||||||||