support multiple results
[Biblio-Z3950.git] / COBISS.pm
index 1de03c3..f3c698d 100644 (file)
--- a/COBISS.pm
+++ b/COBISS.pm
@@ -5,7 +5,6 @@ use strict;
 
 use WWW::Mechanize;
 use MARC::Record;
-use File::Slurp;
 
 binmode STDOUT, ':utf8';
 
@@ -76,21 +75,19 @@ sub search {
 
        my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
 
-diag "get $url";
+diag "get $url";
 
        $mech->get( $url );
 
-diag "got session";
+diag "got session";
 
        $mech->follow_link( text_regex => qr/union/ );
 
-diag "# switch to advanced form (select)";
-
-diag $mech->content;
+diag "switch to advanced form (select)";
 
        $mech->follow_link( url_regex => qr/mode=3/ );
 
-diag "submit search $query";
+diag "submit search $query";
 
        $mech->submit_form(
                fields => {
@@ -106,11 +103,11 @@ diag "# submit search $query";
                return;
        }
 
-diag "got $hits results, get first one";
+diag "got $hits results, get first one";
 
        $mech->follow_link( url_regex => qr/ukaz=DISP/ );
 
-diag "in COMARC format";
+diag "in COMARC format";
 
        $mech->follow_link( url_regex => qr/fmt=13/ );
 }
@@ -121,36 +118,37 @@ sub fetch_marc {
 
        my $comarc;
 
-       if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) {
+       if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
 
                my $comarc = $1;
                my $nr = $2;
                my $id = $3;
 
-diag "fetch_marc $nr [$id]";
+diag "fetch_marc $nr [$id]";
 
                $comarc =~ s{</?b>}{}gs;
                $comarc =~ s{<font[^>]*>}{<s>}gs;
                $comarc =~ s{</font>}{<e>}gs;
 
-               write_file "comarc/$id", $comarc;
+               open(my $out, '>:utf8', "comarc/$id");
+               print $out $comarc;
+               close($out);
 
                print $comarc;
 
                my $marc = MARC::Record->new;
 
                foreach my $line ( split(/[\r\n]+/, $comarc) ) {
-                       our @f;
 
                        if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
                                diag "SKIP: $line";
                        } else {
                                $line .= "<eol>";
 
-                               @f = ( $1, $2, $3 );
+                               our @f = ( $1, $2, $3 );
                                sub sf { push @f, @_; }
                                $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;
-                               diag "f:", join('|', @f), " left: |$line|";
+                               diag "f:", join('|', @f), " left: |$line|";
                                $marc->add_fields( @f );
                        }
                }
@@ -161,6 +159,9 @@ diag "# fetch_marc $nr [$id]";
 
                diag $marc->as_formatted;
 
+               $nr++;
+               $mech->follow_link( url_regex => qr/rec=$nr/ );
+
                return $marc->as_usmarc;
        } else {
                die "can't fetch COMARC format from ", $mech->content;