| 1 | package WWW::Scraper::ISBN::LOC_Driver; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 5 | our $VERSION = '0.25'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | ########################################################################### | ||||||||||||||||||||||||||||||||
| 11 | # Inheritence | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 5 | our @ISA = qw(WWW::Scraper::ISBN::Driver); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | ########################################################################### | ||||||||||||||||||||||||||||||||
| 16 | # Modules | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use WWW::Scraper::ISBN::Driver; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | ########################################################################### | ||||||||||||||||||||||||||||||||
| 23 | # Public Interface | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | sub search { | ||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 27 | 0 | my $isbn = shift; | |||||||||||||||||||||||||||||||
| 28 | 0 | my %data; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 0 | $self->found(0); | |||||||||||||||||||||||||||||||
| 31 | 0 | $self->book(undef); | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | # first, initialize the session: | ||||||||||||||||||||||||||||||||
| 34 | 0 | my $post_url = "http://www.loc.gov/cgi-bin/zgate?ACTION=INIT&FORM_HOST_PORT=/prod/www/data/z3950/locils.html,z3950.loc.gov,7090"; | |||||||||||||||||||||||||||||||
| 35 | 0 | my $ua = new LWP::UserAgent; | |||||||||||||||||||||||||||||||
| 36 | 0 | my $res = $ua->request(GET $post_url); | |||||||||||||||||||||||||||||||
| 37 | 0 | my $doc = ''; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # get page | ||||||||||||||||||||||||||||||||
| 40 | # removes blank lines, DOS line feeds, and leading spaces. | ||||||||||||||||||||||||||||||||
| 41 | 0 | $doc = join "\n", grep { /\S/ } split /\n/, $res->as_string(); | |||||||||||||||||||||||||||||||
| 42 | 0 | $doc =~ s/\r//g; | |||||||||||||||||||||||||||||||
| 43 | 0 | $doc =~ s/^\s+//g; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 0 | my $sessionID = ''; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | unless ( ($sessionID) = ($doc =~ /<INPUT NAME="SESSION_ID" VALUE="([^"]*)" TYPE="HIDDEN">/i) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | print "Error starting LOC Query session.\n" if $self->verbosity; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 0 | $self->error("Cannot start LOC query session.\n"); | |||||||||||||||||||||||||||||||
| 50 | 0 | $self->found(0); | |||||||||||||||||||||||||||||||
| 51 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | $post_url = "http://www.loc.gov/cgi-bin/zgate"; | |||||||||||||||||||||||||||||||
| 55 | 0 | $res = $ua->request( | |||||||||||||||||||||||||||||||
| 56 | POST $post_url, | ||||||||||||||||||||||||||||||||
| 57 | Referer => $post_url, | ||||||||||||||||||||||||||||||||
| 58 | Content => [ | ||||||||||||||||||||||||||||||||
| 59 | TERM_1 => $isbn, | ||||||||||||||||||||||||||||||||
| 60 | USE_1 => '7', | ||||||||||||||||||||||||||||||||
| 61 | ESNAME => 'F', | ||||||||||||||||||||||||||||||||
| 62 | ACTION => 'SEARCH', | ||||||||||||||||||||||||||||||||
| 63 | DBNAME => 'VOYAGER', | ||||||||||||||||||||||||||||||||
| 64 | MAXRECORDS => '20', | ||||||||||||||||||||||||||||||||
| 65 | RECSYNTAX => '1.2.840.10003.5.10', | ||||||||||||||||||||||||||||||||
| 66 | STRUCT_1 => '1', | ||||||||||||||||||||||||||||||||
| 67 | STRUCT_2 => '1', | ||||||||||||||||||||||||||||||||
| 68 | STRUCT_3 => '1', | ||||||||||||||||||||||||||||||||
| 69 | SESSION_ID => $sessionID | ||||||||||||||||||||||||||||||||
| 70 | ] | ||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | $doc = ''; | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | # get page | ||||||||||||||||||||||||||||||||
| 76 | # removes blank lines, DOS line feeds, and leading spaces. | ||||||||||||||||||||||||||||||||
| 77 | 0 | $doc = join "\n", grep { /\S/ } split /\n/, $res->as_string(); | |||||||||||||||||||||||||||||||
| 78 | 0 | $doc =~ s/\r//g; | |||||||||||||||||||||||||||||||
| 79 | 0 | $doc =~ s/^\s+//g; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( (my $book_data) = ($doc =~ /.*<PRE>(.*)<\/PRE>.*/is) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | print $book_data if ($self->verbosity > 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 0 | my @author_lines; | |||||||||||||||||||||||||||||||
| 85 | my $other_authors; | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | # get author field | ||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | while ($book_data =~ s/uthor(s)?:\s+(\D+?)(?:, [0-9-.]*|\.)$/if (($1) && ($1 eq "s")) { "uthors:"; } else { "" }/me) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$1 and $1 eq "s"
MC/DC0%0/2
$1 and $1 eq "s"
$1
$1 eq "s"
Truth table0%0/3
A and B
A= $1B= $1 eq "s"
| |||||||||||||||||||||||||||||||||
| 89 | 0 | my $temp = $2; | |||||||||||||||||||||||||||||||
| 90 | 0 | $temp =~ s/ ([A-Z])$/ $1./; # trailing middle initial | |||||||||||||||||||||||||||||||
| 91 | 0 | push @author_lines, $temp; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 0 | @author_lines = sort @author_lines; | |||||||||||||||||||||||||||||||
| 95 | 0 | foreach my $line(@author_lines) { | |||||||||||||||||||||||||||||||
| 96 | 0 | $line =~ s/(\w+), (.*)/$2 $1/; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | 0 | $data{author} = join ", ", @author_lines; | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | # get other fields | ||||||||||||||||||||||||||||||||
| 101 | 0 | ($data{title}) = $book_data =~ /Title:\s+((.*)\n(\s+(.*)\n)*)/; | |||||||||||||||||||||||||||||||
| 102 | 0 | ($data{edition}) = $book_data =~ /Edition:\s+(.*)\n/; | |||||||||||||||||||||||||||||||
| 103 | 0 | ($data{volume}) = $book_data =~ /Volume:\s+(.*)\n/; | |||||||||||||||||||||||||||||||
| 104 | 0 | ($data{dewey}) = $book_data =~ /Dewey No.:\s+(.*)\n/; | |||||||||||||||||||||||||||||||
| 105 | 0 | ($data{publisher},$data{pubdate}) = $book_data =~ /Published:\s+[^:]+:\s+(.*), c(\d+)\.\n/; | |||||||||||||||||||||||||||||||
| 106 | 0 | ($data{pages},$data{height}) = $book_data =~ /Description:\s+\w+,\s+(\d+)\s+:[^;]+;\s+(\d+)\s*cm.\n/; | |||||||||||||||||||||||||||||||
| 107 | 0 | ($data{isbn10},$data{binding}) = $book_data =~ /ISBN:\s+(\d+)\s+\(([^\)]+)\)\n/; | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # trim and clean data | ||||||||||||||||||||||||||||||||
| 110 | 0 | for my $key (keys %data) { | |||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | next unless($data{$key}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 0 | $data{$key} =~ s/\n//g; | |||||||||||||||||||||||||||||||
| 113 | 0 | $data{$key} =~ s/ +/ /g; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # reformat and default fields | ||||||||||||||||||||||||||||||||
| 117 | 0 | $data{title} =~ s/(.*) \/(.*)/$1/; | |||||||||||||||||||||||||||||||
| 118 | 0 | $data{height} *= 10; # cm => mm | |||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $data{author} ||= ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$data{"author"} ||= ""
MC/DC0%0/1
$data{"author"} ||= ""
$data{"author"}
Truth table0%0/2
A
A= $data{"author"}
| |||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $data{edition} ||= 'n/a'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$data{"edition"} ||= "n/a"
MC/DC0%0/1
$data{"edition"} ||= "n/a"
$data{"edition"}
Truth table0%0/2
A
A= $data{"edition"}
| |||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $data{volume} ||= 'n/a'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$data{"volume"} ||= "n/a"
MC/DC0%0/1
$data{"volume"} ||= "n/a"
$data{"volume"}
Truth table0%0/2
A
A= $data{"volume"}
| |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | # print data if in verbose mode | ||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if($self->verbosity > 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 0 | for my $key (keys %data) { | |||||||||||||||||||||||||||||||
| 126 | 0 | printf "%-8s %s\n", "$key:", $data{$key}; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | # store book data | ||||||||||||||||||||||||||||||||
| 131 | 0 | my $bk = { | |||||||||||||||||||||||||||||||
| 132 | 'isbn' => $isbn, | ||||||||||||||||||||||||||||||||
| 133 | 'isbn13' => $isbn, | ||||||||||||||||||||||||||||||||
| 134 | 'ean13' => $isbn | ||||||||||||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $bk->{isbn10} = $data{isbn10} if(length($data{isbn10}) == 10); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 0 | $bk->{$_} = $data{$_} for(qw(author title edition volume dewey publisher pubdate pages height binding)); | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 0 | $self->book($bk); | |||||||||||||||||||||||||||||||
| 141 | 0 | $self->found(1); | |||||||||||||||||||||||||||||||
| 142 | 0 | return $self->book; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | print "Error extracting data from LOC result page.\n" if $self->verbosity; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 0 | $self->error("Could not extract data from LOC result page.\n"); | |||||||||||||||||||||||||||||||
| 147 | 0 | $self->found(0); | |||||||||||||||||||||||||||||||
| 148 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 5 | 1; | |||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||||||