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