c8e529aef9c7b6c2821ccca0d68b5209d4a259d3
[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
63 sub search {
64         my ( $self, $query ) = @_;
65
66         die "need query" unless defined $query;
67
68         my $url = 'http://161.53.240.197:8991/F?RN=' . int rand(1000000000);
69         # fake JavaScript code on page which creates random session
70
71 diag "get $url";
72
73         my $mech = $self->{mech} || die "no mech?";
74         $mech->get( $url );
75
76 diag "advanced search";
77
78         $mech->follow_link( url_regex => qr/find-c/ );
79
80 diag "submit search [$query] on ", $self->{database};
81
82         $self->save_content;
83
84         $mech->submit_form(
85                 fields => {
86                         'ccl_term' => $query,
87                         'local_base' => $self->{database},
88                 },
89         );
90
91         my $hits = 0;
92         if ( $mech->content =~ m{ukupno\s+(\d+).*do\s+(\d+)}s ) {
93                 $hits = $1;
94                 $hits = $2 if $2 && $2 < $1; # correct for max. results
95         } else {
96                 diag "get't find results in ", $mech->content;
97                 return;
98         }
99
100 diag "got $hits results, get first one";
101
102         $mech->follow_link( url_regex => qr/set_entry=000001/ );
103
104 diag "in MARC format";
105
106         $mech->follow_link( url_regex => qr/format=001/ );
107
108         return $hits;
109 }
110
111
112 our ( $hash, $marc );
113
114 sub next_marc {
115         my ($self,$format) = @_;
116
117         $format ||= 'marc';
118
119         my $mech = $self->{mech} || die "no mech?";
120
121 #warn "## ", $mech->content;
122
123         if ( $mech->content =~ m{Zapis\s+(\d+)}s ) {
124
125                 my $nr = $1;
126
127 warn "parse $nr";
128
129                 $marc = MARC::Record->new;
130                 $hash = {};
131
132                 my $html = $mech->content;
133
134                 sub field {
135                         my ( $f, $v ) = @_;
136                         $v =~ s/\Q&nbsp;\E/ /gs;
137 warn "## $f\t$v\n";
138                         $hash->{$f} = $v;
139
140                         if ( $f eq 'LDR' ) {
141                                 $marc->leader( $v );
142                                 return;
143                         }
144
145                         if ( $f =~ m/\D/ ) {
146                                 warn "$f not numeric!";
147                                 return;
148                         }
149
150                         if ( $v !~ s/^\|// ) { # no subfields
151                                 $marc->add_fields( $f, $v );
152 warn "## ++ ", dump( $f, $v );
153                                 return;
154                         }
155
156                         my ($i1,$i2) = (' ',' ');
157                         ($i1,$i2) = ($2,$3) if $f =~ s/^(...)(.)?(.)?/$1/;
158                         my @sf = split(/\|/, $v);
159                         @sf = map { s/^(\w)\s+//; { $1 => $_ } } @sf;
160 #warn "## sf = ", dump(@sf);
161                         $marc->add_fields( $f, $i1, $i2, @sf );
162 warn "## ++ ", dump( $f, $i1, $i2, @sf );
163                 }
164
165                 $html =~ s|<tr>\s*<td class=td1 id=bold[^>]*>(.+?)</td>\s*<td class=td1>(.+?)</td>|field($1,$2)|ges;
166                 diag "# hash ",dump($hash);
167                 diag "# marc ", $marc->as_formatted;
168
169                 my $id = $hash->{SYS} || die "no SYS";
170
171                 $self->save_marc( $id, $marc->as_usmarc );
172
173                 $nr++;
174
175                 $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
176
177                 return $marc->as_usmarc;
178         } else {
179                 die "can't fetch COMARC format from ", $mech->content;
180         }
181
182 }
183
184 1;