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 category_id=i name=s rank=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 | category_id => $opt{category_id}, |
---|
63 | name => $opt{name}, |
---|
64 | rank => defined($opt{rank}) ? $opt{rank} : 'auto', |
---|
65 | }; |
---|
66 | |
---|
67 | my $response = $ua->post( |
---|
68 | $conf{base_url}.'/ws.php?format=json', |
---|
69 | $form |
---|
70 | ); |
---|
71 | |
---|
72 | print "-" x 50, "\n"; |
---|
73 | printf("response code : %u\n", $response->code); |
---|
74 | printf("response message : %s\n", $response->message); |
---|
75 | print "-" x 50, "\n"; |
---|
76 | print "\n"; |
---|
77 | |
---|
78 | # use Data::Dumper; |
---|
79 | # print Dumper($response); |
---|
80 | |
---|
81 | if ($response->is_success) { |
---|
82 | print "upload successful\n"; |
---|
83 | } |
---|
84 | else { |
---|
85 | warn 'A problem has occured during upload', "\n"; |
---|
86 | warn $response->decoded_content, "\n"; |
---|
87 | die $response->status_line; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | if ($opt{action} eq 'pwg.tags.list') { |
---|
92 | use Text::ASCIITable; |
---|
93 | |
---|
94 | $query = pwg_ws_get_query( |
---|
95 | method => 'pwg.tags.getList', |
---|
96 | sort_by_counter => 'true', |
---|
97 | ); |
---|
98 | |
---|
99 | $result = $ua->get($query); |
---|
100 | my $tag_result = from_json($result->content); |
---|
101 | my $t = Text::ASCIITable->new({ headingText => 'Tags' }); |
---|
102 | $t->setCols('id','counter','name'); |
---|
103 | |
---|
104 | my $tag_number = 1; |
---|
105 | foreach my $tag_href (@{ $tag_result->{result}{tags} }) { |
---|
106 | $t->addRow( |
---|
107 | $tag_href->{id}, |
---|
108 | $tag_href->{counter}, |
---|
109 | $tag_href->{name} |
---|
110 | ); |
---|
111 | |
---|
112 | last if $tag_number++ >= $conf{limit}; |
---|
113 | } |
---|
114 | print $t; |
---|
115 | } |
---|
116 | |
---|
117 | $query = pwg_ws_get_query( |
---|
118 | method => 'pwg.session.logout' |
---|
119 | ); |
---|
120 | $ua->get($query); |
---|
121 | |
---|
122 | sub pwg_ws_get_query { |
---|
123 | my %params = @_; |
---|
124 | |
---|
125 | my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format}; |
---|
126 | |
---|
127 | foreach my $key (keys %params) { |
---|
128 | $query .= '&'.$key.'='.$params{$key}; |
---|
129 | } |
---|
130 | |
---|
131 | return $query; |
---|
132 | } |
---|