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