Advertisement
0x255

itmages.ru shutter upload plugin

Feb 26th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.77 KB | None | 0 0
  1. #! /usr/bin/env perl
  2. ###################################################
  3. #
  4. #  Copyright (C) 2010-2012 Mario Kemper <[email protected]> and Shutter Team
  5. #
  6. #  This file is part of Shutter.
  7. #
  8. #  Shutter is free software; you can redistribute it and/or modify
  9. #  it under the terms of the GNU General Public License as published by
  10. #  the Free Software Foundation; either version 3 of the License, or
  11. #  (at your option) any later version.
  12. #
  13. #  Shutter is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with Shutter; if not, write to the Free Software
  20. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21. #
  22. ###################################################
  23.  
  24. package ITmages_auth;
  25.  
  26. use lib $ENV{'SHUTTER_ROOT'}.'/share/shutter/resources/modules';
  27.  
  28. use utf8;
  29. use strict;
  30. use POSIX qw/setlocale/;
  31. use Locale::gettext;
  32. use Glib qw/TRUE FALSE/;
  33.  
  34. use Shutter::Upload::Shared;
  35. our @ISA = qw(Shutter::Upload::Shared);
  36.  
  37. my $d = Locale::gettext->domain("shutter-upload-plugins");
  38. $d->dir( $ENV{'SHUTTER_INTL'} );
  39.  
  40. my %upload_plugin_info = (
  41.     'module'                        => "ITmages_auth",
  42.     'url'                           => "http://itmages.ru/",
  43.     'registration'                  => "http://itmages.ru/user/register",
  44.     'description'                   => $d->get( "Upload screenshots to itmages.ru" ),
  45.     'supports_anonymous_upload'     => TRUE,
  46.     'supports_authorized_upload'    => TRUE,
  47.     'supports_oauth_upload'         => FALSE,                           #TRUE if OAuth is used (see Dropbox.pm as an example)
  48. );
  49.  
  50. binmode( STDOUT, ":utf8" );
  51. if ( exists $upload_plugin_info{$ARGV[ 0 ]} ) {
  52.     print $upload_plugin_info{$ARGV[ 0 ]};
  53.     exit;
  54. }
  55.  
  56.  
  57. #don't touch this
  58. sub new {
  59.     my $class = shift;
  60.  
  61.     #call constructor of super class (host, debug_cparam, shutter_root, gettext_object, main_gtk_window, ua)
  62.     my $self = $class->SUPER::new( shift, shift, shift, shift, shift, shift );
  63.  
  64.     bless $self, $class;
  65.     return $self;
  66. }
  67.  
  68. #load some custom modules here (or do other custom stuff)  
  69. sub init {
  70.     my $self = shift;
  71.  
  72.     use JSON;
  73.     use LWP::UserAgent;
  74.     use HTTP::Request::Common;
  75.    
  76.     return TRUE;
  77. }
  78.  
  79. #handle
  80. sub upload {
  81.     my ( $self, $upload_filename, $username, $password ) = @_;
  82.  
  83.     utf8::encode $upload_filename;
  84.     utf8::encode $password;
  85.     utf8::encode $username;
  86.  
  87.     #examples related to the sub 'init'
  88.     my $json_coder = JSON::XS->new;
  89.  
  90.     my $client = LWP::UserAgent->new();
  91.  
  92.     eval{
  93.  
  94.         my @opts;
  95.         push(@opts, 'Content-Type' => 'form-data');
  96.         push(@opts, 'X-Username' => $username) if $username;
  97.         push(@opts, 'X-Password' => $password) if $password;
  98.         push(@opts, Content => ["file" => [ $upload_filename, $upload_filename, content_type => 'image/png' ]]);
  99.  
  100.         my $request = HTTP::Request::Common::POST('https://itmages.com/api/v3/pictures/', @opts);
  101.         push @{ $client->requests_redirectable }, 'POST';
  102.         my $response = $client->request($request);
  103.  
  104.         my $picture = from_json( $response->content  )->{ success  };
  105.  
  106.         $self->{_links}->{'direct_link'} = 'http://'.$picture->{ storage  }.'.static.itmages.ru/'.$picture->{ picture  };
  107.         $self->{_links}->{'thumbnail'} = 'http://'.$picture->{ storage  }.'.static.itmages.ru/'.$picture->{ thumbnail  };
  108.        
  109.         $self->{_links}{'status'} = 200;                
  110.  
  111.     };
  112.     if($@){
  113.         $self->{_links}{'status'} = $@;
  114.         #~ print "$@\n";
  115.     }
  116.    
  117.     #and return links
  118.     return %{ $self->{_links} };
  119. }
  120.  
  121. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement