source: trunk/tools/piwigo_remote.pl @ 2584

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

new: WebService method pwg.tags.getAdminList was added. The difference with
pwg.tags.getList is that this new method doesn't take permissions into
account, and so is available only for administrator connected users.

  • Property svn:eol-style set to LF
File size: 3.8 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 categories=s define=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{response_format} = 'json';
22$conf{username} = 'pierrick';
23$conf{password} = 'z0rglub';
24$conf{limit} = 10;
25
26my $result = undef;
27my $query = undef;
28
29binmode STDOUT, ":encoding(utf-8)";
30
31# TODO : don't connect at each script call, use the session duration instead.
32my $form = {
33    method => 'pwg.session.login',
34    username => $conf{username},
35    password => $conf{password},
36};
37
38$result = $ua->post(
39    $conf{base_url}.'/ws.php?format=json',
40    $form
41);
42
43# print "\n", $ua->cookie_jar->as_string, "\n";
44
45if ($opt{action} eq 'pwg.images.add') {
46    use MIME::Base64 qw(encode_base64);
47    use Digest::MD5::File qw/file_md5_hex/;
48    use File::Slurp;
49
50    my $file_content = encode_base64(read_file($opt{file}));
51    my $file_sum = file_md5_hex($opt{file});
52
53    my $thumbnail_content = encode_base64(read_file($opt{thumbnail}));
54    my $thumbnail_sum = file_md5_hex($opt{thumbnail});
55
56    $form = {
57        method => 'pwg.images.add',
58        file_sum => $file_sum,
59        file_content => $file_content,
60        thumbnail_sum => $thumbnail_sum,
61        thumbnail_content => $thumbnail_content,
62        categories => $opt{categories},
63    };
64
65    foreach my $key (keys %{ $opt{define} }) {
66        $form->{$key} = $opt{define}{$key};
67    }
68
69    my $response = $ua->post(
70        $conf{base_url}.'/ws.php?format=json',
71        $form
72    );
73
74    print "-" x 50, "\n";
75    printf("response code    : %u\n", $response->code);
76    printf("response message : %s\n", $response->message);
77    print "-" x 50, "\n";
78    print "\n";
79
80#     use Data::Dumper;
81#     print Dumper($response);
82
83    if ($response->is_success) {
84        print "upload successful\n";
85    }
86    else {
87        warn 'A problem has occured during upload', "\n";
88        warn $response->decoded_content, "\n";
89        die $response->status_line;
90    }
91}
92
93if ($opt{action} eq 'pwg.tags.list') {
94    use Text::ASCIITable;
95
96    $query = pwg_ws_get_query(
97        method => 'pwg.tags.getList',
98        sort_by_counter => 'true',
99    );
100
101    $result = $ua->get($query);
102    my $tag_result = from_json($result->content);
103    my $t = Text::ASCIITable->new({ headingText => 'Tags' });
104    $t->setCols('id','counter','name');
105
106    my $tag_number = 1;
107    foreach my $tag_href (@{ $tag_result->{result}{tags} }) {
108        $t->addRow(
109            $tag_href->{id},
110            $tag_href->{counter},
111            $tag_href->{name}
112        );
113
114        last if $tag_number++ >= $conf{limit};
115    }
116    print $t;
117}
118
119if ($opt{action} eq 'pwg.tags.getAdminList') {
120    $query = pwg_ws_get_query(
121        method => 'pwg.tags.getAdminList'
122    );
123
124    $result = $ua->get($query);
125    my $tags = from_json($result->content)->{result}{tags};
126
127    foreach my $tag (@{$tags}) {
128        # print join(',', keys %{$tag}), "\n"; exit();
129        printf(
130            '{%u} %s ',
131            $tag->{id},
132            $tag->{name}
133        );
134    }
135
136    print "\n";
137}
138
139if ($opt{action} eq 'pwg.categories.add') {
140    $form = {
141        method => 'pwg.categories.add',
142        name => $opt{define}{name},
143        parent => $opt{define}{parent},
144    };
145
146    my $response = $ua->post(
147        $conf{base_url}.'/ws.php?format=json',
148        $form
149    );
150
151    use Data::Dumper;
152    print Dumper(from_json($response->content));
153}
154
155$query = pwg_ws_get_query(
156    method => 'pwg.session.logout'
157);
158$ua->get($query);
159
160sub pwg_ws_get_query {
161    my %params = @_;
162
163    my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format};
164
165    foreach my $key (keys %params) {
166        $query .= '&'.$key.'='.$params{$key};
167    }
168
169    return $query;
170}
Note: See TracBrowser for help on using the repository browser.