INSPEC/Retriever.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 161 CC 11 SCAR 37.6
CC
11
Cov
0%
retrieve
90.0
get_INSPEC_search
12.0
get_AXIOM_URL
2.0
CRAP 43.1
1 package INSPEC::Retriever;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AXIOM_userid $AXIOM_password $AXIOM_code);
5
6 $VERSION = '0.01';
7
8 require 5.000;
9
10 use Exporter;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw();
14 @EXPORT_OK = qw(
15 retrieve
16 );
17
18 # global package variables, user must assign these in
19 # calling code
20 #$AXIOM_userid = "";
21 #$AXIOM_password = "";
22 #$AXIOM_code = "";
23
24
25
26
27
28 #
29 # Routines used for the INPSEC service through AXIOM
30 # By: Vincent P. LaBella copyright (c) 2000 vlabella@uark.edu
31 #
32 # You are free to use this code but not make money from it ;-)
33 # --If you make money from it, give me some.
34 #
35
36 sub get_AXIOM_URL
37 {
38 #
39 # returns the axiom leading url with the user id and all
40 #
41 my $AXIOM_home_page = "axiom.iop.org";
42 return "http://".$AXIOM_userid.":".$AXIOM_password."\@".$AXIOM_home_page."/P/".$AXIOM_code."/searchres?Action=Search&Srch=E&src=01";
43 }
44
45 sub uuencode
46 {
47 #
48 # returns the uuencoded string use this for forming search strings
49 #
50 #translation table
51 #%28 = (
52 #%22 = "
53 #%29 = )
54 #+ = (space)
55 #%3C = <
56 #%3E = <
57 my $ret = $_[0];
58 $ret =~ s/ /\+/g;
59 $ret =~ s/\"/\%22/g;
60 $ret =~ s/</\%3C/g;
61 $ret =~ s/>/\%3E/g;
62 $ret =~ s/\(/\%28/g;
63 $ret =~ s/\)/\%29/g;
64 return $ret;
65 }
66
67 sub get_INSPEC_search
68 {
69 #
70 # short and easy takes the coden and the vol and page number and returns search string
71 # must appeand to end of QueryText=
72 #
73 my($ID,$vol,$page,@extra_ids) = @_;
74 #
75 # decide wheter or not it is a CODEN or ISSN
76 #
77 my($id_search)="";
78 if(index($ID,"-") == -1)
79 {
80 $id_search="CN";
81 }
82 else
83 {
84 $id_search="IS";
85 }
86 #
87 my $ret = "(".$ID.")+<in>+(".$id_search.")";
88 #print @extra_ids."\n";
89 foreach my $ids (@extra_ids)
90 {
91 $ret .= "+<or>+(".$ids.")+<in>+(".$id_search.")";
92 }
93 if( @extra_ids != 0)
94 {
95 $ret = "(".$ret.")";
96 }
97
98 $ret.="+<and>+(".$vol.")+<in>+(VV)+<and>+(".$page.")+<in>+(PS)&insp=1";
99
100 return uuencode($ret);
101 }
102
103
104 sub pull_info
105 {
106 #
107 # pulls the info from the inspec search return
108 #
109 # 0 the text to pull the infor from
110 # 1.. the name of the info
111 #
112 # info is in the form
113 #<b>Accession number</b>: 6420680<br>
114 # in this case 1 would be "Accession number"
115 # and the return value would be "6420680"
116 #
117 my ($text,$name) = @_;
118 my $ret = "";
119
120 my $start_idx = index($text,"<b>".$name."</b>:");
121 my $end_idx = index($text,"<br>",$start_idx);
122 $ret = substr($text,$start_idx,$end_idx-$start_idx);
123
124 $ret =~ s/\Q<b>$name<\/b>://;
125 $ret =~ s/^\s+//;
126 $ret =~ s/\s+$//;
127
128 return $ret;
129 }
130
131
132
133 sub retrieve{
134 #
135 # get_cite - gets the citation from the INSPEC database
136 #
137 # Agruments
138 #=====================================================================
139 # 0..............CODEN or ISSN
140 # 1..............volume numer
141 # 2..............starting page number
142 # 3-n............multiple CODENS or ISSN will search all
143 # if you are not sure or if journal has changed CODEN or ISSN in past
144 #
145 # Returns
146 #=====================================================================
147 # returns hash with the keys as inspec field names and
148 # the content is the info as recieved from INSPEC
149 #
150 # If there is an error then the hash will only have one key called "ERROR"
151 # which will equal 1
152 #
153 # internal variable for bug testing
154 my $dump = 0; # this will dump all info to files if 1
155 my $testing = 0; # this will load the files for parse testing if 1
156 my $page1 = ""; # storage buffer
157 my %ret; # the return hash
158 use LWP::Simple;
159 my($ID,$volume,$page) = @_;
160 #
161 # see if there is more than one CODEN or ISSN on the end
162 #
163 my @extra_ids;
164 if( @_ > 3 ){
165 my $j=0;
166 for(my $i=3;$i<@_;$i++)
167 {
168 if(@_[$i] ne "")
169 {
170 $extra_ids[$j]=@_[$i];
171 $j++;
172 }
173 }
174 }
175 #
176 # FORM the URL to search AXIOM's INSPEC database
177 #
178 # sample url to get our 2x4 paper
179 #
180 #http://getcite:getcite1@axiom.iop.org/P/ARKAN/searchres?Action=Search&Srch=E&
181 #QueryText=%28PRLTAO%29+%3Cin%3E+%28CN%29+%3Cand%3E+%2883%29+%3Cin%3E+%28VV%29+%3Cand%3E+%282989%29+%3Cin%3E+%28PS%29&src=01
182 #
183 my $URL = get_AXIOM_URL()."&QueryText=".get_INSPEC_search($ID,$volume,$page,@extra_ids);
184 if($dump) { # dump URL
185 open (fhout,"+>url");
186 print fhout $URL."\n";
187 close fhout;
188 }
189 if(!$testing)
190 {
191 #
192 # get to web and get it
193 #
194 $page1=get($URL);
195 if($dump){ # dump page1 to page1.html
196 open (fhout,"+>page1.html");
197 print fhout $page1;
198 close fhout;
199 print "\nDumped page to page1.html\n";
200 }
201 }
202 else
203 { #
204 # this is for pratice parsing load it from a file
205 #
206 open(fh,"page1.html");
207 $page1="";
208 foreach my $line (<fh>) {$page1.=$line;}
209 close(fh);
210 print "\nLoading from file page1.html\n";
211 }
212 if($page1 eq "")
213 {
214 #
215 # error
216 #
217 #print "ERROR page1 contains no text.\n URL=[".$URL."]\nExiting.\n";
218 $ret{'ERROR'}=1;
219 return %ret;
220 }
221 #
222 # get everything after "Record 1 of" if it doesn't exist then there is error
223 #
224 my $start_text="Record 1 of";
225 my $idx=index($page1,$start_text);
226 if( $idx == -1 )
227 {
228 #
229 # error
230 #
231 $ret{'ERROR'}=1;
232 #print %ret;
233 #print "ERROR no search results found.\nURL=[".$URL."]\nExiting.\n";
234 return %ret;
235 }
236 $page1 = substr($page1,$idx);
237 # clean it out some more
238 #$page1 =~ m/Number of references:<\/B>.+<BR>/g;
239 $page1 =~ m/Number of references:/g;
240 my $end_text="Number of references:";
241 my $end_idx = index($page1,$end_text);
242 $end_idx = index($page1,"<BR>",$end_idx)+length("<BR>");
243 #print $end_idx."\n";
244 $start_text = "<B>Accession number:</B>";
245 $page1 = substr($page1,index($page1,$start_text),$end_idx-index($page1,$start_text));
246 $page1 =~ s/\s{2,}/ /g;
247 $page1 =~ s/\n/ /g;
248 $page1 =~ s/\t/ /g;
249 $page1 =~ s/<b>/<B>/g;
250 $page1 =~ s/<br>/<BR>/g;
251 $page1 =~ s/<\\b>/<\\B>/g;
252 #$page1 =~ s/\s+$//;
253 #$page1 =~ s/\s+//;
254 if($dump)
255 {
256 #
257 # dump page1 to page2.html
258 #
259 open (fhout,"+>page2.html");
260 print fhout $page1;
261 close fhout;
262 print "\nDumped parsed text to page2.html\n";
263 }
264 #
265 # Fill Hash with raw information from INSPEC
266 #
267 # format of inspec stuff is
268 # <b>key text</b>: entry text<br>
269 #
270 $end_idx = 0;
271 my $start_idx = 0;
272 while( $end_idx + length("<BR>") < length($page1) )
273 {
274 $start_idx = index($page1,"<B>",$end_idx);
275 $end_idx = index($page1,"<BR>",$start_idx);
276 my $text = substr($page1,$start_idx,$end_idx-$start_idx);
277 my $key = substr($text,length("<B>"),index($text,"</B>")-length("</B>")+1);
278 my $value = substr($text,index($text,"</B>")+length("</B>"));
279 $key =~ s/^\s+//;
280 $key =~ s/:$//;
281 $key =~ s/\s+$//;
282 $value =~ s/://;
283 $value =~ s/^\s+//;
284 $value =~ s/\s+$//;
285 $ret{$key}=$value;
286 }
287 return %ret;
288 }
289
290 1;
291 __END__