From Robert DeWeese, 3 Years ago, written in Perl.
Embed
  1. #! /usr/bin/env perl
  2. ###################################################
  3. #
  4. #  i.nw2.us Shutter Plugin
  5. #  Save to /usr/share/shutter/resources/system/upload_plugins/upload/nw2.pm
  6. #  Requires Slurp - ubuntu: sudo apt-get install libfile-slurp-perl
  7. #  Copyright (C) 2013 Robert DeWeese rob@nw2.us
  8. #
  9. #  This file is part of Shutter.
  10. #
  11. #  Shutter is free software; you can redistribute it and/or modify
  12. #  it under the terms of the GNU General Public License as published by
  13. #  the Free Software Foundation; either version 3 of the License, or
  14. #  (at your option) any later version.
  15. #
  16. #  Shutter is distributed in the hope that it will be useful,
  17. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. #  GNU General Public License for more details.
  20. #
  21. #  You should have received a copy of the GNU General Public License
  22. #  along with Shutter; if not, write to the Free Software
  23. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24. #
  25. ###################################################
  26.  
  27. package nw2;                                                       #edit
  28.  
  29. use lib $ENV{'SHUTTER_ROOT'}.'/share/shutter/resources/modules';
  30.  
  31. use utf8;
  32. use strict;
  33. use POSIX qw/setlocale/;
  34. use Locale::gettext;
  35. use Glib qw/TRUE FALSE/;
  36. use Data::Dumper;
  37. use File::Slurp;
  38.  
  39. use Shutter::Upload::Shared;
  40. our @ISA = qw(Shutter::Upload::Shared);
  41.  
  42. my $d = Locale::gettext->domain("shutter-plugins");
  43. $d->dir( $ENV{'SHUTTER_INTL'} );
  44.  
  45. my %upload_plugin_info = (
  46.     'module'                        => "nw2",                       #edit (must be the same as 'package')
  47.     'url'                           => "http://i.nw2.us/",           #edit (the website's url)
  48.     'registration'                  => "-",   #edit (a link to the registration page)
  49.     'name'                          => "i.nw2.us",                       #edit (the provider's name)
  50.     'description'                   => "Upload screenshots to i.nw2.us",#edit (a description of the service)
  51.     'supports_anonymous_upload'     => TRUE,                         #TRUE if you can upload *without* username/password
  52.     'supports_authorized_upload'    => FALSE,                         #TRUE if username/password are supported (might be in addition to anonymous_upload)
  53.     'supports_oauth_upload'         => FALSE,                            #TRUE if OAuth is used (see Dropbox.pm as an example)
  54. );
  55.  
  56. binmode( STDOUT, ":utf8" );
  57. if ( exists $upload_plugin_info{$ARGV[ 0 ]} ) {
  58.     print $upload_plugin_info{$ARGV[ 0 ]};
  59.     exit;
  60. }
  61.  
  62.  
  63. #don't touch this
  64. sub new {
  65.     my $class = shift;
  66.  
  67.     #call constructor of super class (host, debug_cparam, shutter_root, gettext_object, main_gtk_window, ua)
  68.     my $self = $class->SUPER::new( shift, shift, shift, shift, shift, shift );
  69.  
  70.     bless $self, $class;
  71.     return $self;
  72. }
  73.  
  74. #load some custom modules here (or do other custom stuff)  
  75. sub init {
  76.     my $self = shift;
  77.  
  78.     use JSON;                   #example1
  79.     use LWP::UserAgent;         #example2
  80.     use HTTP::Request::Common;  #example3
  81.     #use HTTP::Request::Common 'POST';
  82.  
  83.  
  84.  
  85.     return TRUE;    
  86. }
  87.  
  88. #handle
  89. sub upload {
  90.     my ( $self, $upload_filename, $username, $password ) = @_;
  91.  
  92.     #store as object vars
  93.     $self->{_filename} = $upload_filename;
  94.     $self->{_username} = $username;
  95.     $self->{_password} = $password;
  96.  
  97.     utf8::encode $upload_filename;
  98.     utf8::encode $password;
  99.     utf8::encode $username;
  100.  
  101.     #examples related to the sub 'init'
  102.     my $json_coder = JSON::XS->new;
  103.  
  104.     my $browser = LWP::UserAgent->new(
  105.         'timeout'    => 20,
  106.         'keep_alive' => 10,
  107.         'env_proxy'  => 1,
  108.     );
  109.      
  110.     #username/password are provided
  111.     if ( $username ne "" && $password ne "" ) {
  112.  
  113.         eval{
  114.  
  115.             ########################
  116.             #put the login code here
  117.             ########################
  118.              
  119.             #if login failed (status code == 999) => Shutter will display an appropriate message
  120.             #unless($login == 'success'){
  121.             #   $self->{_links}{'status'} = 999;
  122.             #   return;
  123.             #}
  124.  
  125.         };
  126.         if($@){
  127.             $self->{_links}{'status'} = $@;
  128.             return %{ $self->{_links} };
  129.         }
  130.         if($self->{_links}{'status'} == 999){
  131.             return %{ $self->{_links} };
  132.         }
  133.          
  134.     }
  135.      
  136.     #upload the file
  137.     eval{
  138.  
  139.         #########################
  140.         #put the upload code here
  141.         #########################
  142. my $ua = LWP::UserAgent->new;
  143. $ua->timeout(10);
  144. my $url = "http://i.nw2.us/api/upload/json";
  145. my $picture = $upload_filename;
  146. my %args;
  147. my $field_name = "image";
  148. my $buf ;
  149. my $buf_ref = $args{'buf'} || \$buf ;
  150.  
  151.  
  152. my $value = read_file( $picture , binmode => ':raw' , scalar_ref => 1 );
  153.  
  154. my $response = $ua->post( $url,
  155.                         Content_Type => 'form-data',
  156.                         Content => [ $field_name => ["$picture"] ]
  157.                         );
  158. print $response->content;
  159. my $decoded_json = decode_json( $response->content );
  160.  
  161.         #End dicking around
  162.          
  163.         #save all retrieved links to a hash, as an example:
  164.         $self->{_links}->{'direct_link'} = $decoded_json->{'link'};
  165.         $self->{_links}->{'delete_link'} = $decoded_json->{'delete'};
  166.         #$self->{_links}->{'bbcode'} = 'mylink3';
  167.  
  168.         #set success code (200)
  169.         $self->{_links}{'status'} = 200;
  170.          
  171.     };
  172.     if($@){
  173.         $self->{_links}{'status'} = $@;
  174.     }
  175.      
  176.     #and return links
  177.     return %{ $self->{_links} };
  178. }
  179.  
  180.  
  181. #you are free to implement some custom subs here, but please make sure they don't interfere with Shutter's subs
  182. #hence, please follow this naming convention: _<provider>_sub (e.g. _imageshack_convert_x_to_y)
  183.  
  184.  
  185. 1;
  186.