@zikju

Почему не работает модуль загрузки картинок на бесплатные хостинги?

Здравствуйте, господа!
Нужна помощь от знающих PERL, ибо сам я не силен в нем.
Перестал работать модуль, отвечающий за загрузку картинок на бесплатные image-хостинги (fastpic.ru, imgur.com, trl.lt). Не исключено, что у самих сервисов поменялся API.
В идеале было бы настроить заливку хотя бы на trl.lt
Может взглянув на код вы бы запросто определили в чем беда?
Спасибо за любые советы.

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;
  • Вопрос задан
  • 225 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы