package Emiri::Net; use strict; use LWP::Simple; use Digest::MD5 qw(md5_hex); use JSON; use MIME::Base64; $emiri7::Net::HOST = 'emiripapas.com'; $emiri7::Net::PATH = '/cgi-bin/emiri'; sub new{ my($pkg) = shift; my(%params) = @_; my $self = {}; my $id = $params{'id'}; my $pkey = $params{'pkey'}; my $akey = $params{'akey'}; my $host = $emiri7::Net::HOST; my $path = $emiri7::Net::PATH; if(exists($params{'host'})){ $host = $params{'host'}; } if(exists($params{'path'})){ $path = $params{'path'}; } if(! $id || (! $pkey || $akey)){ print STDERR "$pkg requires id and pkey or akey.\n"; } $self->{'id'} = $id; $self->{'pkey'} = $pkey; $self->{'akey'} = $akey; $self->{'host'} = $host; $self->{'path'} = $path; bless $self, $pkg; return $self; } sub getImage{ my($self) = shift; my($slide,%params) = @_; my $size = $params{'size'}; my $type = $params{'type'}; my $dl = $params{'download'}; my $base = $slide->{'base'}; my $pict = $slide->{'pict'}; my $host = $self->{'host'}; my $prefix = undef; my $suffix = undef; unless($type){ $type = 'jpeg'; } if($size eq 'org'){ } elsif($size eq 'large'){ $prefix = ''; } elsif($size eq 'midium'){ $prefix = 'T'; } elsif($size eq 'small'){ $prefix = 'ST'; } if($type eq 'gif'){ $suffix .= '.gif'; } elsif($type eq 'png'){ $suffix .= '.png'; } elsif($type eq 'movie'){ if($slide->{'pict_type'} == 1){ $suffix .= '.flv'; } } elsif($type eq 'jpeg'){ if($size eq 'org'){ $suffix.= '_org'; } $suffix .= '.jpg'; } unless(defined($suffix)){ return undef; } my $res = "http://${host}${base}/${prefix}${pict}${suffix}"; if($dl && $slide->{'pict_type'} == 0){ $res = get($res); if($dl eq 'base64'){ $res = encode_base64($res); } } return $res; } sub error{ my($self) = shift; return $self->{'error'}; } sub getAlbum{ my($self) = shift; my(%params) = @_; my $year = $params{'year'}; my $month = $params{'month'}; my $day = $params{'day'}; my $pos = $params{'pos'}; my $res; if($year && $month && $day && ! defined($pos)){ $res = $self->_get('album',@_); } else{ $res = []; $res->[0] = $self->_get('slide',@_); } return $res; } sub getDiary{ my($self) = shift; my(%params) = @_; my $year = $params{'year'}; my $month = $params{'month'}; my $day = $params{'day'}; my $res = $self->_get('diary',@_); return $res; } sub _get{ my($self) = shift; my($cmd,%params) = @_; my $id = $self->{'id'}; my $pkey = $self->{'pkey'}; my $akey = $self->{'akey'}; my $host = $self->{'host'}; my $path = $self->{'path'}; my $year = $params{'year'}; my $month = $params{'month'}; my $day = $params{'day'}; my $pos = $params{'pos'}; my $type = $params{'type'}; unless($type){ $type = 'json'; } my $url = "http://${host}${path}"; if($cmd eq 'slide'){ $url .= '/Slide.cgi?'; } elsif($cmd eq 'album'){ $url .= '/Album.cgi?'; } elsif($cmd eq 'diary'){ $url .= '/Diary.cgi?'; } $url .= "base=${id}&"; if($pkey){ $pkey = md5_hex($id,$pkey); $url .= "pkey=${pkey}&"; } elsif($akey){ $akey = md5_hex($id,$akey); $url .= "akey=${akey}&"; } if($cmd eq 'diary'){ my $date; my $rand; if($year){ $url .= sprintf("year=%04d&",$year); if($month){ $url .= sprintf("month=%02d&",$month); if($day){ $url .= sprintf("day=%02d&",$day); } else{ $url .= 'rand=pict&'; } } else{ $url .= 'rand=pict&'; } } else{ $url .= 'rand=pict&'; } } else{ if($year && $month && $day){ my $date = sprintf("year=%04d&month=%02d&day=%02d&",$year,$month,$day); $url .= $date; if($cmd eq 'slide' && defined($pos)){ $url .= "pos=${pos}&"; } } else{ $url .= 'rand&'; } } $url .= "type=${type}"; $self->{'req'} = $url; my $content = get($url); if($content){ return from_json($content); } return undef; } 1;