From e02d0852008761094c20fb75437bd11bae615980 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 29 Oct 2013 18:19:37 +0100 Subject: [PATCH] initial REST API for EPrints --- eprints-rest.pl | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 eprints-rest.pl diff --git a/eprints-rest.pl b/eprints-rest.pl new file mode 100755 index 0000000..c4a0ba0 --- /dev/null +++ b/eprints-rest.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl +use warnings; +use strict; +use LWP::Debug qw(+); +use LWP::UserAgent; +use Data::Dump qw(dump); +use utf8; + +=begin eprints configuration + +add to /usr/share/eprints3/archives/grf/cfg/cfg.d/user_roles.pl + +$c->{user_roles}->{admin} = [qw{ + # ... + rest + upsert + # ... +}]; + +=cut + +our $repo = "http://eprints.ffzg.hr/grf"; +our $user = 'admin'; +our $passwd = 'XXXX'; +require "config.pl"; + +warn "# repo: $repo [$user]\n"; + +my $nr; + +my $ua = LWP::UserAgent->new; +$ua->credentials("eprints.ffzg.hr:80", "EPrintsREST", "admin", "grfadmin"); + +my @ids; +my $response = $ua->get( "$repo/rest/eprint/" ); +if ($response->is_success) { + my $html = $response->decoded_content; +# print $html; + @ids = grep { defined $_ } map { m{href=['"](\d+)/} && $1 || undef } split(/[\n\r]/, $html); +} else { + die $response->status_line; +} + +print "ids = ",dump(@ids),$/; + +$nr = $ids[-1]; + +print "nr = $nr\n"; + +$response = $ua->get( "$repo/rest/eprint/$nr.xml" ); + +if ($response->is_success) { + print "ORIG\n", $response->decoded_content; +} else { + die "ERROR: ", $response->status_line; +} + + +my $xml = $response->decoded_content; # or whatever +my $title = $1 if $xml =~ m{(.+)}; +$title =~ s{\s*\[editirano]}{} or $title .= ' [editirano]'; +my $update = $ua->request( HTTP::Request->new( 'PUT', "$repo/rest/eprint/$nr/title.txt", undef, "$title" ) ); +if ($update->is_success) { + print "UPDATE: ", $update->decoded_content; +} else { + die "ERROR: ", $update->status_line; +} + +my $old = $nr; +$nr++; +$xml =~ s{(eprint.*)$old}{$1$nr}g; + +my $create = $ua->request( HTTP::Request->new( 'PUT', "$repo/rest/eprint/$nr/", [ 'Content-type' => 'text/xml' ], utf8::decode($xml) ) ); +if ($create->is_success) { + print "CREATE: ", $create->decoded_content; +} else { + die "ERROR: ", $create->status_line, $create->decoded_content; +} + -- 2.20.1