#!/usr/bin/perl $|++; my $MYDIR; BEGIN { $MYDIR = $0 =~ m!(.*[/\\])! ? $1 : './'; } use lib "${MYDIR}lib"; use strict; use warnings; use Cache::File; use LWP::UserAgent; use XML::Simple qw( XMLin ); use Date::Parse; use POSIX qw( strftime ); use Encode; use WWW::Mixi; use Net::Twitter; ### Account Setting my $MIXI_USERNAME = ''; my $MIXI_PASSWORD = ''; my $TWITTER_USERNAME = ''; my $consumer_key = ''; my $consumer_secret = ''; my $token = ''; my $token_secret = ''; ### Title of mixi dialy sub getEntryTitle { my $timestr=strftime("%Y/%m/%d", localtime(time)); <{created_at_formatted}$twit->{text_sjis} PERLHEREDOC } ### Format timestamp of a twit sub getFormattedDatetime { strftime '%H:%M', localtime( $_[0] ); } ### Other definition my $NET_TIMEOUT = 30; my $CACHE_FILENAME = "${MYDIR}.cache"; ### Print message sub msg { print STDOUT @_, "\n"; } ### Cache::File my $cache = undef; my $CACHE_KEY_NAME = 'last_update'; sub initCache { $cache = Cache::File->new( cache_root => $CACHE_FILENAME ) or die 'Failed to initialize '; } sub getTwitterOauth { my $statuses; # As of 13-Aug-2010, Twitter requires OAuth for authenticated requests my $nt = Net::Twitter->new( traits => [qw/OAuth API::REST/], consumer_key => $consumer_key, consumer_secret => $consumer_secret, access_token => $token, access_token_secret => $token_secret, ); eval { $statuses = $nt->user_timeline(); }; if ( my $err = $@ ) { die $@ unless blessed $err && $err->isa('Net::Twitter::Error'); warn "HTTP Response Code: ", $err->code, "\n", "HTTP Message......: ", $err->message, "\n", "Twitter error.....: ", $err->error, "\n"; } return $statuses; } ### mixi sub postTwits2mixi { my $twits = shift; msg( "* Post your twits into mixi" ); my $mixi = WWW::Mixi->new( $MIXI_USERNAME, $MIXI_PASSWORD ); $mixi->timeout( $NET_TIMEOUT ); msg( "\tLogin" ); my $res = $mixi->login or die; msg( "\tFirst posting a dialy" ); my @items = $mixi->get_add_diary_preview( diary_title => 'dmy', diary_body => 'dmy', ) or die; msg( "\tConfirm posting the dialy" ); $mixi->get_add_diary_confirm( diary_title => getEntryTitle(), diary_body => getEntryBody( $twits ), post_key => $items[0]->{post_key}, ); msg( "\tdone" ); } ### Main sub main { msg( "* twttmixi - TWiTter To MIXI dialy"); initCache(); my $this_update = 0; my $last_update = $cache->get( $CACHE_KEY_NAME ) || 0; msg( "\tLast proceeded at ". getFormattedDatetime( $last_update )); my $statuses = getTwitterOauth(); my $twits =''; for my $item ( @$statuses ) { my $t = str2time( $item->{created_at} ); $t <= $last_update and next; $this_update <= $t and $this_update = $t; $item->{created_at_formatted} = getFormattedDatetime( $t ); $item->{text_sjis} = encode ('eucjp', $item->{text}); $twits = getFormattedTwit( $item ) . $twits; } unless( $this_update ) { msg( "* Not found new twits since last proceeded" ); return; } postTwits2mixi( $twits ); $cache->set( $CACHE_KEY_NAME, $this_update ); } &main(); __END__ WWW::Mixi URL http://digit.que.ne.jp/work/wiki.cgi?mycmd=search&mymsg=Perl%e3%83%a2%e3%82%b8%e3%83%a5%e3%83%bc%e3%83%ab%2fWWW%3a%3aMixi