#!/usr/local/bin/perl # ftppcatappr.pl # desc: get the most current approvals files from Promptcat # wordsettings: # end_autoz use strict; use Net::FTP; use Time::Local; use POSIX; use MARC::Batch; my ($ftp, $status, $line, $fopen, $pw, $file, $filecount, $plural); my (@rawlines, @linepieces, @fnamepieces, @unsorted, @sorted); ### constants my $inipath = "/usr/local/bin/"; my $inifile = "promptcat.ini"; my $filepath = "/m1/local/promptcat/approvals"; my $pfile = "$filepath/pcat.temp"; my $system = "edx.oclc.org"; my $user = "WMU1"; my $dir = "'edx.pcat.exw'"; my $xfermode = "b"; ### ftp mode my %modename; $modename{'a'} = "ascii"; # ASCII xfer mode name $modename{'b'} = "binary"; # Binary xfer mode name ### get todays date my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime; my $today = sprintf("%-2.2d%-2.2d%-2.2d", $year-100, $month+1, $day); my $yesterday = sprintf("%-4.4d.%-2.2d.%-2.2d", $year+1900, $month+1, $day); $yesterday = computedate($yesterday, -1); $yesterday =~ s/\.//g; $yesterday = substr($yesterday, 2); # get the password for the promptcat ftp site $fopen = sprintf("Cannot open %s/%s for input\n", $inipath, $inifile); open(inifile, "$inipath/$inifile") or die $fopen; $pw = ; close(inifile); chomp $pw; # create FTP object $ftp = Net::FTP->new($system); if (!defined($ftp)) {die "Can't create FTP object: $@\n";} # login to promptcat if ($ftp->login($user, $pw) == 0) {die "Can't login to <$system>\n";} # go to the desired directory if ($ftp->cwd($dir) == 0) {die "Can't change directory to <$dir>\n";} # set file mode $status = ($xfermode eq "a") ? $ftp->ascii : $ftp->binary; if (!defined($status)) {die "Can't set file mode to <$modename{$xfermode}>\n";} # get list of files in our directory @rawlines = $ftp->dir(""); foreach $line (@rawlines) # check each file at the ftp site { # reduce to single space as separator $line =~ s/\s\s*/ /g; @linepieces = split / /, $line; # last piece in the line is the filename @fnamepieces = split /\./, $linepieces[scalar(@linepieces-1)]; # get yesterday's approval file(s) if (($fnamepieces[0] eq 'RCD') and ($fnamepieces[2] =~ /^Z/) and ($fnamepieces[3] eq "D$yesterday") and ($line =~ /APPR$/)) { push @unsorted, $linepieces[scalar(@linepieces-1)]; } } # file names are of this type: RCD.BWX.Znnnnnnn.Dyymmdd.APPR # sort them implicitly in Z order @sorted = sort{$a cmp $b} @unsorted; # prep things for processing or stop if no files $filecount = scalar(@sorted); $plural = 's'; unless ($filecount == 1) {$plural = '';} if ($filecount == 0) { print "\nNo files found for yesterday\n"; exit(0); } # get the file(s) print "\nfile$plural of interest:\n"; foreach $line (@sorted) {print "$line\n";} foreach $file (@sorted) { $status = $ftp->get($file); if (defined($status)) { print "File $file retrieved\n"; $filecount--; } } $ftp->quit; if ($filecount != 0) { print "Not all files retrieved -- aborting\n"; exit(0); } # process the file(s) foreach $file (@sorted) { print "\n===== begin processing file: $file =====\n"; processfile($file, $pfile); system("$filepath/oclc980.pl $pfile"); system("$filepath/importall.sh"); system("mv $filepath/*.marc $filepath/loaded"); system("mv $filepath/*.imp $filepath/loaded"); system("mv $filepath/*.preimp $filepath/loaded"); unlink $pfile; print "\n===== end processing file: $file =====\n"; } system("mv $filepath/RCD* $filepath/loaded"); sub processfile # input: MARC file # output: MARC file # process: removed unwanted 6xx, 938, and 948 fields # do some editing of the 856 { my ($infile, $outfile) = @_; # read input file into "array" my $batch = MARC::Batch->new('USMARC', "$infile"); open (outfile, ">$outfile") or die "Cannot open $outfile for output\n"; while (my $record = $batch->next()) { my $field948 = $record->field('948'); my @field6xxs = $record->field('6..'); my @field856s = $record->field('856'); my @field938s = $record->field('938'); foreach my $field6xx (@field6xxs) { if ($field6xx and $field6xx->indicator(2) ge '4' and $field6xx->indicator(2) le '8') { $record->delete_field($field6xx); } } foreach my $field (@field856s) { if ($field and !$field->subfield('3')) { if ($field->as_string() =~ /\-t\.html/i or $field->as_string() =~ /\/toc\//i) { $field->update(3=>'Table of contents only'); } if ($field->as_string() =~ /\-b\.html/i) { $field->update(3=>'Contributor biographical information'); } if ($field->as_string() =~ /\-d\.html/i) { $field->update(3=>'Publisher description'); } if ($field->as_string() =~ /\-s\.html/i) { $field->update(3=>'Sample text'); } } } foreach my $field938 (@field938s) { if ($field938) {$record->delete_field($field938);} } if ($field948) {$record->delete_field($field948);} print outfile $record->as_usmarc(); } #end while $batch close (outfile); } sub computedate # input: date YYYY.MM.DD, days # output: date result of input date + days { my ($cdate, $delta) = @_; # in case of incoming blank date if (($cdate eq '') or ($cdate eq ' ')) {return ' . . ';} my $day = substr($cdate, 8, 2); my $month = substr($cdate, 5, 2) - 1; my $year = substr($cdate, 0, 4); my ($sec, $min, $hr) = 10; # convert for math, do math, and convert back my $ctime = timelocal($sec, $min, $hr, $day, $month, $year); $ctime += $delta * 86400; # add/subtract specified number of days ($sec, $min, $hr, $day, $month, $year) = localtime($ctime); my $dateout = sprintf ("%4.4d.%2.2d.%2.2d", $year+1900, $month+1, $day); return $dateout; }