moving stuff, again
[migration-tools.git] / ils-specific / spit_spectrum_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 $filetype = $ARGV[0]; # XML or USMARC
9 my $filename = $ARGV[1]; 
10 if (! $filetype && ! $filename ) { die "./script <USMARC or XML> <filename>\n"; }
11
12 my $count = 0;
13
14 binmode(STDOUT, ':utf8');
15 binmode(STDIN, ':utf8');
16
17 print join("\t",
18     "bib_id",
19     "bib001",
20     "material_number",
21     "call_number",
22     "copy_location",
23     "price_and_vendor",
24     "subf_b",
25     "subf_x",
26     "bib961u"
27 ) . "\n";
28
29
30 print STDERR "Processing " . $ARGV[$argnum] . "\n";
31
32 my $M;
33 open $M, '<:utf8', $ARGV[1];
34 my $batch = MARC::Batch->new($ARGV[0],$M);
35
36 $batch->strict_off();
37 $batch->warnings_off();
38
39 while ( my $record = $batch->next() ) {
40
41     $count++;
42
43     print STDERR "WARNINGS: Record $count : " .  join(":",@warnings) . " : continuing...\n" if ( @warnings );
44     my $my_903 = $record->field('903');
45     my $my_903a = $my_903 ? $my_903->subfield('a') : ''; # target bib id's here
46     my @my_961_tags = $record->field('961'); my $bib961u = '';
47     foreach my $my_961 ( @my_961_tags ) {
48         my @subfield_u = $my_961->subfield('u');
49         foreach my $u ( @subfield_u ) {
50             $bib961u .= $u . "|";
51         } 
52     }
53     $bib961u =~ s/\|$//;
54     my @tags = $record->field('852');
55     foreach my $tag ( @tags ) {
56         if ($tag->subfield('p')) { # if material_number
57                 print join("\t",
58                     $my_903a, # bib id
59                     $record->field('001') ? $record->field('001')->as_string() : '', #bib001
60                     $tag->subfield('p') || '', # material_number
61                     $tag->subfield('h') || '', # call_number
62                     $tag->subfield('c') || '', # copy_location
63                     $tag->subfield('9') || '', # price_and_vendor
64                     $tag->subfield('b') || '', # subf_b
65                     $tag->subfield('x') || '', # subf_x
66                     $bib961u # bib961u
67                 ) . "\n";
68         }
69     }
70
71 }
72 print STDERR "Processed $count records\n";