比这篇新的文章:
FolderDialog
比这篇旧的文章: map_reduce
作者: liufeng, 点击1101次, 评论(2), 收藏者(0), , 打分:
所有评论,共2条:( 我也来说两句)
比这篇旧的文章: map_reduce
通过Twitter来更新校内状态
语言: Perl, 标签: xiaonei twitter Perl 2009/04/21发布 1年前更新作者: liufeng, 点击1101次, 评论(2), 收藏者(0), , 打分:
Perl语言: 通过Twitter来更新校内状态
01 #!/usr/bin/perl -w
02
03 # Twitter -> Xiaonei status sync tool.
04 # Author: Feng Liu <liufeng@cnliufeng.com>
05 # Version: 1.0
06 # Date: Tue Apr 21 23:06:40 +0000 2009
07
08 use utf8;
09 use Encode;
10 use LWP::Simple;
11 use HTML::Entities;
12
13 ####################### Settings starts here #######################
14 # Set your base account information here. Don't show this to others!
15 my $twitter_account = '';
16 my $xiaonei_email = '';
17 my $xiaonei_passwd = '';
18
19 # some machine's wget is too old, so you may need to rebuild a newer
20 # version and indicate the path of your own wget here.
21 my $wget_cmd = 'wget';
22
23 # The program needs a log file for keeping the time of your last
24 # tweet. Otherwise you may get your xiaonei status updated to a same
25 # tweet. So please keep this file!
26 my $logfile = '/tmp/twxn.log';
27 ######################## Settings ends here ########################
28
29 my $twitter_url = 'http://twitter.com/statuses/user_timeline/' . $twitter_account . '.xml';
30 my $statuses = get($twitter_url);
31 my @lines = split /\n/, $statuses;
32 my $latest_text = $lines[5];
33 my $latest_time = $lines[3];
34
35 if ($latest_text =~ /<text>(.*)<\/text>/) {
36 $status = $1;
37 };
38 $text = decode_entities($status);
39
40 # If the log file doesn't exist, create a new one.
41 if (!(-e $logfile)) {
42 open LOG,"> /tmp/twxn.log" or die "ERROR: Cannot create log file.";
43 close LOG;
44 print "Created a new log file: $logfile\n";
45 }
46
47 open LOG, "< $logfile" || die "ERROR: Cannot open log file!";
48 $last_time = <LOG>;
49 close LOG;
50
51 if ($last_time ne $latest_time) {
52
53 my $login_cmd = $wget_cmd . ' --no-proxy -O xiaoneilogin.log --post-data="email=' . $xiaonei_email . '&password=' . $xiaonei_passwd . '&autoLogin=true" --keep-session-cookies --save-cookies=xiaoneicookie http://login.xiaonei.com/Login.do';
54
55 my $post_cmd = $wget_cmd . ' --no-proxy -O xiaoneipost.log --post-data="c=' . $text . '" --keep-session-cookies --load-cookies=xiaoneicookie http://status.xiaonei.com/doing/update.do --referer=http://status.xiaonei.com/getdoing.do';
56
57 system($login_cmd);
58 system($post_cmd);
59
60 open LOG, "> $logfile" || die "ERROR: Cannot open log file!";
61 print LOG $latest_time;
62 close LOG;
63 }
02
03 # Twitter -> Xiaonei status sync tool.
04 # Author: Feng Liu <liufeng@cnliufeng.com>
05 # Version: 1.0
06 # Date: Tue Apr 21 23:06:40 +0000 2009
07
08 use utf8;
09 use Encode;
10 use LWP::Simple;
11 use HTML::Entities;
12
13 ####################### Settings starts here #######################
14 # Set your base account information here. Don't show this to others!
15 my $twitter_account = '';
16 my $xiaonei_email = '';
17 my $xiaonei_passwd = '';
18
19 # some machine's wget is too old, so you may need to rebuild a newer
20 # version and indicate the path of your own wget here.
21 my $wget_cmd = 'wget';
22
23 # The program needs a log file for keeping the time of your last
24 # tweet. Otherwise you may get your xiaonei status updated to a same
25 # tweet. So please keep this file!
26 my $logfile = '/tmp/twxn.log';
27 ######################## Settings ends here ########################
28
29 my $twitter_url = 'http://twitter.com/statuses/user_timeline/' . $twitter_account . '.xml';
30 my $statuses = get($twitter_url);
31 my @lines = split /\n/, $statuses;
32 my $latest_text = $lines[5];
33 my $latest_time = $lines[3];
34
35 if ($latest_text =~ /<text>(.*)<\/text>/) {
36 $status = $1;
37 };
38 $text = decode_entities($status);
39
40 # If the log file doesn't exist, create a new one.
41 if (!(-e $logfile)) {
42 open LOG,"> /tmp/twxn.log" or die "ERROR: Cannot create log file.";
43 close LOG;
44 print "Created a new log file: $logfile\n";
45 }
46
47 open LOG, "< $logfile" || die "ERROR: Cannot open log file!";
48 $last_time = <LOG>;
49 close LOG;
50
51 if ($last_time ne $latest_time) {
52
53 my $login_cmd = $wget_cmd . ' --no-proxy -O xiaoneilogin.log --post-data="email=' . $xiaonei_email . '&password=' . $xiaonei_passwd . '&autoLogin=true" --keep-session-cookies --save-cookies=xiaoneicookie http://login.xiaonei.com/Login.do';
54
55 my $post_cmd = $wget_cmd . ' --no-proxy -O xiaoneipost.log --post-data="c=' . $text . '" --keep-session-cookies --load-cookies=xiaoneicookie http://status.xiaonei.com/doing/update.do --referer=http://status.xiaonei.com/getdoing.do';
56
57 system($login_cmd);
58 system($post_cmd);
59
60 open LOG, "> $logfile" || die "ERROR: Cannot open log file!";
61 print LOG $latest_time;
62 close LOG;
63 }
所有评论,共2条:( 我也来说两句)
| 1 |
admin
1年前
回复
改用perl了?
|
| 2 |
@admin: 嗯。yegle提供的那些wget语句可以运行(http://yegle.net/2009/04/21/use-bash-to-update-xiaonei-status-and-blog/),之前写过处理dict.cn的,对XML处理还比较熟悉。关键是 1)我写的Python版本需要一些包依赖,不方便在虚拟主机上运行;2)Python的奇怪字符串问题一直弄的我很困惑。我只有等放假后再贯彻您给我会的邮件的精神了
|
代码

过程详见:http://blog.cnliufeng.com/2009/04/got-crazy-by-python-string.html 以及之后的两篇文章。