use save_marc
[Biblio-Z3950.git] / COBISS.pm
index f3c698d..c324898 100644 (file)
--- a/COBISS.pm
+++ b/COBISS.pm
@@ -3,10 +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' ] },
@@ -16,7 +16,7 @@ my $cobiss_marc21 = {
        },
         205  => { a => [  250 , 'a' ] },
         210  => {
-               a => [  250 , 'a' ],
+               a => [  260 , 'a' ],
                c => [  260 , 'b' ],
                d => [  260 , 'c' ],
        },
@@ -30,9 +30,6 @@ my $cobiss_marc21 = {
        },
 };
 
-our $mech = WWW::Mechanize->new();
-our $hits;
-
 sub diag {
        print "# ", @_, $/;
 }
@@ -50,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',
@@ -61,12 +58,7 @@ our $usemap = {
 #      1007    => '',
 #      1016    => '',
 
-};
-
-sub usemap {
-       my $f = shift || die;
-       $usemap->{$f};
-}
+}};
 
 sub search {
        my ( $self, $query ) = @_;
@@ -77,6 +69,9 @@ sub search {
 
 diag "get $url";
 
+       my $mech = $self->{mech} || die "no mech?";
+
+       my $hits;
        $mech->get( $url );
 
 diag "got session";
@@ -110,11 +105,19 @@ diag "got $hits results, get first one";
 diag "in COMARC format";
 
        $mech->follow_link( url_regex => qr/fmt=13/ );
+
+       return $hits;
 }
 
 
-sub fetch_marc {
-       my ($self) = @_;
+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};
 
        my $comarc;
 
@@ -124,7 +127,7 @@ sub fetch_marc {
                my $nr = $2;
                my $id = $3;
 
-diag "fetch_marc $nr [$id]";
+diag "fetch_marc $nr [$id] $format";
 
                $comarc =~ s{</?b>}{}gs;
                $comarc =~ s{<font[^>]*>}{<s>}gs;
@@ -143,20 +146,45 @@ diag "fetch_marc $nr [$id]";
                        if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
                                diag "SKIP: $line";
                        } else {
+                               our @f = ( $1, $2, $3 );
                                $line .= "<eol>";
 
-                               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|";
-                               $marc->add_fields( @f );
+                               if ( $format eq 'unimarc' ) {
+
+                                       diag dump(@f), "line: $line";
+                                       sub sf_uni {
+                                               warn "sf ",dump(@_);
+                                               push @f, @_;
+                                       }
+                                       $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges;
+                                       diag "f:", dump(@f), " left: |$line|";
+                                       $marc->add_fields( @f );
+
+                               } elsif ( $format eq 'usmarc' ) {
+
+                                       my ( $f, $i1, $i2 ) = @f;
+
+                                       our $out = {};
+
+                                       sub sf_us {
+                                               my ($f,$sf,$v) = @_;
+                                               if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
+                                                       push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
+                                               }
+                                               return;
+                                       }
+                                       $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges;
+
+                                       diag "converted marc21 ",dump( $out );
+
+                                       foreach my $f ( keys %$out ) {
+                                               $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
+                                       }
+                               }
                        }
                }
 
-               open(my $out, '>:utf8', "marc/$id");
-               print $out $marc->as_usmarc;
-               close($out);
-
+               $self->save_marc( $id, $marc->as_usmarc );
                diag $marc->as_formatted;
 
                $nr++;