ignore ssl certificate errors
[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=4 title 
41 # @attr 1=7 isbn
42 # @attr 1=8 issn 
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
51 # AU=   Autor - osoba
52 # CB=   Autor - korporacija
53 # CL=   Zbirka
54 # CP=   Mesto sast./dod. nazivu korp.
55 # PP=   Mesto izdanja
56 # PU=   Izdavač
57 # PY=   Godina izdanja
58 # P2=   Zaključna godina izdanja
59 # TI=   Naslov
60 # TO=   Naslov originala
61 # BN=   ISBN
62 # SN=   ISSN uz članak
63 # SP=   ISSN
64 # PN=   Predmetna odrednica - lično ime
65 # CS=   Predm. odred. - naziv korporacije
66 # DU=   Slobodno oblikovane predm. odred.
67 # SU=   Predmetne odrednice - sve
68 # AC=   Kod za vrstu autorstva
69 # CC=   Kod za vrstu sadržaja
70 # CO=   Zemlja/regija izdavanja
71 # FC=   Šifra organizacije
72 # LA=   Jezik teksta
73 # LC=   Kod za književni oblik
74 # LO=   Jezik izvornog dela
75 # TA=   Kod za predviđene korisnike
76 # TD=   Tipologija dok./dela
77 # UC=   UDK za pretraživanje
78 # KW=   Ključne reči
79
80 sub usemap {{
81         7               => 'BN',        # FIXME check
82         8               => 'SP',        # FIXME check
83         4               => 'TI',
84         1003    => 'AU',
85         16              => 'CU',
86         21              => 'SU',
87 #       12              => '',
88 #       1007    => '',
89 #       1016    => '',
90
91 }};
92
93 sub search {
94         my ( $self, $query ) = @_;
95
96         die "need query" unless defined $query;
97
98 #       my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
99         my $url = 'http://www.cobiss.ba/scripts/cobiss?ukaz=GETID&lani=en';
100
101 diag "get $url";
102
103         my $mech = $self->{mech} || die "no mech?";
104
105         my $hits;
106         $mech->get( $url );
107
108 diag "got session";
109
110         $mech->follow_link( text_regex => qr/union/ );
111
112 diag "switch to advanced form (select)";
113
114         $mech->follow_link( url_regex => qr/mode=3/ );
115
116 diag "submit search $query";
117
118         $mech->submit_form(
119                 fields => {
120                         'SS1' => $query,
121                 },
122         );
123
124         $hits = 0;
125         if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
126                 $self->{hits} = $hits = $1;
127         } else {
128                 diag "get't find results in ", $mech->content;
129                 return;
130         }
131
132 diag "got $hits results, get first one";
133
134         $mech->follow_link( url_regex => qr/ukaz=DISP/ );
135
136 diag "in COMARC format";
137
138         $mech->follow_link( url_regex => qr/fmt=13/ );
139
140         return $hits;
141 }
142
143
144 sub next_marc {
145         my ($self,$format) = @_;
146
147         my $mech = $self->{mech} || die "no mech?";
148
149         $format ||= 'unimarc';
150
151         die "unknown format: $format" unless $format =~ m{(uni|us)marc};
152
153         my $comarc;
154
155         if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
156
157                 my $markup = $1;
158                 my $nr = $2;
159                 my $id = $3;
160
161 diag "fetch $nr [$id] $format";
162
163                 $markup =~ s{</?b>}{}gs;
164                 $markup =~ s{<font[^>]*>}{<s>}gs;
165                 $markup =~ s{</font>}{<e>}gs;
166
167                 $markup =~ s/[\r\n]+\s{5}//gs; # join continuation lines
168
169                 $self->save_marc( "$id.xml", $markup );
170
171                 my $marc = MARC::Record->new;
172                 my $comarc = MARC::Record->new;
173
174                 foreach my $line ( split(/[\r\n]+/, $markup) ) {
175
176                         if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
177                                 diag "SKIP: $line";
178                         } else {
179                                 $line .= "<eol>";
180
181                                 my ( $f, $i1, $i2 ) = ( $1, $2, $3 );
182
183                                 our $marc_map = undef;
184                                 our $comarc_map = undef;
185                                 our $ignored = undef;
186
187                                 sub sf_parse {
188                                         my ($f,$sf,$v) = @_;
189
190                                         $v =~ s/\s+$//;
191
192                                         push @{ $comarc_map->{ $f } }, ( $sf, $v );
193                                         if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
194                                                 push @{ $marc_map->{ $m->[0] } }, ( $m->[1], $v );
195                                         } else {
196                                                 $ignored->{$f}++;
197                                         }
198                                         return ''; # fix warning
199                                 }
200                                 my $l = $line;
201                                 $l =~ s{<s>(\w)<e>([^<]+)}{sf_parse($f,$1, $2)}ges;
202
203                                 diag "[$format] $line -> ",dump( $comarc_map, $marc_map ) if $comarc_map;
204
205                                 foreach my $f ( keys %$comarc_map ) {
206                                         $comarc->add_fields( $f, $i1, $i2, @{ $comarc_map->{$f} } );
207                                 }
208
209                                 foreach my $f ( keys %$marc_map ) {
210                                         $marc->add_fields( $f, $i1, $i2, @{ $marc_map->{$f} } );
211                                 }
212                         }
213                 }
214
215                 $self->save_marc( "$id.marc", $marc->as_usmarc );
216                 $self->save_marc( "$id.unimarc", $comarc->as_usmarc );
217                 diag $marc->as_formatted;
218
219                 if ( $nr < $self->{hits} ) {
220                         warn "# fetch next result";
221                         $nr++;
222                         $mech->follow_link( url_regex => qr/rec=$nr/ );
223                 } else {
224                         warn "# no more results";
225                 }
226
227                 return $id;
228         } else {
229                 die "can't fetch COMARC format from ", $mech->content;
230         }
231
232 }
233
234 1;