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