collect results to support offset in fetch record
[Biblio-Z3950.git] / COBISS.pm
1 package COBISS;
2
3 use warnings;
4 use strict;
5
6 use MARC::Record;
7 use Data::Dump qw/dump/;
8
9 use base 'Scraper';
10
11 my $cobiss_marc21 = {
12         '010' => { a => [ '020', 'a' ] },
13          200  => {
14                         a => [  245 , 'a' ],
15                         f => [  245 , 'f' ],
16         },
17          205  => { a => [  250 , 'a' ] },
18          210  => {
19                 a => [  260 , 'a' ],
20                 c => [  260 , 'b' ],
21                 d => [  260 , 'c' ],
22         },
23         215 => {
24                 a => [  300 , 'a' ],
25                 c => [  300 , 'b' ],
26                 d => [  300 , 'c' ],
27         },
28         700 => {
29                 a => [  100 , 'a' ],
30         },
31 };
32
33 sub diag {
34         print "# ", @_, $/;
35 }
36
37 # Koha Z39.50 query:
38 #
39 # Bib-1 @and @and @and @and @and @and @and @or
40 # @attr 1=8 isbn-issn 
41 # @attr 1=7 isbn-issn 
42 # @attr 1=4 title 
43 # @attr 1=1003 author 
44 # @attr 1=16 dewey 
45 # @attr 1=21 subject-holding 
46 # @attr 1=12 control-no 
47 # @attr 1=1007 standard-id 
48 # @attr 1=1016 any
49
50 sub usemap {{
51         8               => 'BN',        # FIXME check
52         7               => 'SN',        # FIXME check
53         4               => 'TI',
54         1003    => 'TI',
55         16              => 'CU',
56         21              => 'SU',
57 #       12              => '',
58 #       1007    => '',
59 #       1016    => '',
60
61 }};
62
63 sub search {
64         my ( $self, $query ) = @_;
65
66         die "need query" unless defined $query;
67
68         my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
69
70 diag "get $url";
71
72         my $mech = $self->{mech} || die "no mech?";
73
74         my $hits;
75         $mech->get( $url );
76
77 diag "got session";
78
79         $mech->follow_link( text_regex => qr/union/ );
80
81 diag "switch to advanced form (select)";
82
83         $mech->follow_link( url_regex => qr/mode=3/ );
84
85 diag "submit search $query";
86
87         $mech->submit_form(
88                 fields => {
89                         'SS1' => $query,
90                 },
91         );
92
93         $hits = 0;
94         if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
95                 $self->{hits} = $hits = $1;
96         } else {
97                 diag "get't find results in ", $mech->content;
98                 return;
99         }
100
101 diag "got $hits results, get first one";
102
103         $mech->follow_link( url_regex => qr/ukaz=DISP/ );
104
105 diag "in COMARC format";
106
107         $mech->follow_link( url_regex => qr/fmt=13/ );
108
109         return $hits;
110 }
111
112
113 sub next_marc {
114         my ($self,$format) = @_;
115
116         my $mech = $self->{mech} || die "no mech?";
117
118         $format ||= 'unimarc';
119
120         die "unknown format: $format" unless $format =~ m{(uni|us)marc};
121
122         my $comarc;
123
124         if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
125
126                 my $markup = $1;
127                 my $nr = $2;
128                 my $id = $3;
129
130 diag "fetch $nr [$id] $format";
131
132                 $markup =~ s{</?b>}{}gs;
133                 $markup =~ s{<font[^>]*>}{<s>}gs;
134                 $markup =~ s{</font>}{<e>}gs;
135
136                 $markup =~ s/[\r\n]+\s{5}//gs; # join continuation lines
137
138                 $self->save_marc( "$id.xml", $markup );
139
140                 my $marc = MARC::Record->new;
141                 my $comarc = MARC::Record->new;
142
143                 foreach my $line ( split(/[\r\n]+/, $markup) ) {
144
145                         if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
146                                 diag "SKIP: $line";
147                         } else {
148                                 $line .= "<eol>";
149
150                                 my ( $f, $i1, $i2 ) = ( $1, $2, $3 );
151
152                                 our $marc_map = undef;
153                                 our $comarc_map = undef;
154                                 our $ignored = undef;
155
156                                 sub sf_parse {
157                                         my ($f,$sf,$v) = @_;
158
159                                         $v =~ s/\s+$//;
160
161                                         push @{ $comarc_map->{ $f } }, ( $sf, $v );
162                                         if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
163                                                 push @{ $marc_map->{ $m->[0] } }, ( $m->[1], $v );
164                                         } else {
165                                                 $ignored->{$f}++;
166                                         }
167                                         return ''; # fix warning
168                                 }
169                                 my $l = $line;
170                                 $l =~ s{<s>(\w)<e>([^<]+)}{sf_parse($f,$1, $2)}ges;
171
172                                 diag "[$format] $line -> ",dump( $comarc_map, $marc_map ) if $comarc_map;
173
174                                 foreach my $f ( keys %$comarc_map ) {
175                                         $comarc->add_fields( $f, $i1, $i2, @{ $comarc_map->{$f} } );
176                                 }
177
178                                 foreach my $f ( keys %$marc_map ) {
179                                         $marc->add_fields( $f, $i1, $i2, @{ $marc_map->{$f} } );
180                                 }
181                         }
182                 }
183
184                 $self->save_marc( "$id.marc", $marc->as_usmarc );
185                 $self->save_marc( "$id.unimarc", $comarc->as_usmarc );
186                 diag $marc->as_formatted;
187
188                 if ( $nr < $self->{hits} ) {
189                         warn "# fetch next result";
190                         $nr++;
191                         $mech->follow_link( url_regex => qr/rec=$nr/ );
192                 } else {
193                         warn "# no more results";
194                 }
195
196                 return $id;
197         } else {
198                 die "can't fetch COMARC format from ", $mech->content;
199         }
200
201 }
202
203 1;