1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | use JSON; |
---|
7 | use LWP::UserAgent; |
---|
8 | use Getopt::Long; |
---|
9 | use Encode qw/is_utf8 decode/; |
---|
10 | |
---|
11 | my %opt = (); |
---|
12 | GetOptions( |
---|
13 | \%opt, |
---|
14 | qw/action=s file=s thumbnail=s high=s categories=s define=s%/ |
---|
15 | ); |
---|
16 | |
---|
17 | our $ua = LWP::UserAgent->new; |
---|
18 | $ua->cookie_jar({}); |
---|
19 | |
---|
20 | my %conf; |
---|
21 | $conf{base_url} = 'http://localhost/~pierrick/piwigo/2.0'; |
---|
22 | $conf{response_format} = 'json'; |
---|
23 | $conf{username} = 'pierrick'; |
---|
24 | $conf{password} = 'z0rglub'; |
---|
25 | $conf{limit} = 10; |
---|
26 | |
---|
27 | my $result = undef; |
---|
28 | my $query = undef; |
---|
29 | |
---|
30 | binmode STDOUT, ":encoding(utf-8)"; |
---|
31 | |
---|
32 | # TODO : don't connect at each script call, use the session duration instead. |
---|
33 | my $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 | |
---|
46 | if ($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 | |
---|
99 | if ($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 | |
---|
125 | if ($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 | |
---|
145 | if ($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 | |
---|
161 | if ($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 | |
---|
176 | if ($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 | if ($opt{action} eq 'pwg.images.setInfo') { |
---|
196 | $form = { |
---|
197 | method => $opt{action}, |
---|
198 | }; |
---|
199 | |
---|
200 | foreach my $key (keys %{ $opt{define} }) { |
---|
201 | $form->{$key} = $opt{define}{$key}; |
---|
202 | } |
---|
203 | |
---|
204 | my $response = $ua->post( |
---|
205 | $conf{base_url}.'/ws.php?format=json', |
---|
206 | $form |
---|
207 | ); |
---|
208 | |
---|
209 | use Data::Dumper; |
---|
210 | # print Dumper(from_json($response->content)->{result}); |
---|
211 | print Dumper($response); |
---|
212 | } |
---|
213 | |
---|
214 | $query = pwg_ws_get_query( |
---|
215 | method => 'pwg.session.logout' |
---|
216 | ); |
---|
217 | $ua->get($query); |
---|
218 | |
---|
219 | sub pwg_ws_get_query { |
---|
220 | my %params = @_; |
---|
221 | |
---|
222 | my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format}; |
---|
223 | |
---|
224 | foreach my $key (keys %params) { |
---|
225 | $query .= '&'.$key.'='.$params{$key}; |
---|
226 | } |
---|
227 | |
---|
228 | return $query; |
---|
229 | } |
---|