080703

|



ドアストッパーを取りつけました。もうちょっとよく考えてから設置すればよ
かった。



よくブログやWeb日記にあるアマゾンへのリンクを導入してみました。ISBN(本)、 EAN(CD,DVD)を読みとって、AWS(Amazon Web Service)に照会してアマゾン独自 のASINを得て、そのASINからAWSから情報を得ればいいようだ。携帯でバーコー ドを読みとって送るとブログエントリになるようにしてみました。
アマゾンとのやりとりはこんな感じで。
package uch_aws;

use strict;
use Jcode;
use WebService::Simple;

%uch_aws::media_type = (
    book	=> { id => 'ISBN',	index => 'Books' },
    cd		=> { id => 'EAN',	index => 'Music' },
    dvd	=>	=> { id => 'EAN',	index => 'DVD' },
);

sub new
{
    my $class = shift;

    my $self = {
	aws_access_key_id => shift,  # AWS Access Key ID
	aws => '',
    };

    $self->{aws} = WebService::Simple->new(
	base_url => "http://webservices.amazon.co.jp/onca/xml",
	param    => {
	    SubscriptionId => $self->{aws_access_key_id},
	    Service        => 'AWSECommerceService',
	    locale         => 'jp',
	    ContentType    => 'text/xml',
	    Version        => '2008-07-03',
	}
	);

    return bless $self, $class;
}

sub item_info
{
    my ($self, $code, $title, $content) = @_;
    my $asin;

    foreach my $encode (keys %uch_aws::media_type) {
	$asin = $self->code_to_asin ($code, $encode);
	if (defined ($asin)) {
	    last;
	}
    }

    my $item_query = $self->{aws}->get(
	{
	    Operation		=> 'ItemLookup',
	    ItemId		=> $asin,
	    ResponseGroup	=> 'Small,Images',
	}
	);
#print Dumper $item_query->parse_response;
    my $item = $item_query->parse_response->{Items}->{Item};
    if (!defined ($item)) {
	print STDERR "ASIN exists, but no information\n";
	return 0;
    }

    $$title .= sprintf ("%s ", $item->{ItemAttributes}->{Title});
    my $author = $item->{ItemAttributes}->{Author};
    if (ref ($author) eq 'ARRAY')
    {
	for (my $i = 0; $author->[$i]; $i++)
	{
	    $$title .= sprintf ("%s ", $author->[$i]);
	}
    }
    else
    {
	$$title .= sprintf ("%s ", $author);
    }

    my $creator = $item->{ItemAttributes}->{Creator};
    if (defined ($creator))
    {
	if (ref ($creator) eq 'ARRAY')
	{
	    for (my $i = 0; (my $c = $creator->[$i]->{content}); $i++)
	    {
		$$title .= sprintf ("%s:%s ", $c->{Role}, $c->{content});
	    }
	}
	else
	{
	    $$title .= sprintf ("%s:%s ",
			       $creator->{Role},
			       $creator->{content});
	}
    }

    my $image;
    if (defined (($image = $item->{SmallImage}->{URL})))
    {
	$$content .= sprintf ("<a href=\"%s\"><img src=\"%s\"></a>\n",
			      $item->{DetailPageURL},
			      $image);
    }
    else
    {
	$$content .= sprintf ("<a href=\"%s\">%s</a>\n",
			      $item->{DetailPageURL},
			      $$title);
    }

    return 1;
}

sub code_to_asin
{
    my ($self, $code, $encode) = @_;

    my $id = $uch_aws::media_type{$encode}{id};
    my $index =$uch_aws::media_type{$encode}{index};
    my $asin_query = $self->{aws}->get(
	{
	    Operation		=> 'ItemLookup',
	    ItemId		=> $code,
	    IdType		=> $id,
	    SearchIndex		=> $index,
	    ResponseGroup	=> 'Small',
	}
	);

    return $asin_query->parse_response->{Items}->{Item}->{ASIN};
}
1;
MovableTypeとのやりとりは相手はMovableTypeに固定してこんな感じで。

package uch_mtrpc;

use strict;
use XMLRPC::Lite;
use Jcode;

sub new
{
    my $class = shift;

    my $self = {
	url => shift,
        user => shift,
	password => shift,
        blog_id => shift,
	category_id => shift,
	xmlrpc => '',
    };
    $self->{xmlrpc} = XMLRPC::Lite->new;
    $self->{xmlrpc}->proxy ($self->{url});

    return bless $self, $class;
}

sub post
{
    my ($self, $title, $contents, $publish) = @_;
    my $post_id = '0';

    my $ret = $self->{xmlrpc}->call
	('metaWeblog.newPost',
	 $self->{blog_id},
	 $self->{user},
	 $self->{password},
	 {
	     'title' => $title,
	     'description' => $contents,
	     'mt_text_more' => ''
	 },
	 0); # not published yet.

    if (defined ($ret)) {
	$post_id = $ret->result ();
    }

    if ($self->{category_id} != 0) {
	$self->set_category ($post_id);
    }

    if ($publish) {
	$self->publish ($post_id);
    }

    return $post_id;
}

sub publish
{
    my ($self, $post_id) = @_;

    return $self->{xmlrpc}->call
	('mt.publishPost',
	 $post_id,
	 $self->{user},
	 $self->{password});
}

sub edit
{
    my ($self, $post_id, $title, $contents, $publish) = @_;
    my $post_id = 0;

    my $result = $self->{xmlrpc}->call
	('metaWeblog.newPost',
	 $post_id,
	 $self->{user},
	 $self->{password},
	 {
	     'title' => $title,
	     'description' => $contents,
	     'mt_text_more' => ''
	 },
	 $publish)->result;

    return defined ($result);
}

sub set_category
{
    my ($self, $post_id) = @_;

    my $result = $self->{xmlrpc}->call
	('mt.setPostCategories',
	 $post_id,
	 $self->{user},
	 $self->{password},
	 [{
	     'categoryId' => $self->{category_id},
	     'isPrimary' => 1,
	  }])->result;

    return defined ($result);
}
1;
これを組みあわせてコマンドにして
#!/usr/pkg/bin/perl

BEGIN
{
    my $MT_DIR = '/home/uch/public_html/sn/';
    my $AMAZON_DIR = '/home/uch/amazon/';
    push @INC, $MT_DIR . 'extlib';
    push @INC, $MT_DIR . 'lib';
    push @INC, $AMAZON_DIR . 'lib';
    push @INC, $AMAZON_DIR;
}

use strict;
use Jcode;
use uch_mtrpc;
use uch_aws;

$uch_mtrpc::mt = new uch_mtrpc 'http://crescentmoon.s.vnop.net/~uch/sn/mt-xmlrpc.cgi ', 'uch', '8ea8j7gl', 1, 7;
$uch_aws::aws = new uch_aws '01XB2AJX6DH0D8W91S02';

my $barcode;
my $title;
my $content;

while (<STDIN>) {
    $barcode = $barcode.$_;
}

$uch_aws::aws->item_info ($barcode, \$title, \$content);
print $title;
print $content;

my $post_id = $uch_mtrpc::mt->post ($title, $content, 1);
print $post_id;

exit;
これ用のアカウントを作ってprocmailで流しこみました。
:0
* ^TO_.xxx@vnop\.net
{
	:0 fbw
	| aws.pl | nkf -j

	:0
	*
	xxx/.
}
10年振りにperlを使いましたよ。コードはちょっとおかしいかも。やっぱりこ ういうのはperlに分があるわね。とはいえ芋づる式にモジュールをCPANから持っ てこないといけないので、ちょい面倒なのはある。