strip spaces from end of value
[Biblio-Z3950.git] / Aleph.pm
index c8e529a..8fdb1de 100644 (file)
--- a/Aleph.pm
+++ b/Aleph.pm
@@ -59,13 +59,16 @@ sub usemap {{
 #      1016    => '',
 }};
 
+our $session_id;
 
 sub search {
        my ( $self, $query ) = @_;
 
        die "need query" unless defined $query;
 
-       my $url = 'http://161.53.240.197:8991/F?RN=' . int rand(1000000000);
+       $session_id ||= int rand(1000000000);
+       # FIXME allocate session just once
+       my $url = 'http://161.53.240.197:8991/F?RN=' . $session_id;
        # fake JavaScript code on page which creates random session
 
 diag "get $url";
@@ -77,9 +80,18 @@ diag "advanced search";
 
        $mech->follow_link( url_regex => qr/find-c/ );
 
+       my $database = $self->{database};
+
+       if ( $mech->content =~ m{Requested library is unavailable at the moment} ) {
+               warn "ERROR: default database not available, try to swith to $database\n";
+               $self->save_content;
+               $mech->follow_link( url_regex => qr/local_base=$database/i );
+               diag "re-try advanced search";
+               $mech->follow_link( url_regex => qr/find-c/ );
+       }
+
 diag "submit search [$query] on ", $self->{database};
 
-       $self->save_content;
 
        $mech->submit_form(
                fields => {
@@ -105,7 +117,7 @@ diag "in MARC format";
 
        $mech->follow_link( url_regex => qr/format=001/ );
 
-       return $hits;
+       return $self->{hits} = $hits;
 }
 
 
@@ -134,6 +146,7 @@ warn "parse $nr";
                sub field {
                        my ( $f, $v ) = @_;
                        $v =~ s/\Q \E/ /gs;
+                       $v =~ s/\s+$//gs;
 warn "## $f\t$v\n";
                        $hash->{$f} = $v;
 
@@ -170,9 +183,11 @@ warn "## ++ ", dump( $f, $i1, $i2, @sf );
 
                $self->save_marc( $id, $marc->as_usmarc );
 
-               $nr++;
-
-               $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
+               if ( $nr < $self->{hits} ) {
+                       $nr++;
+                       diag "follow link to next record $nr";
+                       $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
+               }
 
                return $marc->as_usmarc;
        } else {