2008年3月9日

有备无患:用脚本自动更新Twitter

如果你也是一个Twitter中毒者,那么也会不消停的在网页上在IM上在手机上,或许还会在第三方服务上发送Twitter消息。不过,你有没有试过在terminal上"骚扰"你的朋友们呢?Digital Streets上有一个很骇很饥渴(wyt:英议very hack very geek)的方法,只需输入一行命令:

$ curl -u yourusername:yourpassword -d status="Your Message Here" http://twitter.com/statuses/update.xml

如果你觉得每次输入一长串用户和密码很麻烦,也可以把他们保存起来写个脚本:

#!/bin/bash
#

USERNAME=yourusername
PASSWORD=yourpassword

if [ $# != 1 ]
then
echo "Usage: ${0##*/} your tweet as you would like it to read"
exit 1
fi
tweet=$1
curl -u $USERNAME:$PASSWORD -d status="$1″ http://twitter.com/statuses/update.xml

如果这样还不能让你提起精神,我们还可以把这段脚本修改一下加到cron里,让它每周每天甚至每小时自动发送消息。比方说,你打算在twitter上作一个奥运会倒计时,可以试试下面这个脚本,把它放到/etc/cron.daily目录下。然后,你的Twitter每天就会更新这样一段消息,"距离北京奥运会开幕,还有 DDD 天 HH 小时。":

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 记得替换你的用户名和密码
USERNAME="yourusername"
PASSWORD="yourpassword"
# 奥运会将于2008年8月8日开幕,不过我家黄历说"诸事不宜"
OPENDATE="20080808"

declare -i sec_open=`date --date="$OPENDATE" +%s`
declare -i sec_now=`date +%s`

declare -i sec_total=$(($sec_open-$sec_now))
declare -i date_day=$(($sec_total/60/60/24))

if [ "$sec_total" -lt "0" ]; then
    declare status="丫睡过头了吧,北京奥运会都开幕 $((-1*$date_day)) 天了。。。"
else
    declare -i date_hour=$(($(($sec_total-$date_day*60*60*24))/60/60))
    declare status="距离北京奥运会开幕,还有 $date_day 天 $date_hour 小时。"
fi

curl -u $USERNAME:$PASSWORD -d status="$status" http://twitter.com/statuses/update.xml

0 评论:

发表评论

欢迎留言

订阅我的博客

搜索我的博客

正在加载...

我的豆瓣广播

分享阅读

豆瓣秀

休斯敦火箭

我的文章归档

版权申明