ignore ssl certificate errors
[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         RPN => {
44                 And => '&bool[]=AND&',
45                 Or  => '&bool[]=OR&',
46         },
47         prefix_term => sub {
48                 my ( $prefix, $term ) = @_;
49                 return 'type[]=' . $prefix . '&lookfor[]=' . $term;
50         }
51 }};
52
53 sub search {
54         my ( $self, $query ) = @_;
55
56         die "need query" unless defined $query;
57
58         # http://catalog.hathitrust.org/Search/Home?lookfor=croatia%20AND%20zagreb&type=title
59         my $url = 'http://catalog.hathitrust.org/Search/Home?' . $query;
60
61 diag "get $url";
62
63         $self->mech->get( $url );
64
65         my $hits = 0;
66
67         if ( $self->mech->content =~ m{of\s*<span class="strong">(\d+)</span>\s*Results for}s ) {
68                 $hits = $1;
69         } else {
70                 diag "get't find results in ", $self->mech->content;
71                 return;
72         }
73
74 diag "got $hits results";
75
76         $self->populate_records;
77
78         return $self->{hits} = $hits;
79 }
80
81 sub populate_records {
82         my ($self) = @_;
83
84         foreach my $link ( $self->mech->find_all_links( url_regex => qr{/Record/\d+} ) ) {
85                 my $url = $link->url;
86                 push @{ $self->{records} }, $url;
87                 warn "## ++ $url\n";
88         }
89 }
90
91 sub next_marc {
92         my ($self,$format) = @_;
93
94         $format ||= 'marc';
95
96         my $url = shift @{ $self->{records} };
97
98         if ( ! $url ) {
99                 diag "fetch next page";
100                 $self->save_content;
101                 $self->mech->follow_link( text_regex => qr/Next/ );
102                 $self->populate_records;
103                 $url = shift @{ $self->{records} };
104                 if ( ! $url ) {
105                         warn "ERROR no more results\n";
106                         return;
107                 }
108         }
109
110         my $id = $1 if $url =~ m{Record/(\d+)};
111
112         $self->mech->get( $url . '.mrc' );
113
114         my $marc = decode('utf-8', $self->mech->content );
115
116         $self->save_marc( "$id.marc", $marc );
117
118         $self->mech->back; # return to search results for next page
119
120         return $id;
121
122 }
123
124 1;