-
-
Save tokuhirom/390913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use 5.12.0; | |
use Pod::Usage; | |
use Config::Pit; | |
use Path::Class; | |
use Data::Dumper; | |
use GDBM_File; | |
use autodie; | |
my $API_USERNAME = 'ficia'; | |
my $API_ENDPOINT = 'http://ficia.com/api/gr2/eyefi/main.php'; | |
my $config = pit_get( | |
"ficia.com", | |
require => { | |
"password" => "your gr2 password on ficia" | |
} | |
); | |
my $API_PASSWORD = $config->{password}; | |
pod2usage() unless $API_PASSWORD; | |
pod2usage() unless @ARGV; | |
&main;exit; | |
sub main { | |
my $ficia = OreOre::FiciaUploadByGR2->new( | |
endpoint => $API_ENDPOINT, | |
name => $API_USERNAME, | |
password => $API_PASSWORD, | |
); | |
# login | |
my $result = $ficia->login; | |
if ($result->{status}) { | |
die $result->{status_text}; | |
} | |
# upload files | |
tie my %db, 'GDBM_File', "$ENV{HOME}/.ficia-eyefi-uploader.db", &GDBM_WRCREAT, 0640; | |
for my $dirpath (@ARGV) { | |
dir($dirpath)->recurse( | |
callback => sub { | |
my $filepath = shift; | |
return if $filepath =~ m{/\.\w+/}i; | |
return unless $filepath =~ /\.jpg$/i; | |
return if $db{"$filepath"}; | |
do_upload($ficia, $filepath); | |
$db{"$filepath"} = 1; | |
} | |
); | |
} | |
} | |
sub do_upload { | |
my ($ficia, $filepath)= @_; | |
say "uploading $filepath"; | |
my ($result, $res) = $ficia->upload("$filepath"); | |
if ($result->{status}) { | |
# warn Dumper($result, $res->headers, $res->content); | |
warn $result->{status} . " " . $result->{status_text} . " (duplicate file?)\n"; | |
} | |
} | |
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, $res) = $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', | |
], | |
) | |
); | |
return ($result, $res); | |
} | |
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); | |
} | |
__END__ | |
=head1 SYNOPSIS | |
perl ficia-eyefi-uploader.pl your_upload.jpg | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment