Livedoor picsに写真を一括アップロード

| 0 Comments | 0 TrackBacks | このエントリーをはてなブックマークに追加 このエントリーのはてなブックマーク件数

Flickrをメインに使っているのですが、Flickrだけにアップロードしておくというのも少し不安があるので、他のところにバックアップしておくのがいいかもしれない!と思いました。


そこで、ディレクトリ内の写真を一括Livedoor picsにアップロードするスクリプトを作ってみました。


写真の置いてあるディレクトリをphotosディレクトリに決めうってますが、そこらは適当に改造して使ってください。

#!/usr/bin/env perl
use strict;
use warnings;
use XML::Atom::Client;
use XML::Atom::Entry;
use XML::Atom::Content;
use Path::Class qw(dir);
use Perl6::Say;
use Data::Dumper;

# config
my $livedoor_id = 'Your Livedoor ID';
my $password    = 'Your password';

my $endpoint_url = 'http://ws.pics.livedoor.com/atom/' . $livedoor_id;
my $api          = api_client();

sub main {
    my $dir = dir('photos');
    upload_photos_in_dir($dir);
}

sub upload_photos_in_dir {
    my $dir = shift;
    dir($dir)->recurse(
        callback => sub {
            my $file = shift;
            return unless -f $file;
            upload_file($file);
        }
    );
}

sub upload_file {
    my $file  = shift;
    my $entry = create_entry($file);
    my $edit_uri   = $api->createEntry( $endpoint_url, $entry );
    say 'Uploaded file uri:' . $edit_uri if $edit_uri;
}

sub create_entry {
    my $file = shift;

    my $body    = $file->slurp;
    my $content = XML::Atom::Content->new;
    $content->body($body);
    $content->type('image/jpeg');
    my $entry = XML::Atom::Entry->new;
    $entry->title( $file->basename );
    $entry->content($content);
}

sub api_client {
    my $client = XML::Atom::Client->new;
    $client->username($livedoor_id);
    $client->password($password);
    $client;
}

main();

# picsにアップロードできるサイズを考えると、一括リサイズしてからアップロードできるようにするなんていう処理をいれてもいいかもしれないですね。

No TrackBacks

TrackBack URL: http://perl-mongers.org/MT/mt-tb.cgi/62

Leave a comment

About this Entry

This page contains a single entry by http://coderepos.org/share/wiki/Committers/dann published on June 21, 2008 8:12 PM.

WebService::SimpleでFlickrのfavoritesの写真を一括ダウンロード was the previous entry in this blog.

S3に写真を一括アップロード is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.21-en