Z
Z
zikju2016-08-09 15:54:23
Perl
zikju, 2016-08-09 15:54:23

Why doesn't the module for uploading pictures to free hostings work?

Hello gentlemen!
I need help from those who know PERL, because I myself am not strong in it.
The module responsible for uploading images to free image hosting sites (fastpic.ru, imgur.com, trl.lt) has stopped working. It is possible that the API of the services themselves has changed.
Ideally, it would be possible to set the fill at least on trl.lt.
Maybe by looking at the code you could easily determine what the trouble is?
Thanks for any advice.
Base.pm

package Local::Image::Upload::Base;

use strict;
use warnings;
use Carp;

use Local::Image::Upload::Pico;
use Local::Image::Upload::Imgur;
use Local::Image::Upload::Fastpic;

our $VERSION = '0.01';

sub new {
  my ($class, %options) = @_;
  $class = ref $class if ref $class;
  my $self = bless {
    _filename 	=> undef,
    _size 		=> undef,
  }, $class;

  if (%options) {
    if (exists $options{file}) {
      $self->set_filename($options{file});
    }

    if (exists $options{size}) {
      $self->resize($options{size});
    }
  }

  return $self;
}

sub resize {
  my $self = shift;
  my $width = shift || 340;
  my $height = shift || '';

  my $file = $self->filename || carp "File name is not defined";
  return system("convert $file -resize ${width}x${height} $file");
}

sub filename {
  my $self = shift;
  return $self->{_filename};
}

sub set_filename {
  my($self, $filename) = @_;
  $self->{_filename} = $filename;
}

sub thumbnail {
  my $self=  shift;
  my $size = shift || 140;

  my $pic_full = $self->upload;
  $self->resize($size);
  my $pic_thumbnail = $self->upload;

  return $pic_thumbnail, $pic_full if defined($pic_thumbnail) && defined($pic_full);
}

sub upload {
  my $self = shift;

  my $file = $self->filename;

  foreach my $service ('Imgur', 'TRL', 'Fastpic') {
    my $img = "Local::Image::Upload::$service"->new(
      file => $file,
    );

    my $result = $img->upload;
    return $result if $result;
  }
}

sub remove {
  my $self = shift;
  unlink $self->filename if -e $self->filename;
}

1;

fastpic.pm:
package Local::Image::Upload::Fastpic;

use strict;
use warnings;
use Carp;
use LWP::UserAgent;

use lib 'modules';
use lib '/home/desktop/perl/modules';
use Local::Image::Upload::Base;

our @ISA = qw(Local::Image::Upload::Base);
our $VERSION = '0.01';

sub upload {
  my $self = shift;
  my $file = $self->filename || carp "File name is not defined";

    my $ua = LWP::UserAgent->new;
    my $res = $ua->post('http://fastpic.ru/uploadmulti', {
      'file[]' 			=> [ $file ],
      'thumb_text' 		=> '',
      'check_thumb' 	=> 'no',
      'thumb_size' 		=> 170,
      'res_select' 		=> 500,
      'orig_resize' 	=> 500,
      'orig_rotate' 	=> '0',
      'jpeg_quality' 	=> '75',
      'submit' 			=> 'Загрузить',
      'uploading' 		=> '1',
    }, 'Content_Type' 	=> 'form-data');

    if ($res->is_success) { 
        if($res->as_string =~ m{Refresh: 0;url=(.*?)\n}) { 
            my $location = $1;
            my $res = $ua->get($location);
            
            if ($res->content =~ m{\[URL=http://fastpic.ru/]\[IMG](.*?)\[/IMG]\[/URL]}) {
            	return $1;
            }
        }
    }
    return '';
}

1;

imgur.pm:
package Local::Image::Upload::Imgur;

use strict;
use warnings;
use Carp;
use LWP::UserAgent;

use lib 'modules';
use lib '/home/desktop/perl/modules';
use Local::Image::Upload::Base;

our @ISA = qw(Local::Image::Upload::Base);
our $VERSION = '0.01';

sub upload {
  my $self = shift;
  my $file = $self->filename || carp "File name is not defined";

  my $ua = LWP::UserAgent->new;

  my $res = $ua->post(
    'http://api.imgur.com/2/upload.xml',
    [
      'image' => [ $file ], 
      'key' 	=> '30d4c4346e86c0b9af3012547d606140',
    ],
        'Content_Type' => 'form-data'
  );

  if($res->content =~ /<original>(.*?)<\/original>/)	{
    return $1;
  } else {
    carp "Failed to upload image $file";
    return "";
  }
}

1;

TRL.pm
package Local::Image::Upload::TRL;

use strict;
use warnings;
use Carp;
use LWP::UserAgent;

use lib 'modules';
use lib '/home/desktop/perl/modules';
use Local::Image::Upload::Base;

our @ISA = qw(Local::Image::Upload::Base);
our $VERSION = '0.01';

sub upload {
  my $self = shift;
  my $file = $self->filename || carp "File name is not defined";

  my $ua = LWP::UserAgent->new;

  my $res = $ua->post(
    'http://trl.lt/upload.php',
    [
      'file[]' => [ $file ],
      'submit' => 'submit'
    ],
        'Content_Type' => 'form-data'
  );

  if($res->content =~ /<original>(.*?)<\/original>/)	{
    return $1;
  } else {
    carp "Failed to upload image $file";
    return "";
  }
}

1;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question