import only new files, get marc from koha if missing
[koha-eprints] / tsv2eprints.pl
index 254f51d..a275fa4 100755 (executable)
@@ -20,23 +20,37 @@ use Data::Dump qw(dump);
 use Storable;
 use LWP::Simple;
 
-=for eprints-api
-
 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 );
 
@@ -100,7 +114,7 @@ while(<$tsv_fh>) {
        my @v = split(/\t/, $_, $#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";
@@ -112,10 +126,18 @@ warn "## row = ",dump( \%row );
        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 "# marc $biblionumber\n";
+               }
 
        } else {
                $stat->{missing}++;
@@ -128,6 +150,13 @@ 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!";