root/trunk/tools/piwigo_remote.pl @ 2519

Revision 2519, 3.0 KB (checked in by plg, 5 years ago)

bug fixed: following r2516, remove partner from piwigo_remote.pl, the
argument is not useful any longer.

  • Property svn:eol-style set to LF
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use JSON;
7use LWP::UserAgent;
8use Getopt::Long;
9
10my %opt = ();
11GetOptions(
12    \%opt,
13    qw/action=s file=s thumbnail=s category_id=i name=s/
14);
15
16our $ua = LWP::UserAgent->new;
17$ua->cookie_jar({});
18
19my %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
26my $result = undef;
27my $query = undef;
28
29binmode STDOUT, ":encoding(utf-8)";
30
31# TODO : don't connect at each script call, use the session duration instead.
32my $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
45if ($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    };
65
66    my $response = $ua->post(
67        $conf{base_url}.'/ws.php?format=json',
68        $form
69    );
70
71    print "-" x 50, "\n";
72    printf("response code    : %u\n", $response->code);
73    printf("response message : %s\n", $response->message);
74    print "-" x 50, "\n";
75    print "\n";
76
77#     use Data::Dumper;
78#     print Dumper($response);
79
80    if ($response->is_success) {
81        print "upload successful\n";
82    }
83    else {
84        warn 'A problem has occured during upload', "\n";
85        warn $response->decoded_content, "\n";
86        die $response->status_line;
87    }
88}
89
90if ($opt{action} eq 'pwg.tags.list') {
91    use Text::ASCIITable;
92
93    $query = pwg_ws_get_query(
94        method => 'pwg.tags.getList',
95        sort_by_counter => 'true',
96    );
97
98    $result = $ua->get($query);
99    my $tag_result = from_json($result->content);
100    my $t = Text::ASCIITable->new({ headingText => 'Tags' });
101    $t->setCols('id','counter','name');
102
103    my $tag_number = 1;
104    foreach my $tag_href (@{ $tag_result->{result}{tags} }) {
105        $t->addRow(
106            $tag_href->{id},
107            $tag_href->{counter},
108            $tag_href->{name}
109        );
110
111        last if $tag_number++ >= $conf{limit};
112    }
113    print $t;
114}
115
116$query = pwg_ws_get_query(
117    method => 'pwg.session.logout'
118);
119$ua->get($query);
120
121sub pwg_ws_get_query {
122    my %params = @_;
123
124    my $query = $conf{base_url}.'/ws.php?format='.$conf{response_format};
125
126    foreach my $key (keys %params) {
127        $query .= '&'.$key.'='.$params{$key};
128    }
129
130    return $query;
131}
Note: See TracBrowser for help on using the browser.