blib/lib/App/NKC2MARC.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package App::NKC2MARC;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Getopt::Std;
10 use IO::Barf qw(barf);
11 use List::Util 1.33 qw(none);
12 use MARC::File::XML;
13 use MARC::Record;
14 use Readonly;
15 use ZOOM;
16  
17 Readonly::Array our @OUTPUT_FORMATS => qw(usmarc xml);
18  
19 our $VERSION = 0.03;
20  
21 $| = 1;
22  
23 # Constructor.
24 sub new {
25 my ($class, @params) = @_;
26  
27 # Create object.
28 my $self = bless {}, $class;
29  
30 # Object.
31 return $self;
32 }
33  
34 # Run.
35 sub run {
36 my $self = shift;
37  
38 # Process arguments.
39 $self->{'_opts'} = {
40 'h' => 0,
41 'o' => 'xml',
42 };
43 if (! getopts('ho:', $self->{'_opts'})
44 || $self->{'_opts'}->{'h'}
45 || @ARGV < 1) {
46  
47 print STDERR "Usage: $0 [-h] [-o output_format] [--version] id_of_book ..\n";
48 print STDERR "\t-h\t\t\tPrint help.\n";
49 print STDERR "\t-o output_format\tOutput format (usmarc, xml - default).\n";
50 print STDERR "\t--version\t\tPrint version.\n";
51 print STDERR "\tid_of_book ..\t\tIdentifier of book e.g. Czech ".
52 "national bibliography id or ISBN\n";
53 return 1;
54 }
55 my @book_ids = @ARGV;
56  
57 if (none { $self->{'_opts'}->{'o'} eq $_ } @OUTPUT_FORMATS) {
58 err 'Bad output format.',
59 'Output format', $self->{'_opts'}->{'o'},
60 ;
61 }
62  
63 # Configuration of National library of the Czech Republic service.
64 my $c = {
65 host => 'aleph.nkp.cz',
66 port => '9991',
67 database => 'NKC01',
68 record => 'usmarc'
69 };
70  
71 # ZOOM object.
72 my $conn = eval {
73 ZOOM::Connection->new(
74 $c->{'host'}, $c->{'port'},
75 'databaseName' => $c->{'database'},
76 );
77 };
78 if ($EVAL_ERROR) {
79 err "Cannot connect to '".$c->{'host'}."'.",
80 'Code', $EVAL_ERROR->code,
81 'Message', $EVAL_ERROR->message,
82 ;
83 }
84 $conn->option(preferredRecordSyntax => $c->{'record'});
85  
86 foreach my $book_id (@book_ids) {
87 # Get MARC record from library.
88 my ($rs, $ccnb);
89 ## CCNB
90 if ($book_id =~ m/^cnb\d+$/ms) {
91 $rs = $conn->search_pqf('@attr 1=48 '.$book_id);
92 if (! defined $rs || ! $rs->size) {
93 print STDERR encode_utf8("Edition with ČČNB '$book_id' doesn't exist.\n");
94 return 1;
95 }
96 $ccnb = $book_id;
97 ## ISBN
98 } else {
99 $rs = $conn->search_pqf('@attr 1=7 '.$book_id);
100 if (! defined $rs || ! $rs->size) {
101 print STDERR "Edition with ISBN '$book_id' doesn't exist.\n";
102 return 1;
103 }
104 }
105 my $raw_record = $rs->record(0)->raw;
106 my $usmarc = MARC::Record->new_from_usmarc($raw_record);
107 if (! defined $ccnb) {
108 $ccnb = $self->_subfield($usmarc, '015', 'a');
109 }
110 my $output_file;
111 if ($self->{'_opts'}->{'o'} eq 'xml') {
112 $output_file = $ccnb.'.xml';
113 my $marc_xml = encode_utf8($usmarc->as_xml);
114 barf($output_file, $marc_xml);
115 } else {
116 $output_file = $ccnb.'.mrc';
117 barf($output_file, $raw_record);
118 }
119  
120 print "MARC record for '".$book_id."' was saved to '$output_file'.\n";
121 }
122  
123 return 0;
124 }
125  
126 sub _subfield {
127 my ($self, $obj, $field, $subfield) = @_;
128  
129 my $field_value = $obj->field($field);
130 if (! defined $field_value) {
131 return;
132 }
133  
134 return $field_value->subfield($subfield);
135 }
136  
137 1;
138  
139  
140 __END__
↑ Summary