vuFind scraper which fetch marc records directly
[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
10 use base 'Scraper';
11
12 my $debug = $ENV{DEBUG} || 0;
13
14 sub diag {
15         warn "# ", @_, $/;
16 }
17
18 # Koha Z39.50 query:
19 #
20 # Bib-1 @and @and @and @and @and @and @and @or
21 # @attr 1=4 title 
22 # @attr 1=7 isbn
23 # @attr 1=8 issn 
24 # @attr 1=1003 author 
25 # @attr 1=16 dewey 
26 # @attr 1=21 subject-holding 
27 # @attr 1=12 control-no 
28 # @attr 1=1007 standard-id 
29 # @attr 1=1016 any
30
31 sub usemap {{
32         4               => 'title',
33         7               => 'isn',
34         8               => 'isn',
35         1003    => 'author',
36 #       16              => '',
37         21              => 'subject',
38 #       12              => '',
39 #       1007    => '',
40         1016    => 'all',
41 }};
42
43 sub search {
44         my ( $self, $query ) = @_;
45
46         die "need query" unless defined $query;
47
48         # http://catalog.hathitrust.org/Search/Home?lookfor=croatia%20AND%20zagreb&type=title
49         my $url = 'http://catalog.hathitrust.org/Search/Home?lookfor=' . $query;
50
51 diag "get $url";
52
53         $self->mech->get( $url );
54
55         my $hits = 0;
56
57         if ( $self->mech->content =~ m{of\s*<span class="strong">(\d+)</span>\s*Results for}s ) {
58                 $hits = $1;
59         } else {
60                 diag "get't find results in ", $self->mech->content;
61                 return;
62         }
63
64 diag "got $hits results";
65
66         foreach my $link ( $self->mech->find_all_links( url_regex => qr{/Record/\d+} ) ) {
67                 push @{ $self->{records} }, $link->url;
68         }
69
70         return $self->{hits} = $hits;
71 }
72
73
74 sub next_marc {
75         my ($self,$format) = @_;
76
77         $format ||= 'marc';
78
79         my $url = shift @{ $self->{records} };
80
81         my $id = $1 if $url =~ m{Record/(\d+)};
82
83         $self->mech->get( $url . '.mrc' );
84
85         $self->save_marc( "$id.marc", $self->mech->content );
86
87         return $id;
88
89 }
90
91 1;