Last active
December 27, 2015 18:09
-
-
Save firstspring1845/7367857 to your computer and use it in GitHub Desktop.
rauth使ってUserStream受信してみる
Python2で使う時はl.decode()をlにすればいいはず
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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