-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageBot.pm
executable file
·147 lines (116 loc) · 3.64 KB
/
ImageBot.pm
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
#! /usr/bin/env perl
###################################################
#
# Copyright (C) 2013 Max Méndez <[email protected]>
#
# This file is part of Shutter.
#
# Shutter is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Shutter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Shutter; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
###################################################
package ImageBot;
use lib $ENV{'SHUTTER_ROOT'}.'/share/shutter/resources/modules';
use utf8;
use strict;
use POSIX qw/setlocale/;
use Locale::gettext;
use Glib qw/TRUE FALSE/;
use Data::Dumper;
use Shutter::Upload::Shared;
our @ISA = qw(Shutter::Upload::Shared);
my $d = Locale::gettext->domain("shutter-upload-plugins");
$d->dir( $ENV{'SHUTTER_INTL'} );
my %upload_plugin_info = (
'module' => "ImageBot",
'url' => "http://www.imagebot.net/",
'registration' => "http://www.imagebot.net/user/register",
'description' => $d->get("Upload screenshots to ImageBot"),
'supports_anonymous_upload' => TRUE,
'supports_authorized_upload' => TRUE,
);
binmode( STDOUT, ":utf8" );
if ( exists $upload_plugin_info{$ARGV[ 0 ]} ) {
print $upload_plugin_info{$ARGV[ 0 ]};
exit;
}
#don't touch this
sub new {
my $class = shift;
#call constructor of super class (host, debug_cparam, shutter_root, gettext_object, main_gtk_window, ua)
my $self = $class->SUPER::new( shift, shift, shift, shift, shift, shift );
bless $self, $class;
return $self;
}
sub init {
my $self = shift;
use JSON;
use LWP::UserAgent;
use HTTP::Request::Common;
return TRUE;
}
sub upload {
my ( $self, $upload_filename, $username, $password ) = @_;
#store as object vars
$self->{_filename} = $upload_filename;
$self->{_username} = $username;
$self->{_password} = $password;
utf8::encode $upload_filename;
utf8::encode $password;
utf8::encode $username;
#examples related to the sub 'init'
my $json_coder = JSON::XS->new;
my $browser = LWP::UserAgent->new(
'timeout' => 20,
'keep_alive' => 10,
'env_proxy' => 1,
);
my $client = LWP::UserAgent->new(
'timeout' => 30,
'keep_alive' => 10,
'env_proxy' => 1,
);
eval{
my %params = (
'file' => [$upload_filename],
'api_username' => $username,
'api_password' => $password,
);
my @params = (
"http://www.imagebot.net/upload_img_api",
'Content_Type' => 'multipart/form-data',
'Content' => [%params]
);
my $req = HTTP::Request::Common::POST(@params);
push @{ $client->requests_redirectable }, 'POST';
my $rsp = $client->request($req);
#convert JSON
my $ref = decode_json($rsp->content);
unless(defined $ref->{'error'}){
foreach (keys %{$ref}){
$self->{_links}->{$_} = $ref->{$_};
}
#set status (success)
$self->{_links}{'status'} = 200;
}else{
$self->{_links}{'status'} = $ref->{'error'};
}
};
if($@){
$self->{_links}{'status'} = $@;
}
#and return links
return %{ $self->{_links} };
}
1;