Skip to content

Instantly share code, notes, and snippets.

@firstspring1845
Last active December 27, 2015 18:09
Show Gist options
  • Save firstspring1845/7367857 to your computer and use it in GitHub Desktop.
Save firstspring1845/7367857 to your computer and use it in GitHub Desktop.
rauth使ってUserStream受信してみる Python2で使う時はl.decode()をlにすればいいはず
import sys,json
from rauth.service import OAuth1Service
auth = OAuth1Service(
name='twitter',
consumer_key='key',
consumer_secret='secret',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authorize')
token = ('accesstoken','accesstokensecret')
session = auth.get_session(token=token)
r = session.post('https://userstream.twitter.com/2/user.json',data={},stream=True)
for l in r.iter_lines(1):
try:
j = json.loads(l.decode())
print('@'+j['user']['screen_name']+' '+j['text'])
except KeyboardInterrupt as e:
sys.exit()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment