Changeset 12465 for trunk/gsdl/perllib/downloaders/Z3950Download.pm
- Timestamp:
- 2006-08-18T09:24:29+12:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/perllib/downloaders/Z3950Download.pm
r11783 r12465 40 40 @Z3950Download::ISA = ('BasDownload'); 41 41 } 42 43 local (*YAZOUT, *YAZIN);44 42 45 43 my $arguments = … … 97 95 # Must set $self->{'url'}, since GLI use $self->{'url'} to calculate the log file name! 98 96 $self->{'url'} = $self->{'host'}.":".$self->{'port'}; 97 98 my $os = $ENV{'GSDLOS'}; 99 100 if ($os !~ /windows/) { 101 $self->{'yaz'} = "$ENV{'GSDLHOME'}/packages/yaz/yaz-2.1.4/client/yaz-client"; 102 } 103 else{ 104 $self->{'yaz'} = "$ENV{'GSDLHOME'}/bin/windows/yaz-client"; 105 } 106 99 107 return bless $self, $class; 100 108 … … 106 114 my ($hashGeneralOptions) = @_; 107 115 my ($strOpen,$strBase,$strFind,$strResponse,$intAmount,$intMaxRecords,$strRecords); 116 117 my $url = $self->{'url'}; 118 108 119 print STDERR "<<Defined Maximum>>\n"; 109 120 110 my $url = $self->{'url'};111 121 print STDERR "Opening connection to $url\n"; 112 122 113 my $childpid = open2(*YAZOUT, *YAZIN, "yaz-client") 114 or die "can't open pipe to yaz-client: $!"; 115 116 $strOpen = &run_command_with_output("open $url"); 123 my $yaz = $self->{'yaz'}; 124 125 my $childpid = open2(*YAZOUT, *YAZIN, $yaz) 126 or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!"); 127 $self->{'YAZOUT'} = *YAZOUT; 128 $self->{'YAZIN'} = *YAZIN; 129 130 $strOpen = $self->open_connection("open $url"); 131 132 if (!$strOpen) { 133 print STDERR "Cannot connect to $url\n"; 134 print STDERR "<<Finished>>\n"; 135 return 0; 136 } 137 117 138 print STDERR "Access database: \"$self->{'database'}\"\n"; 118 &run_command_without_output("base $self->{'database'}");139 $self->run_command_without_output("base $self->{'database'}"); 119 140 print STDERR "Searching for keyword: \"$self->{'find'}\"\n"; 120 $intAmount = &findAmount($self->{'find'});141 $intAmount = $self->findAmount($self->{'find'}); 121 142 122 143 if($intAmount <= 0) 123 144 { 124 145 ($intAmount == -1)? 125 print STDERR "Unexpected format, Parsing operation can not be performed\n" : 126 print STDERR "No Record is found\n"; 146 print STDERR "Something wrong with the arguments,downloading can not be performed\n": 147 print STDERR "No Record is found\n"; 148 print STDERR "<<Finished>>\n"; 127 149 return 0; 128 150 } 129 151 $intMaxRecords = ($self->{'max_records'} > $intAmount)? $intAmount : $self->{'max_records'}; 130 152 print STDERR "<<Total number of record(s):$intMaxRecords>>\n"; 131 $strRecords = &getRecords($intMaxRecords);132 print STDERR $strRecords;133 &saveRecords($self,$strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);153 $strRecords = "Records: $intMaxRecords\n".$self->getRecords($intMaxRecords); 154 155 $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords); 134 156 print STDERR "Closing connection...\n"; 157 print STDERR "<<Finished>>\n"; 158 135 159 close(YAZOUT); 136 160 close(YAZIN); 137 waitpid($childpid, 0);138 161 return 1; 139 162 } 140 163 164 sub open_connection{ 165 my ($self,$strCommand) = (@_); 166 167 $self->run_command($strCommand); 168 169 my $out = $self->{'YAZOUT'}; 170 171 $_ = <$out>; 172 173 return (/Connecting...OK/i)? 1: 0; 174 175 } 176 141 177 sub findAmount 142 178 { 179 my ($self) = shift (@_); 143 180 my($strFindTarget) = @_; 144 my $strResponse = &run_command_with_output("find $strFindTarget");145 181 my $strResponse = $self->run_command_with_output("find $strFindTarget","^Number of hits:"); 182 return ($strResponse =~ m/^Number of hits: (\d+)/m)? $1:-1; 146 183 } 147 184 148 185 sub getRecords 149 186 { 187 my ($self) = shift (@_); 150 188 my ($intMaxRecords) = @_; 151 my ($strShow,$intStartNumber,$ strResponse,$strRecords,$intRecordsLeft);189 my ($strShow,$intStartNumber,$numRecords,$strResponse,$strRecords,$intRecordsLeft); 152 190 153 191 $intStartNumber = 1; 154 192 $intRecordsLeft = $intMaxRecords; 193 $numRecords = 0; 194 $strResponse =""; 195 155 196 while ($intRecordsLeft > 0) 156 197 { 157 198 if($intRecordsLeft > 50) 158 199 { 159 print STDERR "<<Done:50>>\n";200 160 201 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+49)."\n"; 161 202 $numRecords = 50; 162 203 $strShow = "show $intStartNumber+50"; 163 204 $intStartNumber = $intStartNumber + 50; 164 205 $intRecordsLeft = $intRecordsLeft - 50; 206 165 207 } 166 208 else 167 209 { 168 print STDERR "<<Done:".($intRecordsLeft).">>\n";210 $numRecords = $intRecordsLeft; 169 211 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+$intRecordsLeft-1)."\n"; 170 212 $strShow = "show $intStartNumber+$intRecordsLeft"; 171 213 $intRecordsLeft = 0; 214 215 } 216 217 $strResponse .= $self->get($strShow,$numRecords); 218 219 if ($strResponse eq ""){ 220 print STDERR "<<ERROR: failed to get $numRecords records>>\n"; 172 221 } 222 else{ 223 print STDERR "<<Done:$numRecords>>\n"; 224 } 225 } 226 227 return "$strResponse\n"; 173 228 174 $strResponse = &run_command_with_output($strShow);175 176 if($strResponse =~ m/Records: (\d*?)\n(.*?)nextResultSetPosition = (\d*?)\n/s)177 {178 $strRecords .= $2;179 }180 }181 return $strRecords;182 229 } 183 230 … … 188 235 # setup directories 189 236 # Currently only gather the MARC format 190 my $strFileName = &generateFileName($self,$intMaxRecords); 237 my $strFileName = $self->generateFileName($intMaxRecords); 238 239 $strOutputDir =~ s/"//g; 240 191 241 my $strFileURL = "$strOutputDir/$self->{'host'}/$strFileName.marc"; 192 $strFileURL =~ s/:/\//g; 193 242 194 243 # prepare subdirectory for record (if needed) 195 my ($strSubDirPath,$unused) = dirFileSplit($strFileURL); 244 my ($strSubDirPath,$unused) = $self->dirFileSplit($strFileURL); 245 196 246 &util::mk_all_dir($strSubDirPath); 197 247 198 248 my $ds = &util::get_dirsep(); 199 249 my $strOutputFile = &util::filename_cat($strOutputDir,$self->{'host'},"$strFileName.marc"); 200 $strOutputFile =~ s/:/$ds/g; 201 250 202 251 print STDERR "Saving records to \"$strOutputFile\"\n"; 203 252 204 253 # save record 205 254 open (ZOUT,">$strOutputFile") 206 || die "Unable to save oai metadatarecord: $!\n";255 || die "Unable to save Z3950 record: $!\n"; 207 256 print ZOUT $strRecords; 208 257 close(ZOUT); 209 258 } 210 259 260 211 261 sub run_command_with_output 212 262 { 263 my ($self,$strCMD,$strStopRE) =@_; 264 265 $self->run_command($strCMD); 266 267 return $self->get_output($strStopRE); 268 269 } 270 271 sub get{ 272 my ($self,$strShow,$numRecord) = @_; 273 274 $self->run_command($strShow); 275 276 my $strFullOutput=""; 277 my $count=0; 278 my $readRecord = 0; 279 280 while (my $strLine = <YAZOUT>) 281 { 282 283 if ($strLine =~ m/Records: ([\d]*)/i ){ 284 $readRecord = 1; 285 next; 286 } 287 288 return $strFullOutput if ($strLine =~ m/nextResultSetPosition|Not connected/i); 289 290 next if(!$readRecord); 291 292 $strFullOutput .= $strLine; 293 } 294 295 } 296 297 sub run_command_without_output 298 { 299 my ($self) = shift (@_); 213 300 my ($strCMD) = @_; 214 215 return &run_command($strCMD,"^Elapsed:.*\$"); 216 } 217 218 sub run_command_without_output 219 { 220 my ($strCMD) = @_; 221 222 &run_command($strCMD); 301 302 $self->run_command($strCMD); 223 303 } 224 304 225 305 sub run_command 226 306 { 227 my ($strCMD,$strStopRE) = @_; 228 229 230 print YAZIN "$strCMD\n"; 307 my ($self,$strCMD) = @_; 308 309 my $input = $self->{'YAZIN'}; 310 311 print $input "$strCMD\n"; 312 } 313 314 sub get_output{ 315 my ($self,$strStopRE) = @_; 316 231 317 if (!defined $strStopRE){return "";} 232 318 else 233 319 { 234 320 my $strFullOutput; 235 while (my $strLine = <YAZOUT>) 321 my $output = $self->{'YAZOUT'}; 322 while (my $strLine = <$output>) 236 323 { 237 $strFullOutput .= $strLine; 238 if($strLine =~ m/$strStopRE/){return $strFullOutput;}324 $strFullOutput .= $strLine; 325 if($strLine =~ m/^$strStopRE|Not connected/i){return $strFullOutput;} 239 326 } 240 327 } … … 245 332 my ($self,$intMaxRecords) = @_; 246 333 my $strFileName = ($self->{'database'})."_".($self->{'find'})."_".($intMaxRecords); 334 247 335 } 248 336 249 337 sub dirFileSplit 250 338 { 251 my ($strFile) = @_; 252 253 my @aryDirs = split("/",$strFile); 339 my ($self,$strFile) = @_; 340 341 my @aryDirs = split("[/\]",$strFile); 342 254 343 my $strLocalFile = pop(@aryDirs); 255 344 my $strSubDirs = join("/",@aryDirs); … … 258 347 } 259 348 349 sub url_information 350 { 351 my ($self,$url) = @_; 352 353 $url = $self->{'url'} unless defined $url; 354 355 my $yaz = $self->{'yaz'}; 356 357 my $childpid = open2(*YAZOUT, *YAZIN, $yaz) 358 or die "can't open pipe to yaz-client: $!"; 359 360 $self->{'YAZOUT'} = *YAZOUT; 361 $self->{'YAZIN'} = *YAZIN; 362 363 my $strOpen = $self->open_connection("open $url"); 364 365 if (!$strOpen) { 366 print STDERR "Cannot connect to $url\n"; 367 print STDERR "<<Finished>>\n"; 368 return 0; 369 } 370 371 372 $strOpen = $self->run_command_with_output("open $url","^Options"); 373 374 375 $strOpen =~ s/Z> //g; 376 $strOpen =~ s/Elapsed:.*//g; 377 378 print STDERR $strOpen; 379 380 print STDERR "<<Finished>>\n"; 381 382 close(YAZOUT); 383 close(YAZIN); 384 385 return 0; 386 387 } 388 260 389 sub error 261 390 { 262 my ($s trFunctionName,$strError) = @_;391 my ($self,$strFunctionName,$strError) = @_; 263 392 { 264 393 print STDERR "Error occoured in Z3950Download.pm\n".
Note:
See TracChangeset
for help on using the changeset viewer.