| 1 | package WWW::Search::ValentinskaCZ; | ||||||||||
| 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 URI; | ||||||||
|
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 $VALENTINSKA_CZ => 'http://www.valentinska.cz/'; | |||||||||
| 17 | 4 | Readonly::Scalar my $VALENTINSKA_CZ_ACTION1 => 'index.php?route=product/search&search='; | |||||||||
| 18 | |||||||||||
| 19 | 4 | our $VERSION = 0.06; | |||||||||
| 20 | |||||||||||
| 21 | # Setup. | ||||||||||
| 22 | 1 | ▶ | sub _native_setup_search { | ||||||||
|
Subroutine
_native_setup_search: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | my ($self, $query) = @_; | |||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | $self->{'_def'} = scraper { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | # Get list of books. | ||||||||||
| 28 | process '//div[@class="product-inner clearfix"]', | ||||||||||
| 29 | 16 | ▶ | 'books[]' => scraper { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 16 | process '//div[@class="wrap-infor"]/div[@class="name"]/a', | |||||||||
| 32 | 'title' => 'TEXT'; | ||||||||||
| 33 | process '//div[@class="wrap-infor"]/div[@class="name"]/a', | ||||||||||
| 34 | 16 | ▶ | 'url' => ['@href', sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 35 | 16 | my $url = shift; | |||||||||
| 36 | 16 | my $uri = URI->new($url); | |||||||||
| 37 | 16 | my %query = $uri->query_form; | |||||||||
| 38 | 16 | ▶ | if (exists $query{'search'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 16 | delete $query{'search'}; | |||||||||
| 40 | } | ||||||||||
| 41 | 16 | $uri->query_form(\%query); | |||||||||
| 42 | 16 | return $uri->as_string; | |||||||||
| 43 | 16 | }]; | |||||||||
| 44 | 16 | process '//div[@class="image"]/a/img', | |||||||||
| 45 | 'image' => '@src'; | ||||||||||
| 46 | 16 | process '//div[@class="wrap-infor"]/div[@class="author"]', | |||||||||
| 47 | 'author' => 'TEXT'; | ||||||||||
| 48 | process '//div[@class="wrap-infor"]/div[@class="price"]/br/preceding-sibling::text()', | ||||||||||
| 49 | 16 | ▶ | 'price' => ['TEXT', sub { s/^\s*(.*?)\s*$/$1/; }]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 16 | return; | |||||||||
| 51 | 1 | }; | |||||||||
| 52 | 1 | return; | |||||||||
| 53 | 1 | }; | |||||||||
| 54 | 1 | $self->{'_query'} = $query; | |||||||||
| 55 | |||||||||||
| 56 | 1 | return 1; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | # Get data. | ||||||||||
| 60 | 1 | ▶ | sub _native_retrieve_some { | ||||||||
|
Subroutine
_native_retrieve_some: called CC=3 SCAR=16.0
| |||||||||||
| 61 | 1 | my $self = shift; | |||||||||
| 62 | |||||||||||
| 63 | 1 | ▶ | if (defined $self->{search_from_file}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | my $content = slurp($self->{search_from_file}); | |||||||||
| 65 | 1 | $self->_process_content($content); | |||||||||
| 66 | } else { | ||||||||||
| 67 | # Query. | ||||||||||
| 68 | 0 | my $query = decode_utf8($self->{'_query'}); | |||||||||
| 69 | |||||||||||
| 70 | # Get content. | ||||||||||
| 71 | 0 | my $ua = LWP::UserAgent->new( | |||||||||
| 72 | 'agent' => "WWW::Search::ValentinskaCZ/$VERSION", | ||||||||||
| 73 | ); | ||||||||||
| 74 | 0 | my $query_url = $VALENTINSKA_CZ.$VALENTINSKA_CZ_ACTION1.$query; | |||||||||
| 75 | 0 | my $response = $ua->get($query_url); | |||||||||
| 76 | |||||||||||
| 77 | # Process. | ||||||||||
| 78 | 0 | ▶ | if ($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | $self->_process_content($response->content); | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 1 | return; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 1 | ▶ | sub _process_content { | ||||||||
|
Subroutine
_process_content: called CC=2 SCAR=6.9
| |||||||||||
| 87 | 1 | my ($self, $content) = @_; | |||||||||
| 88 | |||||||||||
| 89 | # Get books structure. | ||||||||||
| 90 | 1 | my $books_hr = $self->{'_def'}->scrape($content); | |||||||||
| 91 | |||||||||||
| 92 | # Process each book. | ||||||||||
| 93 | 1 | foreach my $book_hr (@{$books_hr->{'books'}}) { | |||||||||
| 94 | 16 | push @{$self->{'cache'}}, $book_hr; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 1 | return; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 4 | 1; | |||||||||
| 101 | |||||||||||
| 102 | __END__ | ||||||||||