#!/usr/bin/perl

# get-mbooks.pl - get a bunch of MBooks OAI records and save them to a file

# Eric Lease Morgan <emorgan@nd.edu>
# December 15, 2007 - added set spec and binmode; got all records
# December 14, 2007 - Thanks Ed!


# require/include
use Net::OAI::Harvester;
use MARC::File::SAX;
use strict;

# define
use constant OUTPUT => './mbooks.marc';
use constant OAIURL => 'http://quod.lib.umich.edu/cgi/o/oai/oai';

# initialize
$| = 1;
my $harvester =  Net::OAI::Harvester->new( baseURL => OAIURL );
open OUT, ' > ' . OUTPUT or die "Can't open output: $!\n";
binmode OUT, 'utf8';

# get a bunch o' records
my $response  = $harvester->listAllRecords(

        metadataPrefix  => 'marc21',
        metadataHandler => 'MARC::File::SAX',
        set => 'mbooks:pd'

);

# process each record
my $index = 0;
while ( my $record = $response->next ) {

        # save it to a file
        $index++;
        print "Processing record $index\r";
        print OUT $record->metadata()->record()->as_usmarc

}

# clean up
print "\nDone\n";
close OUT;
exit;

