Skip to content

Instantly share code, notes, and snippets.

@MagnusEnger
Created September 6, 2016 11:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnusEnger/500262f88ac1216a1a02c0a767e7b65c to your computer and use it in GitHub Desktop.
Save MagnusEnger/500262f88ac1216a1a02c0a767e7b65c to your computer and use it in GitHub Desktop.
Simple script to turn CSV records into MARCXML, using Catmandu
#!/usr/bin/perl
# Copyright 2015 Magnus Enger Libriotech
=head1 NAME
dvd.pl - Turn CSV into MARC.
=head1 SYNOPSIS
perl dvd.pl -v
=cut
use Catmandu;
use String::Util qw( trim );
use Getopt::Long;
use Data::Dumper;
use Pod::Usage;
use Modern::Perl;
# Get options
my ( $input_file, $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',
file => $input_file,
);
my $exporter = Catmandu->exporter(
'MARC',
file => "dvd.marcxml",
type => "XML",
xml_declaration => 1,
collection => 1,
skip_empty_subfields => 1,
pretty => 1,
);
my $record_count = 0;
my $n = $importer->each(sub {
my $raw = $_[0];
my $data = {
record => [
['001', undef, undef, undef, $raw->{ 'Collection ID' } ],
['100', ' ', ' ', 'a', $raw->{ 'Director' }],
['245', ' ', ' ',
'a', $raw->{ 'Title' },
'c', $raw->{ 'Director' },
'h', 'DVD',
],
['260', ' ', ' ', 'a', $raw->{ 'Country' }],
# ['653', ' ', ' ', 'a', $raw->{ 'Genre' }], # Frie nøkkelord
['952', ' ', ' ',
'a', 'VULKAN',
'b', 'VULKAN',
'c', 'GEN',
'y', 'DVD',
],
],
};
# Add genres
my @genres = split /,/, $raw->{ 'Genre' };
foreach my $genre ( @genres ) {
$genre = trim( $genre );
push @{ $data->{ record } }, [ '653', ' ', ' ', 'a', $genre ];
}
# 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;
}
});
=head1 OPTIONS
=over 4
=item B<-i, --infile>
Name of input file.
=item B<-l, --limit>
Only process the n first somethings.
=item B<-v --verbose>
More verbose output.
=item B<-d --debug>
Even more verbose output.
=item B<-h, -?, --help>
Prints this help message and exits.
=back
=cut
sub get_options {
# Options
my $input_file = '';
my $limit = '',
my $verbose = '';
my $debug = '';
my $help = '';
GetOptions (
'i|infile=s' => \$input_file,
'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, $limit, $verbose, $debug );
}
=head1 AUTHOR
Magnus Enger, <magnus [at] libriotech.no>
=head1 LICENSE
This program 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 3 of the License, or
(at your option) any later version.
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment