strip spaces from end of value
[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=8 isbn-issn 
19 # @attr 1=7 isbn-issn 
20 # @attr 1=4 title 
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         1003    => 'WTI',
55         16              => 'CU',
56         21              => 'SU',
57 #       12              => '',
58 #       1007    => '',
59 #       1016    => '',
60 }};
61
62 our $session_id;
63
64 sub search {
65         my ( $self, $query ) = @_;
66
67         die "need query" unless defined $query;
68
69         $session_id ||= int rand(1000000000);
70         # FIXME allocate session just once
71         my $url = 'http://161.53.240.197:8991/F?RN=' . $session_id;
72         # fake JavaScript code on page which creates random session
73
74 diag "get $url";
75
76         my $mech = $self->{mech} || die "no mech?";
77         $mech->get( $url );
78
79 diag "advanced search";
80
81         $mech->follow_link( url_regex => qr/find-c/ );
82
83         my $database = $self->{database};
84
85         if ( $mech->content =~ m{Requested library is unavailable at the moment} ) {
86                 warn "ERROR: default database not available, try to swith to $database\n";
87                 $self->save_content;
88                 $mech->follow_link( url_regex => qr/local_base=$database/i );
89                 diag "re-try advanced search";
90                 $mech->follow_link( url_regex => qr/find-c/ );
91         }
92
93 diag "submit search [$query] on ", $self->{database};
94
95
96         $mech->submit_form(
97                 fields => {
98                         'ccl_term' => $query,
99                         'local_base' => $self->{database},
100                 },
101         );
102
103         my $hits = 0;
104         if ( $mech->content =~ m{ukupno\s+(\d+).*do\s+(\d+)}s ) {
105                 $hits = $1;
106                 $hits = $2 if $2 && $2 < $1; # correct for max. results
107         } else {
108                 diag "get't find results in ", $mech->content;
109                 return;
110         }
111
112 diag "got $hits results, get first one";
113
114         $mech->follow_link( url_regex => qr/set_entry=000001/ );
115
116 diag "in MARC format";
117
118         $mech->follow_link( url_regex => qr/format=001/ );
119
120         return $self->{hits} = $hits;
121 }
122
123
124 our ( $hash, $marc );
125
126 sub next_marc {
127         my ($self,$format) = @_;
128
129         $format ||= 'marc';
130
131         my $mech = $self->{mech} || die "no mech?";
132
133 #warn "## ", $mech->content;
134
135         if ( $mech->content =~ m{Zapis\s+(\d+)}s ) {
136
137                 my $nr = $1;
138
139 warn "parse $nr";
140
141                 $marc = MARC::Record->new;
142                 $hash = {};
143
144                 my $html = $mech->content;
145
146                 sub field {
147                         my ( $f, $v ) = @_;
148                         $v =~ s/\Q&nbsp;\E/ /gs;
149                         $v =~ s/\s+$//gs;
150 warn "## $f\t$v\n";
151                         $hash->{$f} = $v;
152
153                         if ( $f eq 'LDR' ) {
154                                 $marc->leader( $v );
155                                 return;
156                         }
157
158                         if ( $f =~ m/\D/ ) {
159                                 warn "$f not numeric!";
160                                 return;
161                         }
162
163                         if ( $v !~ s/^\|// ) { # no subfields
164                                 $marc->add_fields( $f, $v );
165 warn "## ++ ", dump( $f, $v );
166                                 return;
167                         }
168
169                         my ($i1,$i2) = (' ',' ');
170                         ($i1,$i2) = ($2,$3) if $f =~ s/^(...)(.)?(.)?/$1/;
171                         my @sf = split(/\|/, $v);
172                         @sf = map { s/^(\w)\s+//; { $1 => $_ } } @sf;
173 #warn "## sf = ", dump(@sf);
174                         $marc->add_fields( $f, $i1, $i2, @sf );
175 warn "## ++ ", dump( $f, $i1, $i2, @sf );
176                 }
177
178                 $html =~ s|<tr>\s*<td class=td1 id=bold[^>]*>(.+?)</td>\s*<td class=td1>(.+?)</td>|field($1,$2)|ges;
179                 diag "# hash ",dump($hash);
180                 diag "# marc ", $marc->as_formatted;
181
182                 my $id = $hash->{SYS} || die "no SYS";
183
184                 $self->save_marc( $id, $marc->as_usmarc );
185
186                 if ( $nr < $self->{hits} ) {
187                         $nr++;
188                         diag "follow link to next record $nr";
189                         $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
190                 }
191
192                 return $marc->as_usmarc;
193         } else {
194                 die "can't fetch COMARC format from ", $mech->content;
195         }
196
197 }
198
199 1;