root/twttmixi/trunk/twttmixi.pl

Revision 4, 3.7 kB (checked in by piroli, 1 年 ago)

スクリプトの実行パスを取得するよう修正

Line 
1 #!/usr/bin/perl
2 $|++;
3
4 my $MYDIR;
5 BEGIN {
6     $MYDIR = $0 =~ m!(.*[/\\])! ? $1 : './';
7 }
8
9 use lib "${MYDIR}lib";
10 use strict;
11 use warnings;
12 use Cache::File;
13 use LWP::UserAgent;
14 use XML::Simple qw( XMLin );
15 use Date::Parse;
16 use POSIX qw( strftime );
17 use Encode;
18 use WWW::Mixi;
19
20
21
22 ### Account Setting
23 my $TWITTER_USERNAME    = 'pirolix';
24 my $MIXI_USERNAME       = 'your@mail.example.com';
25 my $MIXI_PASSWORD       = 'password';
26
27 ### Title of mixi dialy
28 sub getEntryTitle {
29     <<PERLHEREDOC;
30 [twitter] º£ÆEÎҁE­
31 PERLHEREDOC
32 }
33
34 ### Header part of mixi diary body
35 sub getEntryBody {
36     my $twits = shift;
37     <<PERLHEREDOC;
38 $twits
39 ---
40 Powered by twitter (http://twitter.com/$TWITTER_USERNAME) + twttmixi
41 PERLHEREDOC
42 }
43
44 ### Format a twit
45 sub getFormattedTwit {
46     my $twit = shift;
47     <<PERLHEREDOC;
48 $twit->{created_at_formatted}¢§$twit->{text_sjis}
49 PERLHEREDOC
50 }
51
52 ### Format timestamp of a twit
53 sub getFormattedDatetime {
54     strftime '%H:%M', localtime( $_[0] );
55 }
56
57
58
59 ### Other definition
60 my $NET_TIMEOUT         = 30;
61 my $TWITTER_API         = "http://twitter.com/statuses/user_timeline/$TWITTER_USERNAME.xml";
62 my $CACHE_FILENAME      = "${MYDIR}.cache";
63
64 ### Print message
65 sub msg {
66     print STDOUT @_, "\n";
67 }
68
69 ### Cache::File
70 my $cache = undef;
71 my $CACHE_KEY_NAME = 'last_update';
72 sub initCache {
73     $cache = Cache::File->new( cache_root => $CACHE_FILENAME )
74         or die 'Failed to initialize <Cache::File>';
75 }
76
77 ### Twitter
78 sub getTwitter {
79     msg( "* Retrieving your twits from twitter.com" );
80     my $twitter = LWP::UserAgent->new
81         or die 'Failed to initialize <LWP::UserAgent>';
82     $twitter->timeout( $NET_TIMEOUT );
83     msg( "\tConnecting twitter.com" );
84     my $res = $twitter->get( $TWITTER_API )
85         or die 'Failed to retrieve - '. $TWITTER_API;
86     $res->is_success
87         or die $res->status_line;
88     my $content = $res->content
89         or die $res->status_line;
90     my $ref = XMLin( $content )
91         or die 'Failed to handle XML response';
92     msg( "\tdone" );
93     $ref;
94 }
95
96 ### mixi
97 sub postTwits2mixi {
98     my $twits = shift;
99     msg( "* Post your twits into mixi" );
100     my $mixi = WWW::Mixi->new( $MIXI_USERNAME, $MIXI_PASSWORD );
101     $mixi->timeout( $NET_TIMEOUT );
102     msg( "\tLogin" );
103     my $res = $mixi->login
104         or die;
105     msg( "\tFirst posting a dialy" );
106     my @items = $mixi->get_add_diary_preview(
107             diary_title => 'dmy',
108             diary_body => 'dmy',
109     ) or die;
110     msg( "\tConfirm posting the dialy" );
111     $mixi->get_add_diary_confirm(
112             diary_title => getEntryTitle(),
113             diary_body => getEntryBody( $twits ),
114             post_key => $items[0]->{post_key},
115     );
116     msg( "\tdone" );
117 }
118
119 ### Main
120 sub main {
121     msg( "* twttmixi - TWiTter To MIXI dialy");
122
123     initCache();
124
125     my $this_update = 0;
126     my $last_update = $cache->get( $CACHE_KEY_NAME ) || 0;
127     msg( "\tLast proceeded at ". getFormattedDatetime( $last_update ));
128
129     my $ref = getTwitter();
130
131     my $statuses = $ref->{status};
132     my $twits = '';
133     foreach (sort keys %$statuses) {
134         my $item = $statuses->{$_}
135             or next;
136         my $t = str2time( $item->{created_at} );
137         $t <= $last_update
138             and next;
139         $this_update <= $t
140             and $this_update = $t;
141         $item->{created_at_formatted} = getFormattedDatetime( $t );
142         $item->{text_sjis} = encode ('eucjp', $item->{text});
143         $twits .= getFormattedTwit( $item );
144     }
145
146     unless( $this_update ) {
147         msg( "* Not found new twits since last proceeded" );
148         return;
149     }
150
151     postTwits2mixi( $twits );
152
153     $cache->set( $CACHE_KEY_NAME, $this_update );
154 }
155 &main();
156
157 __END__
158 WWW::Mixi €ÎºÇ¿·ÈǀϰʲŒ€Î URL €ÇÆþŒê€·€Æ€¯€À€µ€€
159 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
160
Note: See TracBrowser for help on using the browser.