1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
5 | # +-----------------------------------------------------------------------+ |
---|
6 | # | This program is free software; you can redistribute it and/or modify | |
---|
7 | # | it under the terms of the GNU General Public License as published by | |
---|
8 | # | the Free Software Foundation | |
---|
9 | # | | |
---|
10 | # | This program is distributed in the hope that it will be useful, but | |
---|
11 | # | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
12 | # | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
13 | # | General Public License for more details. | |
---|
14 | # | | |
---|
15 | # | You should have received a copy of the GNU General Public License | |
---|
16 | # | along with this program; if not, write to the Free Software | |
---|
17 | # | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
18 | # | USA. | |
---|
19 | # +-----------------------------------------------------------------------+ |
---|
20 | package Uploader::PWG::WebServices; |
---|
21 | |
---|
22 | use strict; |
---|
23 | use warnings; |
---|
24 | use MIME::Base64; |
---|
25 | use JSON; |
---|
26 | use LWP::UserAgent; |
---|
27 | use Data::Dumper; |
---|
28 | use Digest::MD5::File qw/file_md5_hex md5_hex/; |
---|
29 | use File::Slurp; |
---|
30 | use Encode qw/encode decode/; |
---|
31 | use base qw/ |
---|
32 | Uploader::Object |
---|
33 | Class::Accessor::Fast |
---|
34 | /; |
---|
35 | |
---|
36 | __PACKAGE__->mk_accessors( |
---|
37 | qw/ |
---|
38 | uagent |
---|
39 | urlbase |
---|
40 | username |
---|
41 | password |
---|
42 | qry_list_categories |
---|
43 | qry_add_categories |
---|
44 | items |
---|
45 | tags |
---|
46 | categories |
---|
47 | site_high_file |
---|
48 | site_resized_file |
---|
49 | site_thumb_file |
---|
50 | site_image_name |
---|
51 | rank |
---|
52 | site_author |
---|
53 | site_comment |
---|
54 | site_img_date_creation |
---|
55 | uagent_response |
---|
56 | login_result |
---|
57 | action_result |
---|
58 | upload_high |
---|
59 | / |
---|
60 | ); |
---|
61 | |
---|
62 | $|=1; |
---|
63 | |
---|
64 | sub Init { |
---|
65 | my ( $self ) = @_ ; |
---|
66 | |
---|
67 | $self->uagent( |
---|
68 | LWP::UserAgent->new |
---|
69 | ); |
---|
70 | |
---|
71 | $self->uagent->cookie_jar({}); |
---|
72 | |
---|
73 | $self->urlbase( |
---|
74 | $self->{site_url} |
---|
75 | ); |
---|
76 | |
---|
77 | $self->username( |
---|
78 | $self->{site_username} |
---|
79 | ); |
---|
80 | |
---|
81 | $self->password( |
---|
82 | $self->{site_password} |
---|
83 | ); |
---|
84 | |
---|
85 | $self->qry_list_categories( sprintf |
---|
86 | "%s/ws.php?format=json&method=%s&recursive=%s", |
---|
87 | $self->urlbase, |
---|
88 | 'pwg.categories.getAdminList', |
---|
89 | 'true', |
---|
90 | ); |
---|
91 | |
---|
92 | my $form = { |
---|
93 | method => 'pwg.session.login', |
---|
94 | username => $self->username, |
---|
95 | password => $self->password, |
---|
96 | }; |
---|
97 | |
---|
98 | $self->uagent_response( |
---|
99 | $self->uagent->post( |
---|
100 | $self->urlbase.'/ws.php?format=json', |
---|
101 | $form |
---|
102 | ) |
---|
103 | ); |
---|
104 | |
---|
105 | my $hresult = {} ; |
---|
106 | |
---|
107 | if($self->uagent_response->is_success){ |
---|
108 | eval { |
---|
109 | $hresult = from_json( |
---|
110 | $self->uagent_response->content |
---|
111 | ); |
---|
112 | }; |
---|
113 | } |
---|
114 | else{ |
---|
115 | $hresult = { |
---|
116 | 'message' => $self->uagent_response->message, |
---|
117 | 'stat' => 'fail', |
---|
118 | }; |
---|
119 | } |
---|
120 | |
---|
121 | $self->login_result( |
---|
122 | $hresult |
---|
123 | ); |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | sub GetCategories { |
---|
128 | my ( $self ) = @_; |
---|
129 | |
---|
130 | my $result; |
---|
131 | eval { |
---|
132 | $result = $self->uagent->get( |
---|
133 | $self->qry_list_categories |
---|
134 | ); |
---|
135 | }; |
---|
136 | |
---|
137 | if($@){ |
---|
138 | printf("An error occured in query execution %s\n%s", |
---|
139 | $self->qry_list_categories, |
---|
140 | $@, |
---|
141 | ); |
---|
142 | } |
---|
143 | my $hresult; |
---|
144 | |
---|
145 | eval { |
---|
146 | $hresult = from_json( |
---|
147 | $result->content |
---|
148 | ); |
---|
149 | }; |
---|
150 | |
---|
151 | $hresult ||= {}; |
---|
152 | my $categories = $hresult->{result}{categories}; |
---|
153 | |
---|
154 | |
---|
155 | $categories; |
---|
156 | } |
---|
157 | |
---|
158 | sub UploadImage { |
---|
159 | my ( $self ) = @_; |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | my $file_content = encode_base64( |
---|
164 | read_file( |
---|
165 | $self->site_resized_file, |
---|
166 | binmode => ':raw', |
---|
167 | ) |
---|
168 | ); |
---|
169 | |
---|
170 | my $file_sum = file_md5_hex( |
---|
171 | $self->site_resized_file |
---|
172 | ); |
---|
173 | |
---|
174 | my $thumbnail_content = encode_base64( |
---|
175 | read_file( |
---|
176 | $self->site_thumb_file, |
---|
177 | binmode => ':raw', |
---|
178 | ) |
---|
179 | ); |
---|
180 | |
---|
181 | my $thumbnail_sum = file_md5_hex( |
---|
182 | $self->site_thumb_file, |
---|
183 | ); |
---|
184 | |
---|
185 | my $original_sum = file_md5_hex( |
---|
186 | $self->site_high_file |
---|
187 | ); |
---|
188 | |
---|
189 | |
---|
190 | my $form = { |
---|
191 | method => 'pwg.images.add', |
---|
192 | original_sum => $original_sum, |
---|
193 | file_sum => $file_sum, |
---|
194 | file_content => $file_content, |
---|
195 | thumbnail_sum => $thumbnail_sum, |
---|
196 | thumbnail_content => $thumbnail_content, |
---|
197 | categories => $self->categories, |
---|
198 | name => $self->site_image_name, |
---|
199 | rank => $self->rank, |
---|
200 | author => $self->site_author, |
---|
201 | comment => $self->site_comment, |
---|
202 | date_creation => $self->site_img_date_creation, |
---|
203 | |
---|
204 | }; |
---|
205 | |
---|
206 | if($self->upload_high){ |
---|
207 | $form->{high_content} = encode_base64( |
---|
208 | read_file( |
---|
209 | $self->site_high_file, |
---|
210 | binmode => ':raw', |
---|
211 | ) |
---|
212 | ); |
---|
213 | |
---|
214 | $form->{high_sum} = $original_sum; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | my $result = $self->uagent->post( |
---|
219 | $self->urlbase.'/ws.php?format=json', |
---|
220 | $form |
---|
221 | ); |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | return ( $result->is_success, $result->status_line ); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | sub AddCategories{ |
---|
230 | my ( $self, $name, $parentid ) = @_; |
---|
231 | |
---|
232 | my $form = { |
---|
233 | method => 'pwg.categories.add', |
---|
234 | name => $name, |
---|
235 | parent => $parentid, |
---|
236 | |
---|
237 | }; |
---|
238 | |
---|
239 | my $result = $self->uagent->post( |
---|
240 | $self->urlbase.'/ws.php?format=json', |
---|
241 | $form |
---|
242 | ); |
---|
243 | |
---|
244 | return ( $result->is_success, $result->status_line ); |
---|
245 | |
---|
246 | } |
---|
247 | 1; |
---|
248 | |
---|