2009年8月17日月曜日

[python]Twitter Streaming API

参考:
Twitter Streaming APIをRubyで試してみる - しばそんノート
http://d.hatena.ne.jp/shibason/20090816/1250405491
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urlparse
import urllib2
from pit import Pit
try:
import json
except ImportError:
import simplejson as json
SPRITZER_API_URL = 'http://stream.twitter.com/spritzer.json'
SPRITZER_API_HOST = urlparse.urlparse(SPRITZER_API_URL).hostname
AUTHORIZATION_RELM = 'Firehose'
config = Pit.get('twitter.com',
{'require': {'login': 'login name',
'password': 'login password',}})
USER = config['login']
PASSWORD = config['password']
def _install_opener():
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
AUTHORIZATION_RELM,
SPRITZER_API_HOST,
USER, PASSWORD
)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
def main():
_install_opener()
response = urllib2.urlopen(SPRITZER_API_URL)
connection = True
while (connection):
try:
next = response.next()
status = json.loads(next)
if 'text' in status:
print "%s: %s" % (
status['user']['screen_name'],
status['text'])
except Exception, e:
connection = False
print e
print "...close connection."
if __name__ == "__main__":
main()
view raw twi_stream.py hosted with ❤ by GitHub

もりもり流れてくる。

0 件のコメント:

コメントを投稿