move save_marc to Scraper
[Biblio-Z3950.git] / Aleph.pm
index f89aaf7..4c27f5a 100644 (file)
--- a/Aleph.pm
+++ b/Aleph.pm
@@ -3,17 +3,13 @@ package Aleph;
 use warnings;
 use strict;
 
-use WWW::Mechanize;
 use MARC::Record;
 use Data::Dump qw/dump/;
 
-binmode STDOUT, ':utf8';
-
-our $mech = WWW::Mechanize->new();
-our $hits;
+use base 'Scraper';
 
 sub diag {
-       print "# ", @_, $/;
+       warn "# ", @_, $/;
 }
 
 # Koha Z39.50 query:
@@ -53,9 +49,7 @@ sub diag {
 # WGA - Riječi u geografskim odrednicama 
 # WYR - Godina izdavanja
 
-our $usemap = {
-#      8               => '',
-#      7               => '',
+sub usemap {{
        4               => 'WTI',
        1003    => 'WTI',
        16              => 'CU',
@@ -63,13 +57,8 @@ our $usemap = {
 #      12              => '',
 #      1007    => '',
 #      1016    => '',
+}};
 
-};
-
-sub usemap {
-       my $f = shift || die;
-       $usemap->{$f};
-}
 
 sub search {
        my ( $self, $query ) = @_;
@@ -81,6 +70,7 @@ sub search {
 
 diag "get $url";
 
+       my $mech = $self->{mech} || die "no mech?";
        $mech->get( $url );
 
 diag "advanced search";
@@ -95,8 +85,8 @@ diag "submit search $query";
                },
        );
 
-       $hits = 0;
-       if ( $mech->content =~ m{ukupno\s+(\d+).*(do\s+(\d+))}s ) {
+       my $hits = 0;
+       if ( $mech->content =~ m{ukupno\s+(\d+).*do\s+(\d+)}s ) {
                $hits = $1;
                $hits = $2 if $2 && $2 < $1; # correct for max. results
        } else {
@@ -111,12 +101,20 @@ diag "got $hits results, get first one";
 diag "in MARC format";
 
        $mech->follow_link( url_regex => qr/format=001/ );
+
+       return $hits;
 }
 
 
+our ( $hash, $marc );
+
 sub next_marc {
        my ($self,$format) = @_;
 
+       $format ||= 'marc';
+
+       my $mech = $self->{mech} || die "no mech?";
+
 print $mech->content;
 
        if ( $mech->content =~ m{Zapis\s+(\d+)}s ) {
@@ -125,10 +123,10 @@ print $mech->content;
 
 diag "parse $nr";
 
-               my $marc = MARC::Record->new;
+               $marc = MARC::Record->new;
+               $hash = {};
 
                my $html = $mech->content;
-               my $hash;
 
                sub field {
                        my ( $f, $v ) = @_;
@@ -145,26 +143,14 @@ diag "sf = ", dump(@sf);
                }
 
                $html =~ s|<tr>\s*<td class=td1 id=bold[^>]*>(.+?)</td>\s*<td class=td1>(.+?)</td>|field($1,$2)|ges;
-               diag dump($hash);
+               diag "# hash ",dump($hash);
 
                my $id = $hash->{SYS} || die "no SYS";
 
-
-
-               my $path = "marc/$id.$format";
-
-               open(my $out, '>:utf8', $path);
-               print $out $marc->as_usmarc;
-               close($out);
-
-               diag "created $path ", -s $path, " bytes";
-
-               diag $marc->as_formatted;
+               $self->save_marc( $id, $marc->as_usmarc );
 
                $nr++;
 
-               die if $nr == 3; # FIXME
-
                $mech->follow_link( url_regex => qr/set_entry=0*$nr/ );
 
                return $marc->as_usmarc;