448beaa19cdaf6e84d53a3551d05f2c01511ad7c
[Biblio-Z3950.git] / Aleph.pm
1 package Aleph;
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 sub diag {
12         warn "# ", @_, $/;
13 }
14
15 # Koha Z39.50 query:
16 #
17 # Bib-1 @and @and @and @and @and @and @and @or
18 # @attr 1=4 title 
19 # @attr 1=7 isbn
20 # @attr 1=8 issn 
21 # @attr 1=1003 author 
22 # @attr 1=16 dewey 
23 # @attr 1=21 subject-holding 
24 # @attr 1=12 control-no 
25 # @attr 1=1007 standard-id 
26 # @attr 1=1016 any
27
28 # LCC - Klasifikacija Kongresne knjižnice 
29 # LCN - Signatura Kongresne knjižnice
30 # DDC - Deweyjeva klasifikacija 
31 # TIT - Naslovi 
32 # AUT - Autori 
33 # IMP - Impresum
34 # SUB - Predmetnice
35 # SRS - Nakladnička cjelina 
36 # LOC - Lokacija 
37 # WRD - Riječi 
38 # WTI - Riječi u polju naslova 
39 # WAU - Riječi u polju autora 
40 # WPE - Riječi u polju individualnog autora 
41 # WCO - Riječi u polju korporativnog autora 
42 # WME - Riječi u polju sastanka 
43 # WUT - Riječi u polju jedinstvenog naslova 
44 # WPL - Riječi u polju mjesta izdavanja 
45 # WPU - Riječi u polju nakladnika 
46 # WSU - Riječi u polju predmetnica 
47 # WSM - Riječi u predmetnicama MeSH-a 
48 # WST - Riječi u polju status
49 # WGA - Riječi u geografskim odrednicama 
50 # WYR - Godina izdavanja
51
52 sub usemap {{
53         4               => 'WTI=',
54         7               => 'ISBN=',
55         8               => 'ISSN=',
56         1003    => 'AUT=',
57         16              => 'DDC=',
58         21              => 'SUB=',
59         12              => 'LCN=',
60 #       1007    => '',
61         1016    => 'WRD=',
62 }};
63
64 our $session_id;
65
66 sub search {
67         my ( $self, $query ) = @_;
68
69         die "need query" unless defined $query;
70
71         $session_id ||= int rand(1000000000);
72         # FIXME allocate session just once
73         my $url = 'http://katalog.nsk.hr/F?RN=' . $session_id;
74         # fake JavaScript code on page which creates random session
75
76 diag "get $url";
77
78         my $mech = $self->{mech} || die "no mech?";
79         $mech->get( $url );
80
81 diag "advanced search";
82
83         $mech->follow_link( url_regex => qr/find-c/ );
84
85         my $database = $self->{database};
86
87         if ( $mech->content =~ m{Requested library is unavailable at the moment} ) {
88                 warn "ERROR: default database not available, try to swith to $database\n";
89                 $self->save_content;
90                 $mech->follow_link( url_regex => qr/local_base=$database/i );
91                 diag "re-try advanced search";
92                 $mech->follow_link( url_regex => qr/find-c/ );
93         }
94
95 diag "submit search [$query] on ", $self->{database};
96
97
98         $mech->submit_form(
99                 fields => {
100                         'ccl_term' => $query,
101                         'local_base' => $self->{database},
102                 },
103         );
104
105         my $hits = 0;
106         if ( $mech->content =~ m{ukupno\s+(\d+).*do\s+(\d+)} ) { # FIXME Many results in Crotian
107                 $hits = $1;
108                 $hits = $2 if $2 && $2 < $1; # correct for max. results
109         } elsif ( $mech->content =~ m{(\d+)\s+od\s+(\d+)} ) { # FIXME single result in Croatian
110                 $hits = $2;
111         } else {
112                 diag "get't find results in ", $mech->content;
113                 return;
114         }
115
116 diag "got $hits results, get first one";
117
118         $mech->follow_link( url_regex => qr/set_entry=000001/ );
119
120 diag "in MARC format";
121
122         $mech->follow_link( url_regex => qr/format=001/ );
123
124         return $self->{hits} = $hits;
125 }
126
127
128 our ( $hash, $marc );
129
130 sub next_marc {
131         my ($self,$format) = @_;
132
133         $format ||= 'marc';
134
135         my $mech = $self->{mech} || die "no mech?";
136
137 #warn "## ", $mech->content;
138
139         if ( $mech->content =~ m{Zapis\s+(\d+)}s ) {
140
141                 my $nr = $1;
142
143 warn "parse $nr";
144
145                 $marc = MARC::Record->new;
146                 $marc->encoding('utf-8');
147                 $hash = {};
148
149                 my $html = $mech->content;
150
151                 sub field {
152                         my ( $f, $v ) = @_;
153                         $v =~ s/\Q&nbsp;\E/ /gs;
154                         $v =~ s/\s+$//gs;
155 warn "## $f\t$v\n";
156                         $hash->{$f} = $v;
157
158                         if ( $f eq 'LDR' ) {
159                                 $marc->leader( $v );
160                                 return;
161                         }
162
163                         if ( $f =~ m/\D/ ) {
164                                 warn "$f not numeric!";
165                                 return;
166                         }
167
168                         if ( $v !~ s/^\|// ) { # no subfields
169                                 $marc->add_fields( $f, $v );
170 warn "## ++ ", dump( $f, $v );
171                                 return;
172                         }
173
174                         my ($i1,$i2) = (' ',' ');
175                         ($i1,$i2) = ($2,$3) if $f =~ s/^(...)(.)?(.)?/$1/;
176                         $i1 ||= ' ';
177                         $i2 ||= ' ';
178                         my @sf = split(/\|/, $v);
179                         @sf = map { s/^(\w)\s+//; { $1 => $_ } } @sf;
180 #warn "## sf = ", dump(@sf);
181                         $marc->add_fields( $f, $i1, $i2, @sf );
182 warn "## ++ ", dump( $f, $i1, $i2, @sf );
183                 }
184
185                 $html =~ s|<tr>\s*<td class=td1 id=bold[^>]*>(.+?)</td>\s*<td class=td1>(.+?)</td>|field($1,$2)|ges;
186                 diag "# hash ",dump($hash);
187                 diag "# marc ", $marc->as_formatted;
188
189                 my $id = $hash->{SYS} || die "no SYS";
190
191                 $self->save_marc( "$id.marc", $marc->as_usmarc );
192
193                 if ( $nr < $self->{hits} ) {
194                         $nr++;
195                         diag "follow link to next record $nr";
196                         $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
197                 }
198
199                 return $id;
200         } else {
201                 die "can't fetch COMARC format from ", $mech->content;
202         }
203
204 }
205
206 1;