Skip to content

Instantly share code, notes, and snippets.

@MagnusEnger
Created April 2, 2014 07:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnusEnger/9929280 to your computer and use it in GitHub Desktop.
Save MagnusEnger/9929280 to your computer and use it in GitHub Desktop.
Tab separated to MARC via Catmandu
#!/usr/bin/perl
# Copyright 2013 Magnus Enger Libriotech
=head1 NAME
tab2marc.pl - Convert some tab-delimited data to MARCXML.
=head1 SYNOPSIS
$ perl csv2marc.pl -i records_tab.txt -v
To pretty-print the output:
$ perl csv2marc.pl -i records_tab.txt | xmllint --format -
=head1 NOTES
Some of the fields in the input file were given names after the
MARC fields they should go into, like 300a. This could probably
have been used to shove the data into the right MARC field
autmagically, but that was not attempted for this project.
Maybe next time?
=cut
use Catmandu::Importer::CSV;
use Catmandu::Exporter::MARC;
use Getopt::Long;
use Data::Dumper;
use Pod::Usage;
use Modern::Perl;
# Get options
my ( $input_file, $koha, $limit, $verbose, $debug ) = get_options();
# Check that the file exists
if ( !-e $input_file ) {
print "The file $input_file does not exist...\n";
exit;
}
my $importer = Catmandu::Importer::CSV->new(
file => $input_file,
sep_char => "\t",
);
my $exporter = Catmandu::Exporter::MARC->new(
# file => "kurdlib.marcxml",
type => "XML",
xml_declaration => 1,
collection => 1,
skip_empty_subfields => 1,
);
my $record_count = 0;
my $n = $importer->each(sub {
# Get the record
my $rec = $_[0];
# say Dumper $rec;
# say "***|" . $rec->{ 'genre' } . "|";
# Extract the "date entered on file"
my $date_entered = " ";
if ( $rec->{ '008/00-05_skapat datum' } && $rec->{ '008/00-05_skapat datum' } =~ m/\d\d(\d\d)-(\d\d)-(\d\d)/ ) {
$date_entered = "$1$2$3";
}
# Extract the "date of last transaction"
my $last_transaction = " ";
if ( $rec->{ '000_andrat_datum' } && $rec->{ '000_andrat_datum' } =~ m/(\d\d\d\d)-(\d\d)-(\d\d)/ ) {
$last_transaction = "$1$2$3";
}
# 007/00
my $p007_00 = 't';
if ( $rec->{ '008/18-34_format' } =~ m/digitised/i || $rec->{ '008/18-34_format' } =~ m/E-bok/i ) {
$p007_00 = 'c';
}
# Build up the record
my $data = {
record => [
['001', undef, undef, undef, $record_count+1],
['005', undef, undef, undef, $last_transaction],
['005', undef, undef, undef, $p007_00 . " " x 21],
['008', undef, undef, undef, $date_entered . " " x 34],
['020', ' ', ' ', 'a', $rec->{ '020a' }],
['035', ' ', ' ', 'a', $rec->{ 'ID-nummer' }],
['041', ' ', ' ',
'a', $rec->{ '041a1' },
'a', $rec->{ '041a2' },
'a', $rec->{ '041a3' },
'b', $rec->{ '041b' },
'h', $rec->{ '041h1' },
'h', $rec->{ '041h2' },
],
['084', ' ', ' ', 'a', $rec->{ '084a' }],
['100', ' ', ' ', 'a', $rec->{ '100a' }],
['240', ' ', ' ',
'a', $rec->{ '240a' },
'l', $rec->{ '240l' },
],
['245', ' ', ' ',
'a', $rec->{ '245a' },
'b', $rec->{ '245b' },
'c', $rec->{ '245c' },
'h', $rec->{ '245h' },
],
['250', ' ', ' ', 'a', $rec->{ '250a' }],
['260', ' ', ' ',
'a', $rec->{ '260a' },
'b', $rec->{ '260b' },
'c', $rec->{ '260c' },
'e', $rec->{ 'Tryckeri_land' },
'f', $rec->{ 'Tryckeri_namn' },
],
['300', ' ', ' ',
'a', $rec->{ '300a' },
'b', $rec->{ '300b' },
'e', $rec->{ '300e' },
],
['500', ' ', ' ', 'a', $rec->{ '500a' }],
['502', ' ', ' ', 'a', $rec->{ '502' }],
['504', ' ', ' ', 'a', $rec->{ '504' }],
['533', ' ', ' ',
'a', $rec->{ '533a' },
'b', $rec->{ '533b' },
'c', $rec->{ '533c' },
'd', $rec->{ '533d' },
],
['546', ' ', ' ', 'a', $rec->{ '546' }],
['653', '0', '6', 'a', $rec->{ 'sjanger' }],
['700', ' ', ' ',
'a', $rec->{ '700a1' },
'4', $rec->{ '70041' },
],
['700', ' ', ' ',
'a', $rec->{ '700a2' },
'4', $rec->{ '70042' },
],
['700', ' ', ' ',
'a', $rec->{ '700a3' },
'4', $rec->{ '70043' },
],
['700', ' ', ' ',
'a', $rec->{ '700a4' },
'4', $rec->{ '70044' },
],
['710', ' ', ' ', 'a', $rec->{ '710a' }],
['856', ' ', ' ',
'u', $rec->{ '856u' },
'z', $rec->{ '856z' },
],
],
};
# Add fields that should only be used by Koha
if ( $koha ) {
# Add notes
push $data->{'record'},
['500', ' ', ' ', 'a', e2u( $rec->{ 'Originalitet' }, "Originalitet: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Antal i upplaga' }, "Antal i upplaga: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Anskaffning' }, "Anskaffning: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Gåvagivare' }, "Gåvagivare: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Anteckningar' }, "Anteckningar: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Genre' }, "Genre: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Katalogisör' }, "Katalogisör: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Ej ISBN beteckning' }, "Ej ISBN beteckning: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ 'Placering' }, "Placering: " )],
['500', ' ', ' ', 'a', e2u( $rec->{ '008/15-17_utgivningsland' }, "Utgivningsland: " )],
['546', ' ', ' ', 'a', e2u( $rec->{ 'Dialekt_01' }, "Dialekt: " )],
['546', ' ', ' ', 'a', e2u( $rec->{ 'Dialekt_02' }, "Dialekt: " )],
['546', ' ', ' ', 'a', e2u( $rec->{ 'Alfabet_01' }, "Alfabet: " )],
['546', ' ', ' ', 'a', e2u( $rec->{ 'Alfabet_02' }, "Alfabet: " )],
['546', ' ', ' ', 'a', e2u( $rec->{ 'Originalalfabet' }, "Originalalfabet: " )],
['546', ' ', ' ', 'a', e2u( $rec->{ 'Oversettningvia' }, "Översättning via: " )];
# Add items according to the Koha frameworks
if ( $rec->{ 'Antal i biblioteket' } ) {
for ( 1..$rec->{ 'Antal i biblioteket' } ) {
push @{ $data->{ record } }, [ '952', ' ', ' ', 'a', 'KURDLIB', 'b', 'KURDLIB' ];
}
}
}
# Add the record to the exporter
$exporter->add($data);
# Count processed records and bail out if we reached the limit
$record_count++;
if ( $limit && $record_count == $limit ) {
last;
}
});
$exporter->commit;
say "$n records found" if $verbose;
say "$record_count records processed" if $verbose;
sub e2u {
my ( $s, $prefix ) = @_;
if ( $s && $prefix ) {
return "$prefix$s";
}
}
=head1 OPTIONS
=over 4
=item B<-i, --infile>
Name of input file.
=item B<-l, --limit>
Only process the n first records.
=item B<-v --verbose>
More output.
=item B<-d --debug>
Even more output.
=item B<-h, -?, --help>
Prints this help message and exits.
=back
=cut
sub get_options {
# Options
my $input_file = '';
my $koha = '';
my $limit = '',
my $verbose = '';
my $debug = '';
my $help = '';
GetOptions (
'i|infile=s' => \$input_file,
'k|koha' => \$koha,
'l|limit=i' => \$limit,
'v|verbose' => \$verbose,
'd|debug' => \$debug,
'h|?|help' => \$help
);
pod2usage( -exitval => 0 ) if $help;
pod2usage( -msg => "\nMissing Argument: -i, --infile required\n", -exitval => 1 ) if !$input_file;
return ( $input_file, $koha, $limit, $verbose, $debug );
}
=head1 LICENSE
This is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment