Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active Jun 25, 2016
Embed
What would you like to do?
【Python】tweepyでTwitter Streaming APIを使う
# codin: utf-8
import tweepy
import time
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.text
def on_error(self, status_code):
if status_code == 420:
print str(status_code)
return False:
def oauth():
consumer_key = "consumer_key"
consumer_secret = "consumer_secret"
access_key = "access_key"
access_secret = "access_secret"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
return auth
if __name__ == "__main__":
api = tweepy.API(oauth())
stream = tweepy.Stream(auth=api.auth, listener=StreamListener())
while True:
try:
stream.filter(track=[u'google'])
except:
# 例外が発生したら1分待って接続する
time.sleep(60)
stream = tweepy.Stream(auth=api.auth, listener=StreamListener())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment