WebService-Rajce

 view release on metacpan or  search on metacpan

lib/WebService/Rajce.pm  view on Meta::CPAN

package WebService::Rajce;
# ABSTRACT: Perl module for rajce.net web API.
$WebService::Rajce::VERSION = '1.202830';
use 5.006;
use strict;
use warnings;

use WWW::Mechanize;
use XML::Simple;
use Digest::MD5 qw(md5_hex);
use Encode;
use Image::Magick;
use Carp;

use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;


our @ISA = qw(Exporter AutoLoader);
our @EXPORT = qw();

sub new {
	my $class = shift;
  my %passed_parms = @_;
	my $self  = {};
	$self->{API} = 'https://www.rajce.idnes.cz/liveAPI/index.php';
	$self->{XML} = '<?xml version="1.0" encoding="utf-8"?>';
	$self->{DEBUG} = $passed_parms{'debug'};
	$self->{KEEP_EXIF} = $passed_parms{'keep_exif'};
	$self->{BOT} = WWW::Mechanize->new(autocheck => 1, agent => 'github.com/petrkle/rajce - '.$VERSION);
	$self->{BOT}->env_proxy();
	$self->{BOT}->add_header('Accept-Encoding'=>'text/html');
	$self->{BOT}->add_header('Accept-Charset'=>'utf-8');
	$self->{BOT}->add_header('Accept-Language'=>'cs');
	$self->{BOT}->cookie_jar(HTTP::Cookies->new());

	$self->{ERRORS} = {
	'1' => 'Unknown error.',
	'2' => 'Invalid command.',
	'3' => 'Invalid login or password.',
	'4' => 'Bad login token.',
	'5' => 'Unknown or repeating column {colName}.',
	'6' => 'Not correct albumID.',
	'7' => 'Album not exist or logged user is not owner.',
	'8' => 'Bad album token.',
	'9' => 'Albumn cant have empty title.',
	'10' => 'Failed to create new album. (hard to say why ... probably an error on the server side).',
	'11' => 'Album not exist.',
	'12' => 'Non existing application.',
	'13' => 'Wrong application key.',
	'14' => 'File is not attached.',
	'15' => 'Already there is a newer version {version}.',
	'16' => 'Error when saving a file.',
	'17' => 'Illegal file extension {extension}.',
	'18' => 'Wrong version number of client.',
	'19' => 'No such object (target).',
	'20' => 'Missing name to protect the album.',
	'21' => 'Missing password to protect the album.',
	'22' => 'Error communication - arrived an empty file.',
	'23' => 'Some blocks of the video are missing.',
	'24' => 'User does not exist.',
	'25' => 'There is the correct userID or albumID.',
	'26' => 'Album not exist or is not belog to this user or isnt public.',
	'27' => 'Invalid clientVideoID.',
	'28' => 'Upload with a given number does not exist.',

	};

	bless($self, $class);
	return $self;
}

lib/WebService/Rajce.pm  view on Meta::CPAN

return $response;
}

sub _open_album {
	my ($self,$album) = @_;

	my $request = {'request'=>{
			'command'=>['openAlbum'],
			'parameters'=>{
				'token'=>[$self->{sessionToken}],
				'albumID'=>[$album->{albumID}],
			},
		}
	};

	my $xml = XMLout($request, KeepRoot => 1, XMLDecl => $self->{XML});
	$self->_debug($xml);
	my $open = $self->{BOT}->post($self->{API},	{'data' => $xml});
	$self->_debug($open->content());

	my $response = XMLin($open->content());

	if($response->{errorCode}){
		confess($self->{ERRORS}{$response->{errorCode}});
	}

return $response;
}

sub _close_album {
	my ($self,$album) = @_;

	my $request = {'request'=>{
			'command'=>['closeAlbum'],
			'parameters'=>{
				'token'=>[$self->{sessionToken}],
				'albumToken'=>[$album->{albumToken}],
			},
		}
	};

	my $xml = XMLout($request, KeepRoot => 1, XMLDecl => $self->{XML});
	$self->_debug($xml);
	my $close = $self->{BOT}->post($self->{API}, {'data' => $xml});
	$self->_debug($close->content());

	my $response = XMLin($close->content());

	if($response->{errorCode}){
		confess($self->{ERRORS}{$response->{errorCode}});
	}

return $response;
}

sub add_photo {
	my ($self,$filename,$album) = @_;

	my $thumbsize = "100x100";

	my $thumb = new Image::Magick;
	$thumb->Read($filename);
	$thumb->AutoOrient();
	$thumb->Resize(geometry=>"$thumbsize^");
	$thumb->Crop(gravity=>"Center",geometry=>"$thumbsize");
	$thumb->Strip();

	my $pic = new Image::Magick;
	$pic->Read($filename);
	$pic->AutoOrient();
	$pic->Resize(geometry=>"$self->{maxWidth}x$self->{maxHeight}>");

	if(!$self->{KEEP_EXIF}){
		$pic->Strip();
	}

	my ($width, $height) = $pic->Get('width','height');
	
	my $request = {'request'=>{
			'command'=>['addPhoto'],
			'parameters'=>{
				'token'=>[$self->{sessionToken}],
				'albumToken'=>[$album->{albumToken}],
				'width'=>[$width],
				'height'=>[$height],
			},
		}
	};

	$self->_open_album($album);

	my $xml = XMLout($request, KeepRoot => 1,	XMLDecl => $self->{XML});
	$self->_debug($xml);

	my $picture = $self->{BOT}->post($self->{API},
		{'data' => $xml,
			'thumb' => [undef,$filename,Content => $thumb->ImageToBlob()],
			'photo' => [undef,$filename,Content => $pic->ImageToBlob()]},
		Content_Type => 'form-data');


	$self->_debug($picture->content());
	my $response = XMLin($picture->content());


	if($response->{errorCode}){
		confess($self->{ERRORS}{$response->{errorCode}});
	}

	$self->_close_album($album);

return $response;
}

sub get_albumurl {
	my ($self,$album) = @_;

	my $url = {'request'=>{
			'command'=>['getAlbumUrl'],
			'parameters'=>{
				'token'=>[$self->{sessionToken}],
				'albumToken'=>[$album->{albumToken}],
			}
		}
	};

	my $xml = XMLout($url, KeepRoot => 1, XMLDecl => $self->{XML});
	$self->_debug($xml);



( run in 0.856 second using v1.01-cache-2.11-cpan-39bf76dae61 )