| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Scraper::ISBN::LOC_Driver; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 202714 | use strict; | 
|  | 5 |  |  |  |  | 14 |  | 
|  | 5 |  |  |  |  | 211 |  | 
| 4 | 5 |  |  | 5 |  | 101 | use warnings; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 340 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.25'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | #-------------------------------------------------------------------------- | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | ########################################################################### | 
| 11 |  |  |  |  |  |  | # Inheritence | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our @ISA = qw(WWW::Scraper::ISBN::Driver); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | ########################################################################### | 
| 16 |  |  |  |  |  |  | # Modules | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 5 |  |  | 5 |  | 7731 | use HTTP::Request::Common; | 
|  | 5 |  |  |  |  | 244103 |  | 
|  | 5 |  |  |  |  | 520 |  | 
| 19 | 5 |  |  | 5 |  | 6522 | use LWP::UserAgent; | 
|  | 5 |  |  |  |  | 153794 |  | 
|  | 5 |  |  |  |  | 194 |  | 
| 20 | 5 |  |  | 5 |  | 5967 | use WWW::Scraper::ISBN::Driver; | 
|  | 5 |  |  |  |  | 6100 |  | 
|  | 5 |  |  |  |  | 6194 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | ########################################################################### | 
| 23 |  |  |  |  |  |  | # Public Interface | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub search { | 
| 26 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 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(); | 
|  | 0 |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  |  | $doc =~ s/\r//g; | 
| 43 | 0 |  |  |  |  |  | $doc =~ s/^\s+//g; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | my $sessionID = ''; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 | 0 |  |  |  |  | unless ( ($sessionID) = ($doc =~ //i) ) { | 
| 48 | 0 | 0 |  |  |  |  | print "Error starting LOC Query session.\n" if $self->verbosity; | 
| 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(); | 
|  | 0 |  |  |  |  |  |  | 
| 78 | 0 |  |  |  |  |  | $doc =~ s/\r//g; | 
| 79 | 0 |  |  |  |  |  | $doc =~ s/^\s+//g; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 | 0 |  |  |  |  | if ( (my $book_data) = ($doc =~ /.* (.*)<\/PRE>.*/is) ) {  | 
| 82 | 0 | 0 |  |  |  |  | print $book_data if ($self->verbosity > 1); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | my @author_lines; | 
| 85 |  |  |  |  |  |  | my $other_authors; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # get author field | 
| 88 | 0 | 0 | 0 |  |  |  | while ($book_data =~ s/uthor(s)?:\s+(\D+?)(?:, [0-9-.]*|\.)$/if (($1) && ($1 eq "s")) { "uthors:"; } else { "" }/me) { | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 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 | 0 |  |  |  |  | next    unless($data{$key}); | 
| 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 |  | 0 |  |  |  | $data{author}  ||= ''; | 
| 120 | 0 |  | 0 |  |  |  | $data{edition} ||= 'n/a'; | 
| 121 | 0 |  | 0 |  |  |  | $data{volume}  ||= 'n/a'; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | # print data if in verbose mode | 
| 124 | 0 | 0 |  |  |  |  | if($self->verbosity > 1) { | 
| 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 | 0 |  |  |  |  | $bk->{isbn10} = $data{isbn10} if(length($data{isbn10}) == 10); | 
| 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 | 0 |  |  |  |  | print "Error extracting data from LOC result page.\n" if $self->verbosity; | 
| 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 |  |  |  |  |  |  | 1; | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | __END__ |