cleanup code a bit
[koha-eprints] / tsv2eprints.pl
index d49fd95..7c9ea89 100755 (executable)
@@ -18,24 +18,39 @@ use utf8;
 use Encode;
 use Data::Dump qw(dump);
 use Storable;
-
-=for eprints-api
+use LWP::Simple;
 
 use EPrints;
-my $institution = 'Grafički fakultet';
 
 my $ep = EPrints->new();
-my $repo = $ep->repository( 'grf' );
-$repo->{config}->{enable_file_imports} = 1;
-$repo->{config}->{enable_web_imports} = 1;
-
+my $repo = $ep->repository( 'snz' );
+#$repo->{config}->{enable_file_imports} = 1;
+#$repo->{config}->{enable_web_imports} = 1;
 my $dataset = $repo->dataset( 'eprint' );
 my $list = $dataset->search;
 my $count = $list->count;
 warn "# found [$count] eprints\n";
 
-my $eprint = $dataset->dataobj( 21 );
-warn dump( $eprint->get_value('institution'), $institution );
+warn ref( $list );
+
+#warn "# ids = ",dump( $list->ids );
+
+my $info = { count => 0 };
+$list->map( sub {
+       my( $session, $dataset, $eprint, $info ) = @_;
+
+       my $biblionumber = $eprint->get_value('biblionumber');
+
+       $info->{biblionumber}->{$biblionumber}++;
+       $info->{count}++;
+
+}, $info );
+warn dump( $info );
+
+=for update
+
+#my $eprint = $dataset->dataobj( 21 );
+#warn dump( $eprint->get_value('institution'), $institution );
 
 warn dump( $eprint );
 
@@ -49,8 +64,6 @@ $repo->terminate();
 
 =cut
 
-our $eprintid = 1;
-
 my $files;
 
 my $mkp_path = "/mnt/share/MKP/ELEKTRONIČKI DOKUMENTI/EL.DOKUMENTI PO BIBLIOBROJU/";
@@ -88,6 +101,7 @@ my $last_offset = 0;
 my @cols;
 
 while(<$tsv_fh>) {
+       chomp;
        my $line = $_;
        $line =~ s/[\n\r]+$//;
 
@@ -96,25 +110,33 @@ while(<$tsv_fh>) {
                next;
        }
 
-       my @v = split(/\t/, $_, $#cols + 1);
+       my @v = split(/\t/, $line, $#cols + 1);
        my %row;
        @row{@cols} = @v;
-warn "## row = ",dump( \%row );
+#warn "## row = ",dump( \%row );
 
        my $offset = $row{offset} // die "no offset";
        my $biblionumber = $row{biblionumber} || die "no biblionumber";
 
-       warn "# ", join(' ', map { $row{$_} } qw(offset biblionumber title)), "\n";
-
-       exit if $ENV{LAST} && $eprintid >= $ENV{LAST};
+#      warn "# ", join(' ', map { $row{$_} } qw(biblionumber title)), "\n";
 
        if ( delete $files->{$biblionumber} ) {
                $stat->{file}++;
 
-               seek $marc_fh, $last_offset, 0;
-               read $marc_fh, my $marc, $offset - $last_offset;
-               print $import_fh $marc;
-               warn "# marc $biblionumber\n";
+               if ( $info->{biblionumber}->{$biblionumber} ) {
+                       $stat->{existing}++;
+                       warn "EXISTING $biblionumber found in eprints\n";
+               } else {
+
+                       $stat->{new}++;
+
+                       seek $marc_fh, $last_offset, 0;
+                       read $marc_fh, my $marc, $offset - $last_offset;
+                       print $import_fh $marc;
+                       warn "# NEW ", join(' ', map { $row{$_} } qw(biblionumber title)), "\n";
+#                      warn "# NEW $biblionumber\n";
+
+               }
 
        } else {
                $stat->{missing}++;
@@ -126,4 +148,21 @@ warn "## row = ",dump( \%row );
 
 warn "# files left ", dump($files);
 
+foreach my $biblionumber ( keys %$files ) {
+
+       if ( $info->{biblionumber}->{$biblionumber} ) {
+               $stat->{existing}++;
+               warn "EXISTING $biblionumber found in eprints\n";
+               next;
+       }
+
+       if ( my $marc = get("https://koha.ffzg.hr/cgi-bin/koha/opac-export.pl?op=export&bib=$biblionumber&format=utf8") ) {
+               print $import_fh $marc;
+               warn "## marc $biblionumber from koha!";
+               $stat->{koha}++;
+       } else {
+               warn "ERROR: can't fetch $biblionumber from koha";
+       }
+}
+
 warn "# stat ", dump($stat);