source: trunk/tools/piwigo_remote.pl @ 2683

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

feature 889 added: pwg.images.exist check the existence of a photo in the
database based on its md5sum. (avoid failing on pwg.images.add).

  • Property svn:eol-style set to LF
File size: 4.7 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 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/trunk';
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 $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        categories => $opt{categories},
64    };
65
66    if (defined $opt{high}) {
67        $form->{high_content} = encode_base64(read_file($opt{high}));
68        $form->{high_sum} = file_md5_hex($opt{high});
69    }
70
71    foreach my $key (keys %{ $opt{define} }) {
72        $form->{$key} = $opt{define}{$key};
73    }
74
75    my $response = $ua->post(
76        $conf{base_url}.'/ws.php?format=json',
77        $form
78    );
79
80    print "-" x 50, "\n";
81    printf("response code    : %u\n", $response->code);
82    printf("response message : %s\n", $response->message);
83    print "-" x 50, "\n";
84    print "\n";
85
86#     use Data::Dumper;
87#     print Dumper($response);
88
89    if ($response->is_success) {
90        print "upload successful\n";
91    }
92    else {
93        warn 'A problem has occured during upload', "\n";
94        warn $response->decoded_content, "\n";
95        die $response->status_line;
96    }
97}
98
99if ($opt{action} eq 'pwg.tags.list') {
100    use Text::ASCIITable;
101
102    $query = pwg_ws_get_query(
103        method => 'pwg.tags.getList',
104        sort_by_counter => 'true',
105    );
106
107    $result = $ua->get($query);
108    my $tag_result = from_json($result->content);
109    my $t = Text::ASCIITable->new({ headingText => 'Tags' });
110    $t->setCols('id','counter','name');
111
112    my $tag_number = 1;
113    foreach my $tag_href (@{ $tag_result->{result}{tags} }) {
114        $t->addRow(
115            $tag_href->{id},
116            $tag_href->{counter},
117            $tag_href->{name}
118        );
119
120        last if $tag_number++ >= $conf{limit};
121    }
122    print $t;
123}
124
125if ($opt{action} eq 'pwg.tags.getAdminList') {
126    $query = pwg_ws_get_query(
127        method => 'pwg.tags.getAdminList'
128    );
129
130    $result = $ua->get($query);
131    my $tags = from_json($result->content)->{result}{tags};
132
133    foreach my $tag (@{$tags}) {
134        # print join(',', keys %{$tag}), "\n"; exit();
135        printf(
136            '{%u} %s ',
137            $tag->{id},
138            $tag->{name}
139        );
140    }
141
142    print "\n";
143}
144
145if ($opt{action} eq 'pwg.categories.add') {
146    $form = {
147        method => 'pwg.categories.add',
148        name => $opt{define}{name},
149        parent => $opt{define}{parent},
150    };
151
152    my $response = $ua->post(
153        $conf{base_url}.'/ws.php?format=json',
154        $form
155    );
156
157    use Data::Dumper;
158    print Dumper(from_json($response->content));
159}
160
161if ($opt{action} eq 'pwg.tags.add') {
162    $form = {
163        method => 'pwg.tags.add',
164        name => $opt{define}{name},
165    };
166
167    my $response = $ua->post(
168        $conf{base_url}.'/ws.php?format=json',
169        $form
170    );
171
172    use Data::Dumper;
173    print Dumper(from_json($response->content));
174}
175
176if ($opt{action} eq 'pwg.images.exist') {
177    $form = {
178        method => $opt{action},
179    };
180
181    foreach my $key (keys %{ $opt{define} }) {
182        $form->{$key} = $opt{define}{$key};
183    }
184
185    my $response = $ua->post(
186        $conf{base_url}.'/ws.php?format=json',
187        $form
188    );
189
190    use Data::Dumper;
191    print Dumper(from_json($response->content)->{result});
192    # print Dumper($response);
193}
194
195$query = pwg_ws_get_query(
196    method => 'pwg.session.logout'
197);
198$ua->get($query);
199
200sub pwg_ws_get_query {
201    my %params = @_;
202
203    my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format};
204
205    foreach my $key (keys %params) {
206        $query .= '&'.$key.'='.$params{$key};
207    }
208
209    return $query;
210}
Note: See TracBrowser for help on using the repository browser.