ffdd987003b1320efefb5e14940c5efc55eb62c3
[Biblio-Z3950.git] / vuFind.pm
1 package vuFind;
2
3 use warnings;
4 use strict;
5
6 use MARC::Record;
7 use Data::Dump qw/dump/;
8 use JSON::XS;
9 use Encode;
10
11 use base 'Scraper';
12
13 my $debug = $ENV{DEBUG} || 0;
14
15 sub diag {
16         warn "# ", @_, $/;
17 }
18
19 # Koha Z39.50 query:
20 #
21 # Bib-1 @and @and @and @and @and @and @and @or
22 # @attr 1=4 title 
23 # @attr 1=7 isbn
24 # @attr 1=8 issn 
25 # @attr 1=1003 author 
26 # @attr 1=16 dewey 
27 # @attr 1=21 subject-holding 
28 # @attr 1=12 control-no 
29 # @attr 1=1007 standard-id 
30 # @attr 1=1016 any
31
32 sub usemap {{
33         4               => 'title',
34         7               => 'isn',
35         8               => 'isn',
36         1003    => 'author',
37 #       16              => '',
38         21              => 'subject',
39 #       12              => '',
40 #       1007    => '',
41         1016    => 'all',
42 }};
43
44 sub search {
45         my ( $self, $query ) = @_;
46
47         die "need query" unless defined $query;
48
49         # http://catalog.hathitrust.org/Search/Home?lookfor=croatia%20AND%20zagreb&type=title
50         my $url = 'http://catalog.hathitrust.org/Search/Home?lookfor=' . $query;
51
52 diag "get $url";
53
54         $self->mech->get( $url );
55
56         my $hits = 0;
57
58         if ( $self->mech->content =~ m{of\s*<span class="strong">(\d+)</span>\s*Results for}s ) {
59                 $hits = $1;
60         } else {
61                 diag "get't find results in ", $self->mech->content;
62                 return;
63         }
64
65 diag "got $hits results";
66
67         $self->populate_records;
68
69         return $self->{hits} = $hits;
70 }
71
72 sub populate_records {
73         my ($self) = @_;
74
75         foreach my $link ( $self->mech->find_all_links( url_regex => qr{/Record/\d+} ) ) {
76                 my $url = $link->url;
77                 push @{ $self->{records} }, $url;
78                 warn "## ++ $url\n";
79         }
80 }
81
82 sub next_marc {
83         my ($self,$format) = @_;
84
85         $format ||= 'marc';
86
87         my $url = shift @{ $self->{records} };
88
89         if ( ! $url ) {
90                 diag "fetch next page";
91                 $self->save_content;
92                 $self->mech->follow_link( text_regex => qr/Next/ );
93                 $self->populate_records;
94                 $url = shift @{ $self->{records} };
95                 if ( ! $url ) {
96                         warn "ERROR no more results\n";
97                         return;
98                 }
99         }
100
101         my $id = $1 if $url =~ m{Record/(\d+)};
102
103         $self->mech->get( $url . '.mrc' );
104
105         my $marc = decode('utf-8', $self->mech->content );
106
107         $self->save_marc( "$id.marc", $marc );
108
109         $self->mech->back; # return to search results for next page
110
111         return $id;
112
113 }
114
115 1;