c324898ef4d6eb00852aeb753f54c30a6ff47125
[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                 $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                 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
145
146                         if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
147                                 diag "SKIP: $line";
148                         } else {
149                                 our @f = ( $1, $2, $3 );
150                                 $line .= "<eol>";
151
152                                 if ( $format eq 'unimarc' ) {
153
154                                         diag dump(@f), "line: $line";
155                                         sub sf_uni {
156                                                 warn "sf ",dump(@_);
157                                                 push @f, @_;
158                                         }
159                                         $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges;
160                                         diag "f:", dump(@f), " left: |$line|";
161                                         $marc->add_fields( @f );
162
163                                 } elsif ( $format eq 'usmarc' ) {
164
165                                         my ( $f, $i1, $i2 ) = @f;
166
167                                         our $out = {};
168
169                                         sub sf_us {
170                                                 my ($f,$sf,$v) = @_;
171                                                 if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
172                                                         push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
173                                                 }
174                                                 return;
175                                         }
176                                         $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges;
177
178                                         diag "converted marc21 ",dump( $out );
179
180                                         foreach my $f ( keys %$out ) {
181                                                 $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
182                                         }
183                                 }
184                         }
185                 }
186
187                 $self->save_marc( $id, $marc->as_usmarc );
188                 diag $marc->as_formatted;
189
190                 $nr++;
191                 $mech->follow_link( url_regex => qr/rec=$nr/ );
192
193                 return $marc->as_usmarc;
194         } else {
195                 die "can't fetch COMARC format from ", $mech->content;
196         }
197
198 }
199
200 1;