#!/usr/bin/perl -w

use strict;
use warnings;
use HTTP::OAI;

print "\n*** start\n";

my $burl = "https://api.archives-ouvertes.fr/oai/hal/";

# sets the HTTP::OAI::Identify object used by the Harvester agent.
my $h = new HTTP::OAI::Harvester(baseURL=>$burl);

# Get information about the repository.
my $r = $h->Identify;
if( $r->is_error ) {
	print "Error requesting Identify:\n",
	$r->code . " " . $r->message, "\n";
	exit;
}

print map({ "adminEmail => " . $_ . "\n" } $r->adminEmail),
	"baseURL => ", $r->baseURL, "\n",
	"protocolVersion => ", $r->protocolVersion, "\n",
	"repositoryName => ", $r->repositoryName, "\n";

# Retrieve the identifiers, datestamps, sets and deleted status for all records 
# within the specified date range (from/until) and set spec (set)
$r = $h->ListIdentifiers(
	metadataPrefix=>'oai_dc',
	set => 'collection:ETHNO'
	);

if( $r->is_error ) {
	die("Error harvesting: " . $r->message . "\n");
}

print "responseDate => ", $r->responseDate, "\n",
	"requestURL => ", $r->requestURL, "\n";

open(my $fh, '>', 'list.txt');
while( my $id = $r->next ) {

	print $fh "identifier => ", $id->identifier;
	# Only available from OAI 2.0 repositories
	print $fh " (", $id->datestamp, ")" if $id->datestamp;
	print $fh " (", $id->status, ")" if $id->status;
	print $fh "\n";
	# Only available from OAI 2.0 repositories
	for( $id->setSpec ) {
		print $fh "\t", $_, "\n";
	}
}
close $fh;

print "\n*** done\n";
