cleanup unimarc/marc parsing
[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 $comarc = $1;
127                 my $nr = $2;
128                 my $id = $3;
129
130 diag "fetch_marc $nr [$id] $format";
131
132                 $comarc =~ s{</?b>}{}gs;
133                 $comarc =~ s{<font[^>]*>}{<s>}gs;
134                 $comarc =~ s{</font>}{<e>}gs;
135
136                 open(my $out, '>:utf8', "comarc/$id");
137                 print $out $comarc;
138                 close($out);
139
140                 print $comarc;
141
142                 my $marc = MARC::Record->new;
143
144                 $comarc =~ s/[\r\n]+\s{5}//gs; # join continuation lines
145 warn "## comarc join: $comarc\n";
146
147                 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
148
149                         if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
150                                 diag "SKIP: $line";
151                         } else {
152                                 $line .= "<eol>";
153
154                                 my ( $f, $i1, $i2 ) = ( $1, $2, $3 );
155
156                                 our $out = undef;
157                                 our $ignored = undef;
158
159                                 sub sf_us {
160                                         my ($format,$f,$sf,$v) = @_;
161
162                                         $v =~ s/\s+$//;
163
164                                         if ( $format =~ m/unimarc/i ) {
165                                                 push @{ $out->{ $f } }, ( $sf, $v );
166                                         } elsif ( $format =~ m/marc/ ) {
167                                                 if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
168                                                         push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
169                                                 } else {
170                                                         $ignored->{$f}++;
171                                                 }
172                                         }
173                                         return ''; # fix warning
174                                 }
175                                 my $l = $line;
176                                 $l =~ s{<s>(\w)<e>([^<]+)}{sf_us($format,$f,$1, $2)}ges;
177
178                                 diag "[$format] $line -> ",dump( $out ) if $out;
179
180                                 foreach my $f ( keys %$out ) {
181                                         $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
182                                 }
183                         }
184                 }
185
186                 $self->save_marc( "$id.$format", $marc->as_usmarc );
187                 diag $marc->as_formatted;
188
189                 if ( $nr < $self->{hits} ) {
190                         warn "# fetch next result";
191                         $nr++;
192                         $mech->follow_link( url_regex => qr/rec=$nr/ );
193                 } else {
194                         warn "# no more results";
195                 }
196
197                 return $marc->as_usmarc;
198         } else {
199                 die "can't fetch COMARC format from ", $mech->content;
200         }
201
202 }
203
204 1;