Created
October 11, 2015 12:36
-
-
Save jabb3rd/0d535a54440b787fb2c0 to your computer and use it in GitHub Desktop.
Shutter plugin for uploading images to trueimages.ru
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env perl | |
################################################### | |
# | |
# Copyleft (C) 2014 @jabberd | |
# | |
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
# | |
################################################### | |
package TrueImages; | |
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' => "TrueImages", | |
'url' => "http://trueimages.ru/", | |
'registration' => "-", | |
'description' => $d->get( "TrueImages.Ru free images hosting" ), | |
'supports_anonymous_upload' => TRUE, | |
'supports_authorized_upload' => FALSE, | |
); | |
binmode( STDOUT, ":utf8" ); | |
if ( exists $upload_plugin_info{$ARGV[ 0 ]} ) { | |
print $upload_plugin_info{$ARGV[ 0 ]}; | |
exit; | |
} | |
################################################### | |
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; | |
#do custom stuff here | |
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; | |
my $client = LWP::UserAgent->new( | |
'timeout' => 20, | |
'keep_alive' => 10, | |
'env_proxy' => 1, | |
); | |
eval{ | |
my $json = JSON->new(); | |
my %params = ( | |
'resize' => '0x0', | |
'quality' => '100', | |
'fileToUpload[]' => [$upload_filename], | |
); | |
my @params = ( | |
"http://trueimages.ru/upload.php?json", | |
'Content_Type' => 'multipart/form-data', | |
'Content' => [%params] | |
); | |
my $req = HTTP::Request::Common::POST(@params); | |
my $rsp = $client->request($req); | |
$self->{_links} = $json->decode($rsp->content); | |
$self->{_links} = $self->{_links}[0]; | |
# Delete size, width, height and add hostnames: | |
foreach (keys %{$self->{_links}}){ | |
if ($_ eq 'size' || $_ eq 'width' || $_ eq 'height'){ | |
delete $self->{_links}->{$_}; | |
next; | |
} | |
if ($_ eq 'shorturl') { | |
$self->{_links}->{$_} = "http://timg.in/".$self->{_links}->{$_}; | |
} | |
if ($_ eq 'img' || $_ eq 'thumb') { | |
$self->{_links}->{$_} = "http://trueimages.ru/".$self->{_links}->{$_}; | |
} | |
if( $self->{_debug_cparam}) { | |
print $_.": ".$self->{_links}->{$_}, "\n"; | |
} | |
} | |
#set status (success) | |
$self->{_links}{'status'} = 200; | |
}; | |
if($@){ | |
$self->{_links}{'status'} = $@; | |
#~ print "$@\n"; | |
} | |
#and return links | |
return %{ $self->{_links} }; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment