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