support turkish translation of Aleph
[Biblio-Z3950.git] / AlephTR.pm
1 package AlephTR;
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://mksun.mkutup.gov.tr/F?RN=' . $session_id . '&func=find-c-0';
74         # fake JavaScript code on page which creates random session
75
76 diag "advanced search $url";
77
78         my $mech = $self->{mech} || die "no mech?";
79         $mech->get( $url );
80
81 diag "submit search [$query]";
82
83         $mech->submit_form(
84                 fields => {
85                         'ccl_term' => $query,
86                 },
87         );
88
89         my $hits = 0;
90         if ( $mech->content =~ m{Toplam\s+(\d+)} ) { # FIXME Many results in Crotian
91                 $hits = $1;
92         } else {
93                 diag "get't find results in ", $mech->content;
94                 return;
95         }
96
97 diag "got $hits results, get first one";
98
99         $mech->follow_link( url_regex => qr/set_entry=000001/ );
100
101 diag "in MARC format";
102
103         $mech->follow_link( url_regex => qr/format=001/ );
104
105         return $self->{hits} = $hits;
106 }
107
108
109 our ( $hash, $marc );
110
111 sub next_marc {
112         my ($self,$format) = @_;
113
114         $format ||= 'marc';
115
116         my $mech = $self->{mech} || die "no mech?";
117
118 #warn "## ", $mech->content;
119
120         if ( $mech->content =~ m{kay.ttan\s+(\d+)}s ) {
121
122                 my $nr = $1;
123
124 warn "parse $nr";
125
126                 $marc = MARC::Record->new;
127                 $marc->encoding('utf-8');
128                 $hash = {};
129
130                 my $html = $mech->content;
131
132 #diag $html;
133
134                 sub field {
135                         my ( $f, $v ) = @_;
136                         $v =~ s/\Q \E/ /gs;
137                         $v =~ s/\s+$//gs;
138 warn "## $f\t[$v]\n";
139                         $hash->{$f} = $v;
140
141                         if ( $f eq 'LDR' ) {
142                                 $marc->leader( $v );
143                                 return;
144                         }
145
146                         if ( $f =~ m/\D/ ) {
147                                 warn "$f not numeric!";
148                                 return;
149                         }
150
151                         if ( $v !~ s/^\|// ) { # no subfields
152                                 $marc->add_fields( $f, $v );
153 warn "## ++ ", dump( $f, $v );
154                                 return;
155                         }
156
157                         my ($i1,$i2) = (' ',' ');
158                         ($i1,$i2) = ($2,$3 || ' ') if $f =~ s/^(...)(.)(.)?/$1/;
159                         my @sf = split(/\|/, $v);
160                         @sf = map { s/^(\w)\s+//; { $1 => $_ } } @sf;
161 #warn "## sf = ", dump(@sf);
162                         $marc->add_fields( $f, $i1, $i2, @sf );
163 warn "## ++ ", dump( $f, $i1, $i2, @sf );
164                 }
165
166                 $html =~ s|<tr>\s*?<td[^>]*class=td1[^>]*>(.+?)</td>\s*?<td class=td1>(.+?)</td>\s*</tr>|field($1,$2)|ges;
167                 diag "# hash ",dump($hash);
168                 diag "# marc ", $marc->as_formatted;
169
170                 my $id = $hash->{SYS} || die "no SYS";
171
172                 $self->save_marc( "$id.marc", $marc->as_usmarc );
173
174                 if ( $nr < $self->{hits} ) {
175                         $nr++;
176                         diag "follow link to next record $nr";
177                         $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
178                 }
179
180                 return $id;
181         } else {
182                 die "can't fetch " . __PACKAGE__ . " format from ", $mech->content;
183         }
184
185 }
186
187 1;