source: tags/2.0.0/tools/piwigo_remote.pl @ 27510

Last change on this file since 27510 was 3064, checked in by plg, 15 years ago

bug 912 fixed: we need an extra original_sum to pwg.images.add API method to
check photo existence. Using the file_sum was a bad idea.

  • Property svn:eol-style set to LF
File size: 5.2 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use JSON;
7use LWP::UserAgent;
8use Getopt::Long;
9use Encode qw/is_utf8 decode/;
10
11my %opt = ();
12GetOptions(
13    \%opt,
14    qw/action=s file=s thumbnail=s high=s original=s categories=s define=s%/
15);
16
17our $ua = LWP::UserAgent->new;
18$ua->cookie_jar({});
19
20my %conf;
21$conf{base_url} = 'http://localhost/~pierrick/piwigo/2.0';
22$conf{response_format} = 'json';
23$conf{username} = 'pierrick';
24$conf{password} = 'z0rglub';
25$conf{limit} = 10;
26
27my $result = undef;
28my $query = undef;
29
30binmode STDOUT, ":encoding(utf-8)";
31
32# TODO : don't connect at each script call, use the session duration instead.
33my $form = {
34    method => 'pwg.session.login',
35    username => $conf{username},
36    password => $conf{password},
37};
38
39$result = $ua->post(
40    $conf{base_url}.'/ws.php?format=json',
41    $form
42);
43
44# print "\n", $ua->cookie_jar->as_string, "\n";
45
46if ($opt{action} eq 'pwg.images.add') {
47    use MIME::Base64 qw(encode_base64);
48    use Digest::MD5::File qw/file_md5_hex/;
49    use File::Slurp;
50
51    my $original_sum = file_md5_hex($opt{original});
52
53    my $file_content = encode_base64(read_file($opt{file}));
54    my $file_sum = file_md5_hex($opt{file});
55
56    my $thumbnail_content = encode_base64(read_file($opt{thumbnail}));
57    my $thumbnail_sum = file_md5_hex($opt{thumbnail});
58
59    $form = {
60        method => 'pwg.images.add',
61        original_sum => $original_sum,
62        file_sum => $file_sum,
63        file_content => $file_content,
64        thumbnail_sum => $thumbnail_sum,
65        thumbnail_content => $thumbnail_content,
66        categories => $opt{categories},
67    };
68
69    if (defined $opt{high}) {
70        $form->{high_content} = encode_base64(read_file($opt{high}));
71        $form->{high_sum} = file_md5_hex($opt{high});
72    }
73
74    foreach my $key (keys %{ $opt{define} }) {
75        $form->{$key} = $opt{define}{$key};
76    }
77
78    my $response = $ua->post(
79        $conf{base_url}.'/ws.php?format=json',
80        $form
81    );
82
83    print "-" x 50, "\n";
84    printf("response code    : %u\n", $response->code);
85    printf("response message : %s\n", $response->message);
86    print "-" x 50, "\n";
87    print "\n";
88
89#     use Data::Dumper;
90#     print Dumper($response->content);
91#     print Dumper(from_json($response->content));
92
93    if ($response->is_success) {
94        print "upload successful\n";
95    }
96    else {
97        warn 'A problem has occured during upload', "\n";
98        warn $response->decoded_content, "\n";
99        die $response->status_line;
100    }
101}
102
103if ($opt{action} eq 'pwg.tags.list') {
104    use Text::ASCIITable;
105
106    $query = pwg_ws_get_query(
107        method => 'pwg.tags.getList',
108        sort_by_counter => 'true',
109    );
110
111    $result = $ua->get($query);
112    my $tag_result = from_json($result->content);
113    my $t = Text::ASCIITable->new({ headingText => 'Tags' });
114    $t->setCols('id','counter','name');
115
116    my $tag_number = 1;
117    foreach my $tag_href (@{ $tag_result->{result}{tags} }) {
118        $t->addRow(
119            $tag_href->{id},
120            $tag_href->{counter},
121            $tag_href->{name}
122        );
123
124        last if $tag_number++ >= $conf{limit};
125    }
126    print $t;
127}
128
129if ($opt{action} eq 'pwg.tags.getAdminList') {
130    $query = pwg_ws_get_query(
131        method => 'pwg.tags.getAdminList'
132    );
133
134    $result = $ua->get($query);
135    my $tags = from_json($result->content)->{result}{tags};
136
137    foreach my $tag (@{$tags}) {
138        # print join(',', keys %{$tag}), "\n"; exit();
139        printf(
140            '{%u} %s ',
141            $tag->{id},
142            $tag->{name}
143        );
144    }
145
146    print "\n";
147}
148
149if ($opt{action} eq 'pwg.categories.add') {
150    $form = {
151        method => 'pwg.categories.add',
152        name => $opt{define}{name},
153        parent => $opt{define}{parent},
154    };
155
156    my $response = $ua->post(
157        $conf{base_url}.'/ws.php?format=json',
158        $form
159    );
160
161    use Data::Dumper;
162    print Dumper(from_json($response->content));
163}
164
165if ($opt{action} eq 'pwg.tags.add') {
166    $form = {
167        method => 'pwg.tags.add',
168        name => $opt{define}{name},
169    };
170
171    my $response = $ua->post(
172        $conf{base_url}.'/ws.php?format=json',
173        $form
174    );
175
176    use Data::Dumper;
177    print Dumper(from_json($response->content));
178}
179
180if ($opt{action} eq 'pwg.images.exist') {
181    $form = {
182        method => $opt{action},
183    };
184
185    foreach my $key (keys %{ $opt{define} }) {
186        $form->{$key} = $opt{define}{$key};
187    }
188
189    my $response = $ua->post(
190        $conf{base_url}.'/ws.php?format=json',
191        $form
192    );
193
194    use Data::Dumper;
195    print Dumper(from_json($response->content)->{result});
196    # print Dumper($response);
197}
198
199if ($opt{action} eq 'pwg.images.setInfo') {
200    $form = {
201        method => $opt{action},
202    };
203
204    foreach my $key (keys %{ $opt{define} }) {
205        $form->{$key} = $opt{define}{$key};
206    }
207
208    my $response = $ua->post(
209        $conf{base_url}.'/ws.php?format=json',
210        $form
211    );
212
213    use Data::Dumper;
214    # print Dumper(from_json($response->content)->{result});
215    print Dumper($response);
216}
217
218$query = pwg_ws_get_query(
219    method => 'pwg.session.logout'
220);
221$ua->get($query);
222
223sub pwg_ws_get_query {
224    my %params = @_;
225
226    my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format};
227
228    foreach my $key (keys %params) {
229        $query .= '&'.$key.'='.$params{$key};
230    }
231
232    return $query;
233}
Note: See TracBrowser for help on using the repository browser.