1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | #### |
---|
4 | # Usage examples |
---|
5 | # |
---|
6 | # time perl piwigo_remote.pl \ |
---|
7 | # --action=pwg.images.add \ |
---|
8 | # --file=erwann_rocher-web.jpg \ |
---|
9 | # --thumb=erwann_rocher-thumb.jpg \ |
---|
10 | # --high=erwann_rocher-high.jpg \ |
---|
11 | # --original=erwann_rocher-high.jpg \ |
---|
12 | # --define categories=9 \ |
---|
13 | # --chunk_size=200_000 |
---|
14 | |
---|
15 | use strict; |
---|
16 | use warnings; |
---|
17 | |
---|
18 | use JSON; |
---|
19 | use LWP::UserAgent; |
---|
20 | use Getopt::Long; |
---|
21 | use Encode qw/is_utf8 decode/; |
---|
22 | use POSIX qw(ceil floor); |
---|
23 | |
---|
24 | my %opt = (); |
---|
25 | GetOptions( |
---|
26 | \%opt, |
---|
27 | qw/ |
---|
28 | action=s |
---|
29 | file=s |
---|
30 | thumbnail=s |
---|
31 | high=s |
---|
32 | original=s |
---|
33 | categories=s |
---|
34 | chunk_size=i |
---|
35 | base_url=s |
---|
36 | username=s |
---|
37 | password=s |
---|
38 | define=s% |
---|
39 | / |
---|
40 | ); |
---|
41 | |
---|
42 | our $ua = LWP::UserAgent->new; |
---|
43 | $ua->cookie_jar({}); |
---|
44 | |
---|
45 | my %conf; |
---|
46 | $conf{response_format} = 'json'; |
---|
47 | $conf{limit} = 10; |
---|
48 | |
---|
49 | my %conf_default = ( |
---|
50 | base_url => 'http://localhost/piwigo/2.0', |
---|
51 | username => 'plg', |
---|
52 | password => 'plg', |
---|
53 | chunk_size => 500_000, |
---|
54 | ); |
---|
55 | foreach my $conf_key (keys %conf_default) { |
---|
56 | $conf{$conf_key} = defined $opt{$conf_key} ? $opt{$conf_key} : $conf_default{$conf_key} |
---|
57 | } |
---|
58 | |
---|
59 | my $result = undef; |
---|
60 | my $query = undef; |
---|
61 | |
---|
62 | binmode STDOUT, ":encoding(utf-8)"; |
---|
63 | |
---|
64 | # TODO : don't connect at each script call, use the session duration instead. |
---|
65 | my $form = { |
---|
66 | method => 'pwg.session.login', |
---|
67 | username => $conf{username}, |
---|
68 | password => $conf{password}, |
---|
69 | }; |
---|
70 | |
---|
71 | $result = $ua->post( |
---|
72 | $conf{base_url}.'/ws.php?format=json', |
---|
73 | $form |
---|
74 | ); |
---|
75 | |
---|
76 | # print "\n", $ua->cookie_jar->as_string, "\n"; |
---|
77 | |
---|
78 | if ($opt{action} eq 'pwg.images.add') { |
---|
79 | use MIME::Base64 qw(encode_base64); |
---|
80 | use Digest::MD5::File qw/file_md5_hex/; |
---|
81 | use File::Slurp; |
---|
82 | |
---|
83 | $form = {}; |
---|
84 | $form->{method} = 'pwg.images.add'; |
---|
85 | |
---|
86 | my $original_sum = file_md5_hex($opt{original}); |
---|
87 | $form->{original_sum} = $original_sum; |
---|
88 | |
---|
89 | send_chunks( |
---|
90 | filepath => $opt{file}, |
---|
91 | type => 'file', |
---|
92 | original_sum => $original_sum, |
---|
93 | ); |
---|
94 | $form->{file_sum} = file_md5_hex($opt{file}); |
---|
95 | |
---|
96 | send_chunks( |
---|
97 | filepath => $opt{thumbnail}, |
---|
98 | type => 'thumb', |
---|
99 | original_sum => $original_sum, |
---|
100 | ); |
---|
101 | $form->{thumbnail_sum} = file_md5_hex($opt{thumbnail}); |
---|
102 | |
---|
103 | if (defined $opt{high}) { |
---|
104 | send_chunks( |
---|
105 | filepath => $opt{high}, |
---|
106 | type => 'high', |
---|
107 | original_sum => $original_sum, |
---|
108 | ); |
---|
109 | $form->{high_sum} = file_md5_hex($opt{high}); |
---|
110 | } |
---|
111 | |
---|
112 | foreach my $key (keys %{ $opt{define} }) { |
---|
113 | $form->{$key} = $opt{define}{$key}; |
---|
114 | } |
---|
115 | |
---|
116 | my $response = $ua->post( |
---|
117 | $conf{base_url}.'/ws.php?format=json', |
---|
118 | $form |
---|
119 | ); |
---|
120 | |
---|
121 | print "-" x 50, "\n"; |
---|
122 | printf("response code : %u\n", $response->code); |
---|
123 | printf("response message : %s\n", $response->message); |
---|
124 | print "-" x 50, "\n"; |
---|
125 | print "\n"; |
---|
126 | |
---|
127 | # use Data::Dumper; |
---|
128 | # print Dumper($response->content); |
---|
129 | # print Dumper(from_json($response->content)); |
---|
130 | |
---|
131 | if ($response->is_success) { |
---|
132 | print "upload successful\n"; |
---|
133 | } |
---|
134 | else { |
---|
135 | warn 'A problem has occured during upload', "\n"; |
---|
136 | warn $response->decoded_content, "\n"; |
---|
137 | die $response->status_line; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | if ($opt{action} eq 'pwg.tags.list') { |
---|
142 | use Text::ASCIITable; |
---|
143 | |
---|
144 | $query = pwg_ws_get_query( |
---|
145 | method => 'pwg.tags.getList', |
---|
146 | sort_by_counter => 'true', |
---|
147 | ); |
---|
148 | |
---|
149 | $result = $ua->get($query); |
---|
150 | my $tag_result = from_json($result->content); |
---|
151 | my $t = Text::ASCIITable->new({ headingText => 'Tags' }); |
---|
152 | $t->setCols('id','counter','name'); |
---|
153 | |
---|
154 | my $tag_number = 1; |
---|
155 | foreach my $tag_href (@{ $tag_result->{result}{tags} }) { |
---|
156 | $t->addRow( |
---|
157 | $tag_href->{id}, |
---|
158 | $tag_href->{counter}, |
---|
159 | $tag_href->{name} |
---|
160 | ); |
---|
161 | |
---|
162 | last if $tag_number++ >= $conf{limit}; |
---|
163 | } |
---|
164 | print $t; |
---|
165 | } |
---|
166 | |
---|
167 | if ($opt{action} eq 'pwg.tags.getAdminList') { |
---|
168 | $query = pwg_ws_get_query( |
---|
169 | method => 'pwg.tags.getAdminList' |
---|
170 | ); |
---|
171 | |
---|
172 | $result = $ua->get($query); |
---|
173 | my $tags = from_json($result->content)->{result}{tags}; |
---|
174 | |
---|
175 | foreach my $tag (@{$tags}) { |
---|
176 | # print join(',', keys %{$tag}), "\n"; exit(); |
---|
177 | printf( |
---|
178 | '{%u} %s ', |
---|
179 | $tag->{id}, |
---|
180 | $tag->{name} |
---|
181 | ); |
---|
182 | } |
---|
183 | |
---|
184 | print "\n"; |
---|
185 | } |
---|
186 | |
---|
187 | if ($opt{action} eq 'pwg.categories.add') { |
---|
188 | $form = { |
---|
189 | method => 'pwg.categories.add', |
---|
190 | name => $opt{define}{name}, |
---|
191 | parent => $opt{define}{parent}, |
---|
192 | }; |
---|
193 | |
---|
194 | my $response = $ua->post( |
---|
195 | $conf{base_url}.'/ws.php?format=json', |
---|
196 | $form |
---|
197 | ); |
---|
198 | |
---|
199 | use Data::Dumper; |
---|
200 | print Dumper(from_json($response->content)); |
---|
201 | } |
---|
202 | |
---|
203 | if ($opt{action} eq 'pwg.tags.add') { |
---|
204 | $form = { |
---|
205 | method => 'pwg.tags.add', |
---|
206 | name => $opt{define}{name}, |
---|
207 | }; |
---|
208 | |
---|
209 | my $response = $ua->post( |
---|
210 | $conf{base_url}.'/ws.php?format=json', |
---|
211 | $form |
---|
212 | ); |
---|
213 | |
---|
214 | use Data::Dumper; |
---|
215 | print Dumper(from_json($response->content)); |
---|
216 | } |
---|
217 | |
---|
218 | if ($opt{action} eq 'pwg.images.exist') { |
---|
219 | $form = { |
---|
220 | method => $opt{action}, |
---|
221 | }; |
---|
222 | |
---|
223 | foreach my $key (keys %{ $opt{define} }) { |
---|
224 | $form->{$key} = $opt{define}{$key}; |
---|
225 | } |
---|
226 | |
---|
227 | my $response = $ua->post( |
---|
228 | $conf{base_url}.'/ws.php?format=json', |
---|
229 | $form |
---|
230 | ); |
---|
231 | |
---|
232 | use Data::Dumper; |
---|
233 | print Dumper(from_json($response->content)->{result}); |
---|
234 | # print Dumper($response); |
---|
235 | } |
---|
236 | |
---|
237 | if ($opt{action} eq 'pwg.images.setInfo' or $opt{action} eq 'pwg.categories.setInfo') { |
---|
238 | $form = { |
---|
239 | method => $opt{action}, |
---|
240 | }; |
---|
241 | |
---|
242 | foreach my $key (keys %{ $opt{define} }) { |
---|
243 | $form->{$key} = $opt{define}{$key}; |
---|
244 | } |
---|
245 | |
---|
246 | my $response = $ua->post( |
---|
247 | $conf{base_url}.'/ws.php?format=json', |
---|
248 | $form |
---|
249 | ); |
---|
250 | |
---|
251 | use Data::Dumper; |
---|
252 | # print Dumper(from_json($response->content)->{result}); |
---|
253 | print Dumper($response); |
---|
254 | } |
---|
255 | |
---|
256 | $query = pwg_ws_get_query( |
---|
257 | method => 'pwg.session.logout' |
---|
258 | ); |
---|
259 | $ua->get($query); |
---|
260 | |
---|
261 | sub pwg_ws_get_query { |
---|
262 | my %params = @_; |
---|
263 | |
---|
264 | my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format}; |
---|
265 | |
---|
266 | foreach my $key (keys %params) { |
---|
267 | $query .= '&'.$key.'='.$params{$key}; |
---|
268 | } |
---|
269 | |
---|
270 | return $query; |
---|
271 | } |
---|
272 | |
---|
273 | sub send_chunks { |
---|
274 | my %params = @_; |
---|
275 | |
---|
276 | my $content = read_file($params{filepath}); |
---|
277 | my $content_length = length($content); |
---|
278 | my $nb_chunks = ceil($content_length / $conf{chunk_size}); |
---|
279 | |
---|
280 | my $chunk_pos = 0; |
---|
281 | my $chunk_id = 1; |
---|
282 | while ($chunk_pos < $content_length) { |
---|
283 | my $chunk = substr( |
---|
284 | $content, |
---|
285 | $chunk_pos, |
---|
286 | $conf{chunk_size} |
---|
287 | ); |
---|
288 | $chunk_pos += $conf{chunk_size}; |
---|
289 | |
---|
290 | my $response = $ua->post( |
---|
291 | $conf{base_url}.'/ws.php?format=json', |
---|
292 | { |
---|
293 | method => 'pwg.images.addChunk', |
---|
294 | data => encode_base64($chunk), |
---|
295 | original_sum => $params{original_sum}, |
---|
296 | position => $chunk_id, |
---|
297 | type => $params{type}, |
---|
298 | } |
---|
299 | ); |
---|
300 | |
---|
301 | printf( |
---|
302 | 'chunk %05u of %05u for %s "%s"'."\n", |
---|
303 | $chunk_id, |
---|
304 | $nb_chunks, |
---|
305 | $params{type}, |
---|
306 | $params{filepath} |
---|
307 | ); |
---|
308 | if ($response->code != 200) { |
---|
309 | printf("response code : %u\n", $response->code); |
---|
310 | printf("response message : %s\n", $response->message); |
---|
311 | } |
---|
312 | |
---|
313 | $chunk_id++; |
---|
314 | } |
---|
315 | } |
---|