use save_marc
[Biblio-Z3950.git] / COBISS.pm
index e55eeaf..c324898 100644 (file)
--- a/COBISS.pm
+++ b/COBISS.pm
@@ -3,11 +3,10 @@ package COBISS;
 use warnings;
 use strict;
 
-use WWW::Mechanize;
 use MARC::Record;
 use Data::Dump qw/dump/;
 
-binmode STDOUT, ':utf8';
+use base 'Scraper';
 
 my $cobiss_marc21 = {
        '010' => { a => [ '020', 'a' ] },
@@ -17,9 +16,9 @@ my $cobiss_marc21 = {
        },
         205  => { a => [  250 , 'a' ] },
         210  => {
-               a => [  250 , 'a' ],
-               c => [  250 , 'b' ],
-               d => [  250 , 'c' ],
+               a => [  260 , 'a' ],
+               c => [  260 , 'b' ],
+               d => [  260 , 'c' ],
        },
        215 => {
                a => [  300 , 'a' ],
@@ -31,9 +30,6 @@ my $cobiss_marc21 = {
        },
 };
 
-our $mech = WWW::Mechanize->new();
-our $hits;
-
 sub diag {
        print "# ", @_, $/;
 }
@@ -51,7 +47,7 @@ sub diag {
 # @attr 1=1007 standard-id 
 # @attr 1=1016 any
 
-our $usemap = {
+sub usemap {{
        8               => 'BN',        # FIXME check
        7               => 'SN',        # FIXME check
        4               => 'TI',
@@ -62,12 +58,7 @@ our $usemap = {
 #      1007    => '',
 #      1016    => '',
 
-};
-
-sub usemap {
-       my $f = shift || die;
-       $usemap->{$f};
-}
+}};
 
 sub search {
        my ( $self, $query ) = @_;
@@ -78,6 +69,9 @@ sub search {
 
 diag "get $url";
 
+       my $mech = $self->{mech} || die "no mech?";
+
+       my $hits;
        $mech->get( $url );
 
 diag "got session";
@@ -111,12 +105,16 @@ diag "got $hits results, get first one";
 diag "in COMARC format";
 
        $mech->follow_link( url_regex => qr/fmt=13/ );
+
+       return $hits;
 }
 
 
-sub fetch_rec {
+sub next_marc {
        my ($self,$format) = @_;
 
+       my $mech = $self->{mech} || die "no mech?";
+
        $format ||= 'unimarc';
 
        die "unknown format: $format" unless $format =~ m{(uni|us)marc};
@@ -186,14 +184,7 @@ diag "fetch_marc $nr [$id] $format";
                        }
                }
 
-               my $path = "marc/$id.$format";
-
-               open($out, '>:utf8', $path);
-               print $out $marc->as_usmarc;
-               close($out);
-
-               diag "created $path ", -s $path, " bytes";
-
+               $self->save_marc( $id, $marc->as_usmarc );
                diag $marc->as_formatted;
 
                $nr++;