at_yasu's blog

ロード的なことを

[python][twitter] Twitterとおしゃべり

Twitterとおしゃべりするクラスを作ったので。

使用例

継承して機能を追加させます。例:

class userTimelineTwitter (twitter):
	""" タイムラインを取得する """
	def user_timeline(self):
		url = 'http://twitter.com/statuses/user_timeline.json'
		code,msg = self.sendTwitter(url, {})
		return json.read(msg)

tw = userTimelineTwitter()
msg = tw.user_timeline()
for lists in msg:
	for info in lists:
		print "%s:%s" % (info,lists[info])


パスワードなどが設定ファイルで保存されず、直接プログラムから指定する場合。

tw = userTimelineTwitter(email='...', pass='...')
msg = tw.user_timeline()
for lists in msg:
	for info in lists:
		print "%s:%s" % (info,lists[info])


あんまりテストしていないから、挙動はあやしいかも。ごめんね。

クラスのソース

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
import sys
import urllib2, urllib

try:
	import json
except:
	print "This system is not support json. Please must be install Python-Json "\
		  "<http://pypi.python.org/pypi/python-json/>"
	sys.exit(1)

try:
	import yaml
except:
	print "This system is not support yaml. You can install to PyYAML if you wanna"\
		  "<http://pyyaml.org/>"
	print "The PyYaml are using configure file."

class twitter:
	""" Talk to Twitter <http://twitter.com/> class.
	
I read to <http://watcher.moe-nifty.com/memo/docs/twitterAPI13.txt>

(c) a.yasui <a.yasui@gmail.com>
	"""
	
	def __init__ (self, conf='.twitter.yaml', email='', passwd=''):
		self.__home__ = os.path.abspath(os.getenv('HOME'))
		self.__conf__ = os.path.join(self.__home__, conf)
		self.__user__ = email
		self.__pass__ = passwd
		self.loadConf()

	def loadConf (self, conf=''):
		self.__conf__ = conf and os.path.join(self.__home__, conf)\
							 or self.__conf__
		
		if not os.path.exists(self.__conf__) :
			if not self.__user__ and not self.__pass__:
				print "Error: configure file is not found: %s" % (self.__conf__)
				sys.exit(1)
			else:
				return
		
		fname, type = (".".join(self.__conf__.split(".")[0:-1]),
						self.__conf__.split(".")[-1])
		flagment = dict()
		
		if type == 'yaml':
			flagment = yaml.load(open(self.__conf__).read())
		elif type == 'json':
			flagment = json.read(open(self.__conf__).read())
		else:
			print "Error: configure file type '%s' are not support" % (type)
			sys.exit(1)
		
		if 'email' in flagment and 'password' in flagment:
			self.setUserAndPassword(**flagment)
		else:
			print "Error: not found to email and password in configure file."
			sys.exit(1)

	def setUserAndPassword (self, email, password):
		self.__user__ = email
		self.__password__ = password
	
	def sendTwitter (self, url, param, type="GET"):
		auth = self.auther()
		u = None
		if not type in ['GET','POST']:
			print "Error: unknown HTTP method %s" % (type)
			sys.exit(1)
		
		p = urllib.urlencode(param)
		try:
			op = urllib2.build_opener(auth)
			urllib2.install_opener(op)
			if type == 'GET':
				u = urllib2.urlopen("%s?%s" % (url, p))
			else:
				u = urllib2.urlopen(url, p)
		except urllib2.HTTPError, e:
			return (e.errno, e.message)
		return (200, u.read())

	def auther (self):
		auth_handler = urllib2.HTTPBasicAuthHandler()
		if not self.__user__ or not self.__password__:
			if self.__mustlogin__:
				print "Error: The user or password is not set yet."
				sys.exit(1)
			else:
				return None
		auth_handler.add_password('Twitter API', 'https://twitter.com/',
								self.__user__, self.__password__)
		return auth_handler

class updateTwitter (twitter):
	def setStatus (self, status):
		if len(status):
			self.__status__ = status

	def update (self, status=''):
		url = 'https://twitter.com/statuses/update.json'
		if not status:
			self.setStatus(status)
		code, msg = self.sendTwitter(url, {'status':self.__status__}, 'POST')
		if code == 200:
			print "update ok"
		else:
			print msg

class userTimelineTwitter (twitter):
	""" タイムラインを取得する """
	def user_timeline(self):
		url = 'http://twitter.com/statuses/user_timeline.json'
		code,msg = self.sendTwitter(url, {})
		return json.read(msg)

def main():
	tw = userTimelineTwitter()
	msg = tw.user_timeline()
	for lists in msg:
		for info in lists:
			print "%s:%s" % (info,lists[info])

if __name__ == '__main__':
	main()