d0iの連絡帳

気がむいたら書く

tw: ものすごくいい加減なTwitterコマンド

ちょっと前から、まわりで Twitter ユーザが増えてきたのですが、どうも既製のクライアントやWeb Interfaceは肌に合わなかったので、軽く作ってみました*1。作りとしてはものすごくいい加減ですが (特に設定ファイル) 一応使えます。


This is ultra-simple twitter command-line client. You can use it over your favorite shell.

Pythonpython-twitter API (Google Code Archive - Long-term storage for Google Code Project Hosting.) が必要です。

It requires python, simplejson, and python-twitter API.

#!/usr/bin/env python

import twitter
import os
import sys

def readConfigFile(filename):
	f = open(os.path.join(os.getenv("HOME"), filename), 'r')
	r = f.readline().strip()
	f.close()
	return r

if __name__ == '__main__':

	USERNAME=readConfigFile(".tw.username")
	PASSWORD=readConfigFile(".tw.password")
	INPUT_ENCODING=readConfigFile(".tw.encoding")

	api = twitter.Api(USERNAME, PASSWORD)

	def print_entries(ft):
		for ft_ent in ft:
			print '[%s, %s]'%(ft_ent.GetUser().GetScreenName(), ft_ent.GetRelativeCreatedAt())
			print "> %s"%(ft_ent.GetText().encode(INPUT_ENCODING, "replace"),)
		return
	def print_friends():
		ft = api.GetFriendsTimeline()
		ft.reverse()
		print_entries(ft)
		return
	def print_replies():
		r = api.GetReplies()
		r.reverse()
		print_entries(r)
		return
	def print_users_timeline(usernames):
		utL=[]
		for username in usernames:
			utL += api.GetUserTimeline(username)
		utL.sort(lambda a, b: cmp(a.GetCreatedAtInSeconds(), b.GetCreatedAtInSeconds()))
		print_entries(utL)
		return

	if len(sys.argv) > 1:
		CMD = sys.argv[1]
		if CMD in ('r', 'reply', 'replies'):
			print_replies()
		elif CMD in ('u', 'update') and len(sys.argv) > 2:
			api.PostUpdate(" ".join(sys.argv[2:]).decode(INPUT_ENCODING))
		elif CMD in ('@', ) and len(sys.argv) > 2:
			print_users_timeline(sys.argv[2:])
		else:
			print "%s {[r]eplies|[u]pdate 'status_string'|@ username(s)}"%(sys.argv[0],)
	else:
		print_friends()
	pass

準備 (Preparation)

% echo "your_username" > ~/.tw.username
% echo "your_password" > ~/.tw.password
% chmod 600 ~/.tw.password
% echo "your_tty_encoding" > ~/.tw.encoding

使い方 (How to Use)

  • Friends Timeline を見る / Check the friends timeline
% tw
  • 特定の人(another_user)のTimeline を見る / Check other user's timeline
% tw @ another_user1 another_user2
  • 自分宛のreplyを見る / Check replies for you
% tw r
  • 自分のstatusをupdateする / Update your status
% tw u 'status update for Twitter'

例 (example)

% tw @ google|tail
[google, about 2 days ago]
> Keeping the Internet from filling up! IPv6 conference today and tomorrow at Google http://bit.ly/D01VV
[google, about 1 days ago]
> Is your team still dancing? Find out with live scores. http://bit.ly/131d6X #ncaa
[google, about 1 days ago]
> There's a very hungry caterpillar on www.google.com
[google, about a day ago]
> Spring is here!  Temperatures are rising and searches for soup are down http://bit.ly/mnyD
[google, about 6 hours ago]
> Tweets all around -- happy 3rd birthday, Twitter! http://bitly.com/8dLD5

*1:期末の現実逃避ってゆーな