Changeset 3192 for branches/2.0/tools/piwigo_remote.pl
- Timestamp:
- Mar 13, 2009, 12:09:22 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.0/tools/piwigo_remote.pl
r3064 r3192 1 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 2 14 3 15 use strict; … … 8 20 use Getopt::Long; 9 21 use Encode qw/is_utf8 decode/; 22 use POSIX qw(ceil floor); 10 23 11 24 my %opt = (); 12 25 GetOptions( 13 26 \%opt, 14 qw/action=s file=s thumbnail=s high=s original=s categories=s define=s%/27 qw/action=s file=s thumbnail=s high=s original=s categories=s chunk_size=i define=s%/ 15 28 ); 16 29 … … 19 32 20 33 my %conf; 21 $conf{base_url} = 'http://localhost/ ~pierrick/piwigo/2.0';34 $conf{base_url} = 'http://localhost/piwigo/2.0'; 22 35 $conf{response_format} = 'json'; 23 $conf{username} = 'p ierrick';24 $conf{password} = ' z0rglub';36 $conf{username} = 'plg'; 37 $conf{password} = 'plg'; 25 38 $conf{limit} = 10; 39 $conf{chunk_size} = defined $opt{chunk_size} ? $opt{chunk_size} : 500_000; 26 40 27 41 my $result = undef; … … 49 63 use File::Slurp; 50 64 65 $form = {}; 66 $form->{method} = 'pwg.images.add'; 67 51 68 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', 69 $form->{original_sum} = $original_sum; 70 71 send_chunks( 72 filepath => $opt{file}, 73 type => 'file', 61 74 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 }; 75 ); 76 $form->{file_sum} = file_md5_hex($opt{file}); 77 78 send_chunks( 79 filepath => $opt{thumbnail}, 80 type => 'thumb', 81 original_sum => $original_sum, 82 ); 83 $form->{thumbnail_sum} = file_md5_hex($opt{thumbnail}); 68 84 69 85 if (defined $opt{high}) { 70 $form->{high_content} = encode_base64(read_file($opt{high})); 86 send_chunks( 87 filepath => $opt{high}, 88 type => 'high', 89 original_sum => $original_sum, 90 ); 71 91 $form->{high_sum} = file_md5_hex($opt{high}); 72 92 } … … 232 252 return $query; 233 253 } 254 255 sub send_chunks { 256 my %params = @_; 257 258 my $content = encode_base64(read_file($params{filepath})); 259 my $content_length = length($content); 260 my $nb_chunks = ceil($content_length / $conf{chunk_size}); 261 262 my $chunk_pos = 0; 263 my $chunk_id = 1; 264 while ($chunk_pos < $content_length) { 265 my $chunk = substr( 266 $content, 267 $chunk_pos, 268 $conf{chunk_size} 269 ); 270 $chunk_pos += $conf{chunk_size}; 271 272 my $response = $ua->post( 273 $conf{base_url}.'/ws.php?format=json', 274 { 275 method => 'pwg.images.addChunk', 276 data => $chunk, 277 original_sum => $params{original_sum}, 278 position => $chunk_id, 279 type => $params{type}, 280 } 281 ); 282 283 printf( 284 'chunk %05u of %05u for %s "%s"'."\n", 285 $chunk_id, 286 $nb_chunks, 287 $params{type}, 288 $params{filepath} 289 ); 290 if ($response->code != 200) { 291 printf("response code : %u\n", $response->code); 292 printf("response message : %s\n", $response->message); 293 } 294 295 $chunk_id++; 296 } 297 }
Note: See TracChangeset
for help on using the changeset viewer.