adding bad-XML catch and error report
[migration-tools.git] / spit_unicorn_marc_holdings.pl
1 #!/usr/bin/perl
2 use open ':utf8';
3 use MARC::Batch;
4 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
5 use MARC::Field;
6 use Unicode::Normalize;
7
8 my $count = 0;
9
10 binmode(STDOUT, ':utf8');
11 binmode(STDIN, ':utf8');
12
13 print join("\t",
14     "bib id",
15     "library",
16     "barcode",
17     "current location",
18     "home location",
19     "call number",
20     "item type",
21     "acq date",
22     "price",
23     "circulate flag",
24     "total charges",
25     "cat1",
26     "cat2"
27 ) . "\n";
28
29 foreach my $argnum ( 0 .. $#ARGV ) {
30
31         print STDERR "Processing " . $ARGV[$argnum] . "\n";
32
33     my $M;
34     open $M, '<:utf8', $ARGV[$argnum];
35     my $batch = MARC::Batch->new('XML',$M);
36
37         $batch->strict_off();
38         $batch->warnings_off();
39
40         while ( my $record = $batch->next() ) {
41
42         $count++;
43
44                 print STDERR "WARNINGS: Record $count : " .  join(":",@warnings) . " : continuing...\n" if ( @warnings );
45         my $my_903a = $record->field('903')->subfield('a'); # target bib id's here
46         my @tags = $record->field('999');
47         foreach my $tag ( @tags ) {
48             print join("\t",
49                 $my_903a,
50                 $tag->subfield('m') || '', # library
51                 $tag->subfield('i') || '', # barcode
52                 $tag->subfield('k') || '', # current location
53                 $tag->subfield('l') || '', # home location
54                 $tag->subfield('a') || '', # call number
55                 $tag->subfield('t') || '', # item type
56                 $tag->subfield('u') || '', # acq date
57                 $tag->subfield('p') || '', # price
58                 $tag->subfield('r') || '', # circulate flag
59                 $tag->subfield('n') || '', # total charges
60                 $tag->subfield('x') || '', # cat1
61                 $tag->subfield('z') || ''  # cat2
62             ) . "\n";
63         }
64
65         }
66         print STDERR "Processed $count records\n";
67 }