use web imports to transfer files
[koha-eprints] / eprints-rest.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use LWP::Debug qw(+);
5 use LWP::UserAgent;
6 use Data::Dump qw(dump);
7 use utf8;
8
9 =begin eprints configuration
10
11 add to /usr/share/eprints3/archives/grf/cfg/cfg.d/user_roles.pl
12
13 $c->{user_roles}->{admin} = [qw{
14         # ...
15         rest
16         upsert
17         # ...
18 }];
19
20 =cut
21
22 our $repo = "http://eprints.ffzg.hr/grf";
23 our $user = 'admin';
24 our $passwd = 'XXXX';
25 require "config.pl";
26
27 warn "# repo: $repo [$user]\n";
28
29 my $nr;
30
31 my $ua = LWP::UserAgent->new;
32 $ua->credentials("eprints.ffzg.hr:80", "EPrintsREST", "admin", "grfadmin");
33
34 my @ids;
35 my $response = $ua->get( "$repo/rest/eprint/" );
36 if ($response->is_success) {
37         my $html = $response->decoded_content;
38 #       print $html;
39         @ids = grep { defined $_ } map { m{href=['"](\d+)/} && $1 || undef } split(/[\n\r]/, $html);
40 } else {
41         die $response->status_line;
42 }
43
44 print "ids = ",dump(@ids),$/;
45
46 $nr = $ids[-1];
47
48 print "nr = $nr\n";
49
50 $response = $ua->get( "$repo/rest/eprint/$nr.xml" );
51
52 if ($response->is_success) {
53         print "ORIG\n", $response->decoded_content;
54 } else {
55         die "ERROR: ", $response->status_line;
56 }
57
58
59 my $xml = $response->decoded_content;  # or whatever
60 my $title = $1 if $xml =~ m{<title>(.+)</title>};
61 $title =~ s{\s*\[editirano]}{} or $title .= ' [editirano]';
62 my $update = $ua->request( HTTP::Request->new( 'PUT', "$repo/rest/eprint/$nr/title.txt", undef, "$title" ) );
63 if ($update->is_success) {
64         print "UPDATE: ", $update->decoded_content;
65 } else {
66         die "ERROR: ", $update->status_line;
67 }
68
69 my $old = $nr;
70 $nr++;
71 $xml =~ s{(eprint.*)$old}{$1$nr}g;
72
73 my $create = $ua->request( HTTP::Request->new( 'PUT', "$repo/rest/eprint/$nr/", [ 'Content-type' => 'text/xml' ], utf8::decode($xml) ) );
74 if ($create->is_success) {
75         print "CREATE: ", $create->decoded_content;
76 } else {
77         die "ERROR: ", $create->status_line, $create->decoded_content;
78 }
79