File Coverage

blib/lib/WWW/Scraper/ISBN/LibUniIt_Driver.pm
Criterion Covered Total %
statement 15 62 24.1
branch 0 22 0.0
condition n/a
subroutine 5 7 71.4
pod 1 2 50.0
total 21 93 22.5


|){
line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN::LibUniIt_Driver;
2              
3 1     1   27087 use strict;
  1         2  
  1         40  
4 1     1   5 use warnings;
  1         2  
  1         30  
5 1     1   1157 use LWP::UserAgent;
  1         105142  
  1         34  
6 1     1   1339 use WWW::Scraper::ISBN::Driver;
  1         1190  
  1         33  
7 1     1   944 use HTML::Entities qw(decode_entities);
  1         8495  
  1         1023  
8              
9             our @ISA = qw(WWW::Scraper::ISBN::Driver);
10              
11             our $VERSION = '0.2';
12            
13             sub search {
14 0     0 1   my $self = shift;
15 0           my $isbn = shift;
16 0           $self->found(0);
17 0           $self->book(undef);
18            
19 0           my $post_url = 'http://www.libreriauniversitaria.it/c_power_search.php?shelf=BIT&q=' . $isbn . '&submit=Invia';
20 0           my $ua = new LWP::UserAgent;
21 0           my $res = $ua->get($post_url);
22 0           my $doc = $res->as_string;
23            
24 0           my $title = "";
25 0           my $authors = "";
26 0           my $editor = "";
27 0           my $date = "";
28 0           my $price = "";
29            
30 0 0         if ($doc =~ /Nessun prodotto corrisponde ai criteri di ricerca/) {
    0          
    0          
31 0           $self->error("book not found.");
32 0           $self->found(0);
33 0           return 0;
34             } elsif ($doc =~ m|Ricerca - $isbn - libreriauniversitaria\.it|i){
35 0           my $info;
36 0 0         if ($doc =~ m|]+>]+ class="product_heading_title_link" [^>]+>([^<]+)(.*?)
37 0           $title = $1;
38 0           $info = $2;
39 0           $authors = parse_authors($info);
40 0 0         if ($info =~ m|]+ href="libri-editore[^"]+" [^>]+/>([^<]+) - (\d+)|){
41 0           $editor = $1;
42 0           $date = $2;
43             }
44             }
45 0 0         $price = $1 if ($doc =~ /Prezzo: .*?€ (\d+)/);
46             } elsif ($doc =~ /Dettagli del libro/){
47 0 0         $price = $1 if ($doc =~ m|€ ([^<]+)|);
48 0 0         $title = $1 if ($doc =~ m|Titolo: ([^>]+)|);
49 0 0         $authors = parse_authors($1) if ($doc =~ m|Autor[ei]:(.*?)
  • |);
  • 50 0 0         $editor = $1 if ($doc =~ m|Editore:.*?]+/>([^<]+)
  • |);
  • 51 0 0         $date = $1 if ($doc =~ m|Data di Pubblicazione:\s+(\d+)
  • |);
  • 52            
    53             } else {
    54 0           $self->error("liberiauniversitaria.it answered in an unattended way, book information cannot be found.");
    55 0           $self->found(0);
    56             };
    57              
    58 0           decode_entities($title);
    59 0           decode_entities($authors);
    60 0           decode_entities($editor);
    61 0           my $bk = {
    62             'isbn' => $isbn,
    63             'author' => $authors,
    64             'title' => $title,
    65             'editor' => $editor,
    66             'date' => $date,
    67             'price' => $price,
    68             };
    69 0           $self->book($bk);
    70 0           $self->found(1);
    71 0           return $bk;
    72             }
    73              
    74             sub parse_authors {
    75 0     0 0   my $info = shift;
    76 0           my $sep = "";
    77 0           my $authors;
    78 0           while ($info =~ s|]+>([^<]+)||){
    79 0           $authors .= $sep . $1;
    80 0           $sep = ", ";
    81             }
    82 0           return $authors;
    83             }
    84              
    85             1;
    86             __END__