join multi-line fields
[Biblio-Z3950.git] / COBISS.pm
index 47ba441..81b532e 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' ] },
@@ -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};
@@ -143,6 +141,9 @@ diag "fetch_marc $nr [$id] $format";
 
                my $marc = MARC::Record->new;
 
+               $comarc =~ s/[\r\n]+\s{5}//gs; # join continuation lines
+warn "## comarc join: $comarc\n";
+
                foreach my $line ( split(/[\r\n]+/, $comarc) ) {
 
                        if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
@@ -186,14 +187,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++;