-
-
Save kyanny/351566 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; | |
my $API_USERNAME = $ENV{DAYS_USERNAME}; | |
my $API_PASSWORD = $ENV{DAYS_EYEFI_PASSWORD}; | |
my $API_ENDPOINT = 'http://30d.jp/manage/library/main.php'; | |
my $filepath = $ARGV[0]; | |
unless ($API_PASSWORD && $filepath) { | |
print "usage: DAYS_USERNAME=your_days_id DAYS_EYEFI_PASSWORD=your_days_eyefi_password perl $0 your_upload.jpg\n"; | |
exit -1; | |
} | |
my $days = OreOre::DaysUploadByGR2->new( | |
endpoint => $API_ENDPOINT, | |
name => $API_USERNAME, | |
password => $API_PASSWORD, | |
); | |
my $result; | |
$result = $days->login; | |
if ($result->{status}) { | |
die $result->{status_text}; | |
} | |
$result = $days->upload($filepath); | |
if ($result->{status}) { | |
die $result->{status_text} . " (duplicate file?)"; | |
} | |
print "$filepath upload success!\n"; | |
exit 0; | |
package OreOre::DaysUploadByGR2; | |
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', | |
'g2_form[uname]' => $self->name, | |
'g2_form[caption]' => $filename, | |
], | |
) | |
); | |
$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