Annotation of nsdl/nsdlloncapaorg/harvester.pl, revision 1.5

1.1       www         1: #!/usr/local/bin/perl
                      2: 
                      3: #
                      4: # lon-capa.pl
                      5: # Parse the LON-CAPA metadata
                      6: #
                      7: # Andy Dong <adong@smete.org> 10/23/2002
                      8: #
                      9: # Contact Gerd Kortemeyer (korte@lite.msu.edu)
                     10: 
                     11: use strict;
                     12: use LWP::UserAgent;
                     13: use Getopt::Std;
1.2       www        14: use Digest::MD5 qw(md5_hex);
1.4       www        15: use IO::File;
                     16: 
                     17: my $basepath='/home/httpd/cgi-bin/OAI-XMLFile/XMLFile/nsdlexport/data';
1.1       www        18: 
                     19: my $pub_month;
                     20: my $pub_year;
                     21: my @loncapa;
                     22: 
                     23: # HTTP requests
                     24: 
                     25: my $content;
                     26: my $content_regex = 'File Not Found';
                     27: 
                     28: # Configuration
                     29: 
                     30: my $debug = 0;
1.5     ! www        31: 
1.1       www        32: # The list of servers is from the LON-CAPA CVS repository in /loncapa/loncom/production_hosts.tab
1.5     ! www        33: my @servers = (
        !            34: 'newscience.westshore.cc.mi.us',
        !            35: 's10.lite.msu.edu',
        !            36: 's12.lite.msu.edu',
        !            37: 'lon-capa.chem.sunysb.edu',
        !            38: 'schubert.tmcc.edu',
        !            39: 'dalton.chem.sfu.ca',
        !            40: 'capa2.phy.ohiou.edu',
        !            41: 'pollux.physics.fsu.edu',
        !            42: 'loncapa.physics.sc.edu',
        !            43: 'loncapa.math.ucf.edu',
        !            44: 'zappa.ags.udel.edu',
        !            45: 'loncapa.gwu.edu',
        !            46: 'neptune.physics.ndsu.nodak.edu',
        !            47: 'capa1.uwsp.edu');
