Last active
June 25, 2016 02:24
-
-
Save kurozumi/d1228fc0761eb3dc128ad1b2abd8f818 to your computer and use it in GitHub Desktop.
【Python】tweepyでTwitter Streaming APIを使う
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
# 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