mixi に写真をアップロードする

June 29th, 2007

mixiの新着情報をタスクトレイに通知、ミクシィが専用アプリを強化:ニュース - CNET Japan

ミクシィは6月28日、同社が運営するSNS「mixi」の専用アプリケーション「mixi station」に、フォトアップローダー機能と新着お知らせ機能を追加した。

写真のほうをちょっと見てみたら Atom みたいです。こんなスクリプトで最初のアルバムに写真を追加できます。

use strict;
use warnings;

use XML::Atom::Client;
use XML::Atom::Service;
use File::Slurp;

sub get_first_album {
  my ($client) = @_;    
  my $service = $client->getService('http://photo.mixi.jp/atom/r=3');

  for my $c ($service->workspace->collections) {
    if ($c->accept && $c->accept eq 'image/jpeg') {
      return $c;
    }
  }
  return undef;
}

my $client = XML::Atom::Client->new;
$client->username('YOU@EXAMPLE.COM');
$client->password('PASSWORD');

my $collection = get_first_album($client);

my $image = read_file(shift, binmode => ':raw');

my $req = HTTP::Request->new(POST => $collection->href);
$req->content_type('image/jpeg');
$req->content_length(length $image);
$req->content($image);

my $resp = $client->make_request($req);
if (! $resp->is_success) {
  print STDERR $resp->status_line, "\n";
  exit(1);
}

使い方

% perl a.pl ~/a.jpg

XML::Atom::Client で XML にくるまないで画像を送る方法がわからなかったので、HTTP::Request を直に使っています。というか Perl も Atom もよくわからんよ。