create liberated.marc file
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 12 Sep 2013 12:54:09 +0000 (14:54 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 12 Sep 2013 12:54:09 +0000 (14:54 +0200)
crolist2marc.pl

index ef4b056..fdb5402 100755 (executable)
@@ -13,7 +13,7 @@ sub csv_file {
        my ($file,$parse) = @_;
 
        print STDERR "# reading $file ";
-       my $lines = 0;
+       my $lines = 1;
 
        open(my $fh, '<', $file);
        my $h = <$fh>; # header
@@ -23,7 +23,7 @@ sub csv_file {
                $lines++;
 
                if ( ! $text ) {
-                       print STDERR "\nSKIP $lines [$_] ";
+                       print STDERR "\nSKIP $file +$lines [$_] ";
                        next;
                }
 
@@ -36,7 +36,7 @@ sub csv_file {
 
                $sfi =~ s/^\$// || die "can't fix subfield [$sfi]";
 
-               $text =~ tr/^~]}\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982
+               $text =~ tr/^~]}\\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982
 
                $data->{$idsl}->{$tag}->[ $tagno ]->[ 0 ] = $i1;
                $data->{$idsl}->{$tag}->[ $tagno ]->[ 1 ] = $i2;
@@ -72,7 +72,31 @@ csv_file( 'LONTAG.csv', sub {
 print STDERR "\n# getting all ids ";
 my @ids = keys %$data;
 print STDERR scalar(@ids), " found\n";
+
+my $marc_file = 'liberated.marc';
+open(my $marc_fh, '>:encoding(UTF-8)', $marc_file);
+my $number = 0;
+
 foreach my $id ( @ids ) {
-       print "# $id ",dump($data->{$id});
+       my $rec = MARC::Record->new;
+       $rec->encoding( 'UTF-8' );
+       foreach my $field ( sort keys %{ $data->{$id} } ) {
+               foreach my $arr ( @{ $data->{$id}->{$field} } ) {
+                       if ( ! $arr ) {
+                               print STDERR "SKIPPED $id $field ",dump( $data->{$id} );
+                               next;
+                       }
+                       $rec->add_fields( $field, @$arr );
+               }
+       }
+
+       #print $rec->as_formatted;
+       #print "# $id ",dump($data->{$id});
+
+       print $marc_fh $rec->as_usmarc;
+       $number++;
+       print "$number " if $number % 1000 == 0;
 }
 
+close($marc_fh);
+print "$marc_file ",-s $marc_file, " bytes\n";