/[Z3950-HTML-Scraper]/server.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /server.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations)
Sat Jun 20 22:09:33 2009 UTC (14 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 5161 byte(s)
support multiple results 

1 #!/usr/bin/perl -w
2
3 use Net::Z3950::SimpleServer;
4 use Net::Z3950::OID;
5 use COBISS;
6 use strict;
7
8 my $max_records = 3; # XXX configure this
9 my $max_result_sets = 10;
10
11 sub diag {
12 print "# ", @_, $/;
13 }
14
15 sub InitHandle {
16 my $this = shift;
17 my $session = {};
18
19 $this->{HANDLE} = $session;
20 $this->{IMP_NAME} = "Z39.50 HTML scraping bot";
21 $this->{IMP_VER} = "0.1";
22 $session->{SETS} = {};
23 }
24
25 use Data::Dumper;
26
27 sub SearchHandle {
28 my $this = shift;
29
30 diag "SearchHandle ",Dumper($this);
31
32 my $session = $this->{HANDLE};
33 my $rpn = $this->{RPN};
34 my $query;
35
36 eval { $query = $rpn->{query}->render(); };
37 if ( $@ && ref($@) ) { ## Did someone/something report any errors?
38 $this->{ERR_CODE} = $@->{errcode};
39 $this->{ERR_STR} = $@->{errstr};
40 return;
41 }
42
43 diag "search for $query";
44
45 my $setname = $this->{SETNAME};
46 my $repl_set = $this->{REPL_SET};
47 my $result;
48 unless ( $result = COBISS->search( $query ) ) {
49 $this->{ERR_CODE} = 108;
50 return;
51 }
52 my $hits = $COBISS::hits || diag "no results for $query";
53 diag "got $hits hits";
54 my $rs = {
55 lower => 1,
56 upper => $hits < $max_records ? $max_records : $hits,
57 data => $result->{'resultElements'}, # FIXME
58 };
59 my $sets = $session->{SETS};
60
61 if ( defined( $sets->{$setname} ) && !$repl_set ) {
62 $this->{ERR_CODE} = 21;
63 return;
64 }
65 if ( scalar keys %$sets >= $max_result_sets ) {
66 $this->{ERR_CODE} = 112;
67 $this->{ERR_STR} = "Max number is $max_result_sets";
68 return;
69 }
70 $sets->{$setname} = $rs;
71 $this->{HITS} = $session->{HITS} = $hits;
72 $session->{QUERY} = $query;
73 }
74
75 sub FetchHandle {
76 my $this = shift;
77 my $session = $this->{HANDLE};
78 my $setname = $this->{SETNAME};
79 my $req_form = $this->{REQ_FORM};
80 my $offset = $this->{OFFSET};
81 my $sets = $session->{SETS};
82 my $hits = $session->{HITS};
83 my $rs;
84 my $record;
85
86 diag Dumper( $this );
87
88 if ( !defined( $rs = $sets->{$setname} ) ) {
89 $this->{ERR_CODE} = 30;
90 return;
91 }
92 if ( $offset > $hits ) {
93 $this->{ERR_CODE} = 13;
94 return;
95 }
96 $this->{BASENAME} = "HtmlZ3950";
97
98 # if ( !defined($req_form) || ( $req_form eq &Net::Z3950::OID::xml ) )
99 if (0)
100 { ## XML records
101 $this->{REP_FORM} = &Net::Z3950::OID::xml;
102 $this->{RECORD} = '<xml>FIXME: not implementd</xml>';
103 }
104 elsif ( $req_form eq &Net::Z3950::OID::unimarc ) { # FIXME convert to usmarc
105 $this->{REP_FORM} = &Net::Z3950::OID::unimarc;
106 $this->{RECORD} = COBISS->fetch_marc;
107 }
108 else { ## Unsupported record format
109 $this->{ERR_CODE} = 239;
110 $this->{ERR_STR} = $req_form;
111 return;
112 }
113 if ( $offset == $hits ) {
114 $this->{LAST} = 1;
115 }
116 else {
117 $this->{LAST} = 0;
118 }
119 }
120
121 sub CloseHandle {
122 my $this = shift;
123 }
124
125 my $z = new Net::Z3950::SimpleServer(
126 INIT => \&InitHandle,
127 SEARCH => \&SearchHandle,
128 FETCH => \&FetchHandle,
129 CLOSE => \&CloseHandle
130 );
131 $z->launch_server( $0, @ARGV );
132
133 package Net::Z3950::RPN::And;
134
135 sub render {
136 my $this = shift;
137 return $this->[0]->render() . ' AND ' . $this->[1]->render();
138 }
139
140 package Net::Z3950::RPN::Or;
141
142 sub render {
143 my $this = shift;
144 return $this->[0]->render() . ' OR ' . $this->[1]->render();
145 }
146
147 package Net::Z3950::RPN::AndNot;
148
149 sub render {
150 my $this = shift;
151 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
152 }
153
154 package Net::Z3950::RPN::Term;
155
156 use COBISS;
157
158 sub render {
159 my $this = shift;
160
161 print "render ", $this;
162
163 my $attributes = {};
164 my $prefix = "";
165 foreach my $attr ( @{ $this->{attributes} } ) {
166 my $type = $attr->{attributeType};
167 my $value = $attr->{attributeValue};
168 $attributes->{$type} = $value;
169 }
170 if ( defined( my $use = $attributes->{1} ) ) {
171 if ( defined( my $field = COBISS::usemap($use) ) ) {
172 $prefix = $field;
173 }
174 else {
175 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
176 }
177 }
178 if ( defined( my $rel = $attributes->{2} ) )
179 { ## No relation attributes supported
180 if ( $rel != 3 ) {
181 die { errcode => 117, errstr => $rel };
182 }
183 }
184 if ( defined( my $pos = $attributes->{3} ) )
185 { ## No position attributes either
186 if ( $pos != 3 ) {
187 die { errcode => 119, errstr => $pos };
188 }
189 }
190 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
191 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
192 die { errcode => 118, errstr => $struc };
193 }
194 }
195 if ( defined( $attributes->{5} ) ) { ## No truncation
196 die { errcode => 113, errstr => 5 };
197 }
198 my $comp = $attributes->{6};
199 if ($prefix) {
200 if ( defined($comp) && ( $comp >= 2 ) ) {
201 $prefix = "all$prefix= ";
202 }
203 else {
204 $prefix = "$prefix=";
205 }
206 }
207
208 my $q = $prefix . $this->{term};
209 print "# q: $q\n";
210 return $q;
211 }
212

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26