File Coverage

blib/lib/WWW/Search/ValentinskaCZ.pm
Criterion Covered Total %
statement 59 65 90.7
branch 2 6 33.3
condition n/a
subroutine 13 13 100.0
pod n/a
total 74 84 88.1


line stmt bran cond sub pod time code
1             package WWW::Search::ValentinskaCZ;
2              
3 4     4   1095694 use base qw(WWW::Search);
  4         9  
  4         2169  
4 4     4   250120 use strict;
  4         14  
  4         129  
5 4     4   25 use warnings;
  4         8  
  4         318  
6              
7 4     4   1125 use Encode qw(decode_utf8);
  4         27454  
  4         561  
8 4     4   28 use LWP::UserAgent;
  4         12  
  4         145  
9 4     4   2253 use Perl6::Slurp qw(slurp);
  4         7401  
  4         24  
10 4     4   1765 use Readonly;
  4         9791  
  4         250  
11 4     4   24 use URI;
  4         12  
  4         116  
12 4     4   1972 use Web::Scraper;
  4         165199  
  4         33  
13              
14             # Constants.
15             Readonly::Scalar our $MAINTAINER => 'Michal Josef Spacek ';
16             Readonly::Scalar my $VALENTINSKA_CZ => 'http://www.valentinska.cz/';
17             Readonly::Scalar my $VALENTINSKA_CZ_ACTION1 => 'index.php?route=product/search&search=';
18              
19             our $VERSION = 0.06;
20              
21             # Setup.
22             sub _native_setup_search {
23 1     1   332 my ($self, $query) = @_;
24              
25             $self->{'_def'} = scraper {
26              
27             # Get list of books.
28             process '//div[@class="product-inner clearfix"]',
29             'books[]' => scraper {
30              
31 16         431759 process '//div[@class="wrap-infor"]/div[@class="name"]/a',
32             'title' => 'TEXT';
33             process '//div[@class="wrap-infor"]/div[@class="name"]/a',
34             'url' => ['@href', sub {
35 16         144032 my $url = shift;
36 16         185 my $uri = URI->new($url);
37 16         15186 my %query = $uri->query_form;
38 16 50       1294 if (exists $query{'search'}) {
39 16         65 delete $query{'search'};
40             }
41 16         73 $uri->query_form(\%query);
42 16         953 return $uri->as_string;
43 16         155956 }];
44 16         729 process '//div[@class="image"]/a/img',
45             'image' => '@src';
46 16         115190 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         136470 'price' => ['TEXT', sub { s/^\s*(.*?)\s*$/$1/; }];
  16         171258  
50 16         508 return;
51 1     1   584438 };
52 1         320 return;
53 1         9 };
54 1         37 $self->{'_query'} = $query;
55              
56 1         8 return 1;
57             }
58              
59             # Get data.
60             sub _native_retrieve_some {
61 1     1   60 my $self = shift;
62              
63 1 50       11 if (defined $self->{search_from_file}) {
64 1         7 my $content = slurp($self->{search_from_file});
65 1         882 $self->_process_content($content);
66             } else {
67             # Query.
68 0         0 my $query = decode_utf8($self->{'_query'});
69              
70             # Get content.
71 0         0 my $ua = LWP::UserAgent->new(
72             'agent' => "WWW::Search::ValentinskaCZ/$VERSION",
73             );
74 0         0 my $query_url = $VALENTINSKA_CZ.$VALENTINSKA_CZ_ACTION1.$query;
75 0         0 my $response = $ua->get($query_url);
76              
77             # Process.
78 0 0       0 if ($response->is_success) {
79 0         0 $self->_process_content($response->content);
80             }
81             }
82              
83 1         6 return;
84             }
85              
86             sub _process_content {
87 1     1   27 my ($self, $content) = @_;
88              
89             # Get books structure.
90 1         8 my $books_hr = $self->{'_def'}->scrape($content);
91              
92             # Process each book.
93 1         12137 foreach my $book_hr (@{$books_hr->{'books'}}) {
  1         6  
94 16         25 push @{$self->{'cache'}}, $book_hr;
  16         40  
95             }
96              
97 1         6 return;
98             }
99              
100             1;
101              
102             __END__