1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | use JSON; |
---|
7 | use LWP::UserAgent; |
---|
8 | use Getopt::Long; |
---|
9 | |
---|
10 | my %opt = (); |
---|
11 | GetOptions( |
---|
12 | \%opt, |
---|
13 | qw/action=s file=s thumbnail=s categories=s define=s%/ |
---|
14 | ); |
---|
15 | |
---|
16 | our $ua = LWP::UserAgent->new; |
---|
17 | $ua->cookie_jar({}); |
---|
18 | |
---|
19 | my %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 | |
---|
26 | my $result = undef; |
---|
27 | my $query = undef; |
---|
28 | |
---|
29 | binmode STDOUT, ":encoding(utf-8)"; |
---|
30 | |
---|
31 | # TODO : don't connect at each script call, use the session duration instead. |
---|
32 | my $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 | |
---|
45 | if ($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 | |
---|
93 | if ($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 | |
---|
119 | if ($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 | |
---|
139 | if ($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 | |
---|
160 | sub 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 | } |
---|