Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use strict;
use warnings;
my $API_USERNAME = 'ficia';
my $API_PASSWORD = $ENV{FICIA_EYEFI_PASSWORD};
my $API_ENDPOINT = 'http://ficia.com/api/gr2/eyefi/main.php';
my $filepath = $ARGV[0];
unless ($API_PASSWORD && $filepath) {
print "usage: FICIA_EYEFI_PASSWORD=your_ficia_eyefi_password perl $0 your_upload.jpg\n";
exit -1;
}
my $ficia = OreOre::FiciaUploadByGR2->new(
endpoint => $API_ENDPOINT,
name => $API_USERNAME,
password => $API_PASSWORD,
);
my $result;
$result = $ficia->login;
if ($result->{status}) {
die $result->{status_text};
}
$result = $ficia->upload($filepath);
if ($result->{status}) {
die $result->{status_text} . " (duplicate file?)";
}
print "$filepath upload success!\n";
exit 0;
package OreOre::FiciaUploadByGR2;
use File::Spec;
use HTTP::Request::Common 'POST';
use LWP::UserAgent;
sub new {
my($class, %args) = @_;
$args{ua} ||= LWP::UserAgent->new( cookie_jar => +{} );
bless { %args }, $class;
}
sub ua { $_[0]->{ua} }
sub endpoint { $_[0]->{endpoint} }
sub name { $_[0]->{name} }
sub password { $_[0]->{password} }
sub login {
my $self = shift;
my($result) = $self->request(
login => {
uname => $self->name,
password => $self->password,
}
);
$result;
}
sub upload {
my($self, $path) = @_;
my $filename = (File::Spec->splitpath($path))[-1];
my($result) = $self->request(
'add-item' => {}, (
Content_Type => 'multipart/form-data;',
Content => [
g2_controller => 'remote:GalleryRemote',
g2_userfile_name => $filename,
g2_userfile => [ $path ],
'g2_form[cmd]' => 'add-item',
],
)
);
$result;
}
sub request {
my($self, $cmd, $args, @opts) = @_;
my @param = qw( g2_controller remote:GalleryRemote );
push @param, 'g2_form[cmd]', $cmd;
while (my($k, $v) = each %{ $args }) {
push @param, "g2_form[$k]", $v;
}
my $res = $self->ua->request( POST $self->endpoint, \@param, @opts );
return { status => -1 }, $res unless $res->code == 200;
my $content = $res->content;
my $result = +{};
for my $line (split /\n/, $content) {
chomp $line;
next if $line =~ /^#/;
my($k, $v) = split /=/, $line;
$result->{$k} = $v;
}
return $result, $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment