Subscribed unsubscribe Subscribe Subscribe

WassrとTwitter両方のヒトコトを取得する

ガーっと書いてみた。

#!/usr/bin/perl
use strict;
use warnings;

use HTTP::Date;
use Encode;
# Net::Wassrを使用する
use Net::Wassr;
# Net::Twitterを使用する
use Net::Twitter;

# ハッシュデータのキー名
my $name = 'name';
my $time = 'time';
my $text = 'text';
my $reply = 'reply';
my $service = 'service';

# ユーザー情報の入力
# 例:
# wassr_username=hogefugapiyo
# wassr_password=****
# twitter_username=hogefugapiyo
# twitter_password=****
my %conf;
for (<>) {
    $conf{$1} = $2 if $_ =~ /^([^=]*)=(.*)/;
}

my @array = ();

# Wassrからデータを取得
my $wassr = Net::Wassr->new(
    user   => $conf{'wassr_username'},
    passwd => $conf{'wassr_password'},
    );
for (@{$wassr->friends_timeline}) {
    my %data = ();
    $data{$service} = 'Wassr';
    $data{$name} = utf8encode($_->{user}->{'screen_name'}) . "($_->{'user_login_id'})";
    $data{$time} = $_->{'epoch'};
    $data{$text} = $_->{'html'};
    if ($_->{'reply_user_login_id'}) {
	my $reply_message;
	if ($_->{'reply_message'}) {
	    $reply_message = utf8encode($_->{'reply_message'});
	} else {
	    $reply_message = "<非公開ヒトコト>";
	}
	$data{$reply} = "> " . $reply_message . " by " . utf8encode($_->{'reply_user_nick'});
    }
    push(@array, \%data);
}

# Twitterからデータを取得
my $twitter = Net::Twitter->new(
    username => $conf{'twitter_username'},
    password => $conf{'twitter_password'},
    );
for (@{$twitter->friends_timeline({ 'id' => $conf{'twitter_username'} })}) {
    my %data = ();
    $data{$service} = 'Twitter';
    $data{$name} = $_->{'user'}->{'screen_name'};
    my $created_at = $_->{'created_at'};
    $created_at =~ s/\+0000/UTC/;
    $data{$time} = HTTP::Date::str2time($created_at);
    $data{$text} = $_->{'text'};
    push(@array, \%data);
}

# 時刻順にソート
@array = sort {
    $a->{$time} <=> $b->{$time}
} @array;

# まとめて出力
for (@array) {
    print "from $_->{$service}\n";
    print "$_->{$reply}\n" if $_->{$reply};
    print utf8encode($_->{$text}), "\n";
    print "by $_->{$name} at ", HTTP::Date::time2iso($_->{$time}), "\n";
    print "\n";
}

# UTF-8エンコーディング
sub utf8encode {
    return Encode::encode('UTF-8', $_[0]);
}