1.1       www        48: 
1.5     ! www        49: foreach (@servers) {
        !            50:     my $url='http://'.$_.'/cgi-bin/metadata_harvest.pl';
1.1       www        51: # End Configuration
                     52: 
1.2       www        53: my $ua = new LWP::UserAgent;
                     54: $ua->timeout(600);
1.1       www        55: 
1.2       www        56: my $request = new HTTP::Request GET => $url;
                     57: $request->authorization_basic('reaper', 'cat4u');
1.1       www        58: 
1.2       www        59: my $response = $ua->request( $request );
1.1       www        60: 
1.2       www        61: if ( $response->is_success ) {
1.5     ! www        62:      print 'SUCCESS: ' . $response->message.' for '.$url."\n\n";
1.2       www        63: 	$content = $response->content;
1.1       www        64: # Delete all blank lines
1.2       www        65: 	$content =~ s/(?<!.)\n//g;
1.1       www        66: # Replace all ^M with spaces
1.2       www        67: 	$content =~ s/
/\s/g;
1.1       www        68: # Push the content into an array
1.2       www        69: 	@loncapa = split /\n/, $content;
                     70: } else {
1.5     ! www        71:      print 'LON-CAPA request failed: ' . $response->message.' for '.$url."\n\n";
        !            72:      next;
1.2       www        73: }
1.1       www        74: 
1.2       www        75: #@loncapa=undef;
                     76: #open (LON_FILE, 'metadata_harvest.txt') || die;
1.1       www        77: 
1.2       www        78: #while (<LON_FILE>) {
                     79: #       chomp;
                     80: #       push(@loncapa,$_);
                     81: #}
1.1       www        82: 
                     83: my %records = ();;
1.3       www        84: 
1.1       www        85: foreach my $metadata (@loncapa) {
                     86: 	chomp $metadata;
1.2       www        87: 	$metadata=~s/[^\w\d\s\.\;\:\,\|\/]/ /gs;
1.1       www        88: 	my @tkline = split('\|', $metadata);
                     89: 	my $title = $tkline[0];
                     90: 	next if ( $title eq '' );
                     91: 	my $author = $tkline[1];
                     92: 	next if ( $author eq '' );
                     93: 	my @authorname = split(' ', $author);
                     94: 	my $author_fname = $authorname[0];
                     95: 	my $author_lname = $authorname[1];
                     96: 	# We have to make an exception for Multimedia Physics which is an organization not a person
                     97: 	my $object_type;
                     98: 	if ( $author_lname eq 'Physics' ) {
                     99: 		$object_type = 'organization';
                    100: 	} else {
                    101: 		$object_type = 'person';
                    102: 	}
                    103: 	my $subject = $tkline[2];
                    104: 	next if ( ($subject eq 'Sample') || ($subject eq 'Something') );
1.2       www       105: 	my $resourceurl = 'http://nsdl.lon-capa.org' . $tkline[3];
                    106:         my $baseid=$tkline[3];
1.4       www       107: 	my ($adom,$auname)=($baseid=~/^\/res\/(\w+)\/(\w+)\//);
1.2       www       108: 	$baseid=~s/\W/\_/g;
                    109: 	$baseid=~s/^\_res\_//g;
1.4       www       110: 	my $fileid=md5_hex($baseid);
1.2       www       111: 
1.1       www       112: 	next if ( $resourceurl =~ /(.*)\/demo\/(.*)/ );
                    113: 	my $keywords = $tkline[4];
                    114: 	my $version = $tkline[5];
                    115: 	my $notes = $tkline[6];
                    116: 	my $abstract = $tkline[7];
                    117: 	next if ($abstract eq '');
                    118: 	my $type = $tkline[8];
                    119: 	my $learning_resource_type;
                    120: 	if ( $type eq 'problem' ) {
                    121: 		$learning_resource_type = 114;
                    122: 	} elsif ( $type eq 'exam' ) {
                    123: 		$learning_resource_type = 114;
                    124: 	} elsif ( $type eq 'quiz' ) {
                    125: 		$learning_resource_type = 114;
                    126: 	} elsif ( $type eq 'assess' ) {
                    127: 		$learning_resource_type = 114;
                    128: 	} elsif ( $type eq 'survey' ) {
                    129: 		$learning_resource_type = 114;
                    130: 	} elsif ( $type eq 'form' ) {
                    131: 		$learning_resource_type = 114;
                    132: 	} elsif ( $type eq 'library' ) {
                    133: 		$learning_resource_type = 107;
                    134: 	} elsif ( $type eq 'page' ) {
                    135: 		$learning_resource_type = 104;
                    136: 	} elsif ( $type eq 'sequence' ) {
                    137: 		$learning_resource_type = 104;
                    138: 	} elsif ( $type eq 'spreadsheet' ) {
                    139: 		$learning_resource_type = 114;
                    140: 	} else {
                    141: 		$learning_resource_type = 0;
                    142: 	}
                    143: 	
                    144: 	my $media_format;
                    145: 	if ( ($type eq 'htm') || ($type eq 'gif') || ($type eq 'mov') || ($type eq 'xml') ) {
                    146: 		$media_format = 70;
                    147: 	} else {
                    148: 		$media_format = 0;
                    149: 	}
                    150: 
                    151: 	my $language = $tkline[9]; # Look only for seniso
                    152: 	next if ( $language ne 'seniso');
                    153: 	my $primary_language='en-US';
                    154: 	my $creation_date = $tkline[10];
1.3       www       155: 	my ($pub_year,$pub_month,$pub_day) = ( $creation_date =~ /^(\d{4}) (\d{2}) (\d{2})\s(\d{2}):(\d{2}):(\d{2})$/ );
1.1       www       156: 	my $revision_date = $tkline[11];
1.3       www       157: 	my ($rev_year,$rev_month,$rev_day) = ( $revision_date =~ /^(\d{4}) (\d{2}) (\d{2})\s(\d{2}):(\d{2}):(\d{2})$/ );
1.1       www       158: 	my $owner = $tkline[12];
                    159: 	my $rights_description;
                    160: 	my $copyright = $tkline[13]; # public,domain,default,private (skip if private and domain)
                    161: 	# Public means no login required
                    162: 
                    163: 	if ( $copyright eq 'public' ) {
                    164: 		$rights_description = 'LON-CAPA Public Resource. No login required.';
                    165: 	} elsif ($copyright eq 'domain') {
                    166: 		$rights_description = 'Restricted to certain LON-CAPA domains.';
                    167: 	} else {
                    168: 		$rights_description = 'LON-CAPA Default Use Restriction. Login required.';
                    169: 	}
                    170: 	# Domain means restricted to a particular LON-CAPA domain
                    171: 	# Defaults mean access open to any registered LON-CAPA user
                    172: 	# Private means open only to author of material
                    173: 	next if ( $copyright eq 'private');
                    174: 	my $platform = "5";     # HTML Browser (not specified but construed from metadata)
1.4       www       175: #
                    176: # Create path
                    177: #
                    178: 	unless (-e $basepath.'/'.$adom) { mkdir($basepath.'/'.$adom); }
                    179: 	unless (-e $basepath.'/'.$adom.'/'.$auname) { 
                    180: 	    mkdir($basepath.'/'.$adom.'/'.$auname) || die 'Could not create '.$basepath.'/'.$adom.'/'.$auname;
                    181: 	}
                    182: 	open(XML,'>'.$basepath.'/'.$adom.'/'.$auname.'/'.$baseid.'.xml');
                    183: 	print XML (<<ENDMETA);
                    184: <?xml version="1.0" encoding="UTF-8"?>
                    185: 
1.3       www       186: <oaidc:dc xmlns="http://purl.org/dc/elements/1.1/" 
                    187:           xmlns:oaidc="http://www.openarchives.org/OAI/2.0/oai_dc/" 
                    188:           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    189:           xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ 
                    190:                               http://www.openarchives.org/OAI/2.0/oai_dc.xsd"
                    191: >
                    192:     <title>$title</title>
                    193:     <creator>$author_fname $author_lname</creator>
                    194:     <identifier>$resourceurl</identifier>
                    195:     <subject>$keywords</subject>
                    196:     <subject>$subject</subject>
                    197:     <language>$primary_language</language>
                    198:     <description>$abstract</description>
                    199:     <date>$rev_year-$rev_month-$rev_day</date>
                    200: </oaidc:dc>
1.2       www       201: ENDMETA
1.4       www       202:       close (XML);
1.5     ! www       203: }
1.1       www       204: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>