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