#!/usr/bin/perl
# massage B.E.J. in Economic Analysis & Policy XML files, handling
# resumptionToken and Unicode on both input and output
# mod to deal with throttling of OAI server
# Version 2a: incorporate <tier> field to determine journal


print "\nbejeap v2.1.0\n\n";
print "C F Baum  Jan 2005\n\n";
use LWP::Simple;
use Text::Wrap;
require Encode;
$str = get("http://www.bepress.com/cgi/oai2.cgi?verb=ListRecords&metadataPrefix=repec&set=publication:bejeap");
$str = Encode::decode_utf8( $str );

$url2="http://www.bepress.com/cgi/oai2.cgi?verb=ListRecords&resumptionToken=";

($resump) = ($str =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
$url2r = $url2.$resump;
print "\nResump: $resump\n";
sleep 15;
$str2 = get($url2r);
$str2 = Encode::decode_utf8( $str2 );
($resump) = ($str2 =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
$url3r = $url2.$resump;
print "\nResump: $resump\n";
sleep 15;
$str3 = get($url3r);
$str3 = Encode::decode_utf8( $str3 );
($resump) = ($str3 =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
$url4r = $url2.$resump;
print "\nResump: $resump\n";
sleep 15;
$str4 = get($url4r);
$str4 = Encode::decode_utf8( $str4 );
($resump) = ($str4 =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
$url5r = $url2.$resump;
print "\nResump: $resump\n";
sleep 15;
#$str5 = get($url5r);
#$str5 = Encode::decode_utf8( $str5 );
#($resump) = ($str5 =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
#$url6r = $url2.$resump;
#print "\nResump: $resump\n";
#sleep 15;
#$str6 = get($url6r);
#$str6 = Encode::decode_utf8( $str6 );
#($resump) = ($str6 =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
#$url7r = $url2.$resump;
#print "\nResump: $resump\n";
#sleep 15;
#$str7 = get($url7r);
#$str7 = Encode::decode_utf8( $str7 );
#($resump) = ($str7 =~ /<resumptionToken.+?>(.+?)<\/resumptionToken/);
#$url8r = $url2.$resump;
#print "\nResump: $resump\n";
#sleep 15;
#$str8 = get($url8r);
#$str8 = Encode::decode_utf8( $str8 );

$str = $str.$str2.$str3.$str4.$str5.$str6.$str7.$str8;

# set up for *four* journals; open output channels as UTF8
$out[0]="/users/baum/doc/repec/bep/bejeap-adv.rdf";
$jnl[0]="Advances in Economic Analysis & Policy";
$han[0]="eapadv";
open(OUT0,">:utf8",$out[0]) || die "Cannot open $outa for output";
print OUT0 "\x{FEFF}\n"; # utf BOM
$out[1]="/users/baum/doc/repec/bep/bejeap-con.rdf";
$jnl[1]="Contributions to Economic Analysis & Policy";
$han[1]="eapcon";
open(OUT1,">:utf8",$out[1]) || die "Cannot open $outc for output";
print OUT1 "\x{FEFF}\n"; # utf BOM
$out[2]="/users/baum/doc/repec/bep/bejeap-top.rdf";
$jnl[2]="Topics in Economic Analysis & Policy"; 
$han[2]="eaptop";
open(OUT2,">:utf8",$out[2]) || die "Cannot open $outt for output"; 
print OUT2 "\x{FEFF}\n"; # utf BOM
$out[3]="/users/baum/doc/repec/bep/bejeap-fro.rdf";
$jnl[3]="Frontiers of Economic Analysis & Policy";
$han[3]="eapfro";
open(OUT3,">:utf8",$out[3]) || die "Cannot open $outf for output";
print OUT3 "\x{FEFF}\n"; # utf BOM

$nrtem=0;
$str =~ s/\n/ /ig;
$str =~ s/\r/ /ig;
$str =~ s/&amp;/&/ig;
$str =~ s/&quot;/"/ig;

$str =~ s/^.+?<record>//;
(@str) = split("<record>",$str);
$nstr=@str;
print "\n $nstr articles...\n\n";
$debug=1;
foreach $str (@str) {
# DISABLE	$str=&utf8_to_iso8859($str);
	($id) = ($str =~ /<identifier>oai:bepress.com:(.+?)<\/identifier/);
# datestamp=>cd
	($credt) = ($str =~ /<cd>(.+?)<\/cd/);
	($pubyr) = ($credt =~ /^(\d\d\d\d)/);
	($ti) = ($str =~ /<ti>(.+?)<\/ti/);
	(@au) = ($str =~ /<au>(.+?)<\/au/ig);
	$nau = @au;
	($ab) = ($str =~ /<ab>(.+?)<\/ab/);
	($jnl) = ($str =~ /<jrnti>(.+?)<\/jrnti/);
	($vol) = ($str =~ /<vol>(.+?)<\/vol/);
	($iss) = ($str =~ /<iss>(.+?)<\/iss/);
	($ppf) = ($str =~ /<ppf>(.+?)<\/ppf/);
	($ppl) = ($str =~ /<ppl>(.+?)<\/ppl/);
	($url) = ($str =~ /<url>(.+?)<\/url/);
#	($jj) = ($jnl =~ /^(A|C|T|F)/);
#	$wh = 0;
#	if ($jj eq "C") {$wh=1};
#	if ($jj eq "T") {$wh=2};
#	if ($jj eq "F") {$wh=3};
#	print "$id $jnl \n";
	($tier) = ($str =~ /<tier>(.+?)<\/tier>/);
	$wh = 0;
	if ($tier eq "Contributions") {$wh=1};
	if ($tier eq "Topics")        {$wh=2};
	if ($tier eq "Frontiers")     {$wh=3};
	print "$id $wh $tier \n";
	(@kw) = ($str =~ /<kwd>(.+?)<\/kwd/ig);
	$nkw = @kw;
	($jel) = ($str =~ /<categ>(.+?)<\/categ/);
	$oo = "OUT".$wh;
	$jt = $jnl[$wh];
	$hl = $han[$wh];

	print $oo "\nTemplate-Type: ReDIF-Article 1.0\n";
	print $oo "Title: $ti\n";
	foreach $i (@au) {
		($f) = ($i =~ m/fnm>(.+?)<\/fnm/ig);
		($l) = ($i =~ m/snm>(.+?)<\/snm/ig);
		($afl) = ($i =~ m/affl>(.+?)<\/affl/ig);
	print $oo "Author-Name: $f $l \n";
	print $oo "Author-Workplace-Name: $afl \n";
	}
	if (length $ab > 1) {
		print $oo  "Abstract:\n";
		print $oo  wrap(" "," ",$ab);
		print $oo "\n";
	}
	if ($nkw > 0) {
		print $oo "Keywords: ";
		foreach $k (@kw) {
			print $oo "$k, ";
			}
		print $oo "\n";
	}
        ($jok) = ($jel =~ m/([A-Z]\d.)/);
        if ($jok) {
                print $oo "Classification-JEL: $jok \n";
        }
	print $oo "Journal: $jt \n";
	print $oo "Year: $pubyr \n";
	print $oo "Issue: $iss \n";
	print $oo "Volume: $vol \n";
	print $oo "Pages: $ppf-$ppl \n";
	print $oo "Note: oai:bepress:$id\n";
	print $oo "File-URL: $url \n";
	print $oo "File-Format: application/pdf \n";
	print $oo "Handle: RePEc:bep:$hl:v:$vol:y:$pubyr:i:$iss:p:$ppf-$ppl \n";
	$nrtem++;
#	print "$nrtem ";
}
print " \n$nrtem templates processed... \n\n";
exit;
__END__
