#!/home/eric/bin/perl # images2mylibrary.pl - harvest images metadata and import it into MyLibrary # Eric Lease Morgan # 2007-06-29 - made in infomotions image gallery only; used their sets to create places terms # 2007-06-28 - added search for URL as a location # 2004-10-12 - first cut =head1 NAME images2mylibrary.pl - harvest image metadata and import it into MyLibrary =head1 DESCRIPTION The primary purpose of this program is to populate your MyLibrary database with records harvested from an OAI-PMH data repository. If it doesn't already exist, the program will automatically create a location type called URL. If it doesn't already exist, the program will automatically create a facet/term combination called Formats/Images. Only very basic meta-data is saved to the MyLibrary database like title, creator, description, and identifier. =head1 TODO This program includes many hard-coded values, and the program could be improved through a configuration section. =head1 AUTHOR Eric Lease Morgan =cut # include the necessary modules use MyLibrary::Core; use Net::OAI::Harvester; use strict; require 'subroutines.pl'; # display an introduction &clearScreen; print "\nIf it doesn't already exist, the program will create a location type of URL.\n"; print "If they don't exist, this script will then create a facet called Formats.\n"; print "It will then create term called Images. The script will then harvest the\n"; print "image metadata and import it into MyLibrary according.\n\n"; print "Press enter (or return) to begin. "; ; # define where the OAI interface to the image gallery is is use constant GALLERY => 'http://www.infomotions.com/gallery/oai/index.pl'; # look for a location type called URL my $location_type; foreach ( MyLibrary::Resource::Location::Type->all_types ) { my $type = MyLibrary::Resource::Location::Type->new( id => $_ ); if ( $type->name eq 'URL' ) { $location_type = $type->location_type_id; print "Location type URL exists\n"; last; } } # if not, then create it if ( ! $location_type ) { my $type = MyLibrary::Resource::Location::Type->new; $type->name('URL'); $type->description('Internet pointers'); $type->commit; $location_type = $type->location_type_id; print "Location type URL was created\n"; } # check for a facet called Formats my $facet = MyLibrary::Facet->new; if (! MyLibrary::Facet->get_facets(value => 'Formats', field => 'name')) { # create it $facet->facet_name('Formats'); $facet->facet_note('This list of list of physical items embodying information.'); $facet->commit; print "\nThe facet Formats was created.\n"; } else { # already exists $facet = MyLibrary::Facet->new(name => 'Formats'); print "\nThe facet Formats already exists.\n"; } my $facetID = $facet->facet_id; # check for a term named Images my $term = MyLibrary::Term->new; if (! MyLibrary::Term->get_terms(value => 'Images', field => 'name')) { # create it $term->term_name('Images'); $term->term_note('These are things like photographs or paintings.'); $term->facet_id($facetID); $term->commit; print "The term Images was created.\n"; } else { # it already exists $term = MyLibrary::Term->new(name => 'Images'); print "The term Images already exists.\n"; } my $imageTermID = $term->term_id; # check for a facet called Places my $facet = MyLibrary::Facet->new; if (! MyLibrary::Facet->get_facets(value => 'Places', field => 'name')) { # create it $facet->facet_name('Places'); $facet->facet_note('Geographic areas; physical locations'); $facet->commit; print "\nThe facet Places was created.\n"; } else { # already exists $facet = MyLibrary::Facet->new(name => 'Places'); print "\nThe facet Places already exists.\n"; } my $placesID = $facet->facet_id; # get and set the place terms/sets from the repository my $harvester = Net::OAI::Harvester->new('baseURL' => GALLERY); my $sets = $harvester->listSets; foreach ($sets->setSpecs) { # check for this particular term if (! MyLibrary::Term->get_terms(value => $sets->setName($_), field => 'name')) { # create it my $term = MyLibrary::Term->new; $term->term_name($sets->setName($_)); $term->term_note('This term comes from the Infomotions Image Gallery.'); $term->facet_id($placesID); $term->commit; print 'The term ', $sets->setName($_), " was created.\n"; } # it already exits else { print 'The term ', $sets->setName($_), " already exists.\n" } } # loop through each set from GALLERY foreach ($sets->setSpecs) { # get this set name print "\n$_\n"; my $term = MyLibrary::Term->new(name => $sets->setName($_)); my $termID = $term->term_id; # get the records in this set my $records = $harvester->listAllRecords(metadataPrefix => 'oai_dc', set => $_); # process each record while (my $record = $records->next) { my $FKey = $record->header->identifier; my $metadata = $record->metadata(); my $name = $metadata->title(); my @description = $metadata->description(); my $description = join (' ', @description); my @subject = $metadata->subject(); my $description .= join (' ', @subject); my $publisher = $metadata->publisher(); my $creator = $metadata->creator(); my $location = $metadata->identifier(); print "$name..."; # check to see if it already exits if (! MyLibrary::Resource->new(fkey => $FKey)) { # create it my $resource = MyLibrary::Resource->new; $resource->name($name); $resource->publisher($publisher); $resource->note($description); $resource->fkey($FKey); $resource->related_terms(new => [$termID, $imageTermID]); $resource->add_location(location => $location, location_type => $location_type); $resource->commit; print "added (", $resource->id, ").\n"; } else { # already got it print "already exists.\n"; } } } # done print "\nDone\n"; exit;