decode utf-8 marc correctly before saving
[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         foreach my $link ( $self->mech->find_all_links( url_regex => qr{/Record/\d+} ) ) {
68                 push @{ $self->{records} }, $link->url;
69         }
70
71         return $self->{hits} = $hits;
72 }
73
74
75 sub next_marc {
76         my ($self,$format) = @_;
77
78         $format ||= 'marc';
79
80         my $url = shift @{ $self->{records} };
81
82         my $id = $1 if $url =~ m{Record/(\d+)};
83
84         $self->mech->get( $url . '.mrc' );
85
86         my $marc = decode('utf-8', $self->mech->content );
87
88         $self->save_marc( "$id.marc", $marc );
89
90         return $id;
91
92 }
93
94 1;