source: tags/build-Butterfly01/tools/piwigo_remote.pl @ 2493

Last change on this file since 2493 was 2463, checked in by plg, 16 years ago

feature 839, first step : early proof of concept, no error handling. A
remote client can add a photo in a category thanks to the web API. A new
"upload" directory is created (write access required on the base
directory). Uploaded photo have path such as
upload/<year>/<month>/<day>/<datetime>-random.jpg. The thumbnail must come
with the "web sized" photo. The photo has no storage_category_id.

Bugs still need to be fixed and a discussion must occur before next steps.

  • Property svn:eol-style set to LF
File size: 2.7 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use JSON;
7use LWP::UserAgent;
8use Getopt::Long;
9
10my %opt = ();
11GetOptions(
12    \%opt,
13    qw/action=s file=s thumbnail=s category_id=i name=s/
14);
15
16our $ua = LWP::UserAgent->new;
17$ua->cookie_jar({});
18
19my %conf;
20$conf{base_url} = 'http://localhost/~pierrick/piwigo/trunk';
21$conf{partner_key} = 'youhou';
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?partner='.$conf{partner_key}.'&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 $file_content = encode_base64(read_file($opt{file}));
52    my $file_sum = file_md5_hex($opt{file});
53
54    my $thumbnail_content = encode_base64(read_file($opt{thumbnail}));
55    my $thumbnail_sum = file_md5_hex($opt{thumbnail});
56
57    $form = {
58        method => 'pwg.images.add',
59        file_sum => $file_sum,
60        file_content => $file_content,
61        thumbnail_sum => $thumbnail_sum,
62        thumbnail_content => $thumbnail_content,
63        category_id => $opt{category_id},
64        name => $opt{name},
65    };
66
67    $result = $ua->post(
68        $conf{base_url}.'/ws.php?partner='.$conf{partner_key}.'&format=json',
69        $form
70    );
71}
72
73if ($opt{action} eq 'pwg.tags.list') {
74    use Text::ASCIITable;
75
76    $query = pwg_ws_get_query(
77        method => 'pwg.tags.getList',
78        sort_by_counter => 'true',
79    );
80
81    $result = $ua->get($query);
82    my $tag_result = from_json($result->content);
83    my $t = Text::ASCIITable->new({ headingText => 'Tags' });
84    $t->setCols('id','counter','name');
85
86    my $tag_number = 1;
87    foreach my $tag_href (@{ $tag_result->{result}{tags} }) {
88        $t->addRow(
89            $tag_href->{id},
90            $tag_href->{counter},
91            $tag_href->{name}
92        );
93
94        last if $tag_number++ >= $conf{limit};
95    }
96    print $t;
97}
98
99$query = pwg_ws_get_query(
100    method => 'pwg.session.logout'
101);
102$ua->get($query);
103
104sub pwg_ws_get_query {
105    my %params = @_;
106
107    my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format};
108
109    if (defined $conf{partner_key}) {
110        $query .= '&partner='.$conf{partner_key};
111    }
112
113    foreach my $key (keys %params) {
114        $query .= '&'.$key.'='.$params{$key};
115    }
116
117    return $query;
118}
Note: See TracBrowser for help on using the repository browser.