Created
April 25, 2017 17:28
-
-
Save bobquest33/222043b632fefdd1d55ba0d8d2cf94fe to your computer and use it in GitHub Desktop.
100 Scripts in 30 Days challenge: Script 21 — Reading Twitter Stream using Tweepy
This file contains hidden or 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
| APP_NAME = "" | |
| CONSUMER_KEY = "" | |
| CONSUMER_SECRET = "" | |
| access_token = "" | |
| access_token_secret = "" |
This file contains hidden or 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 | |
| import toml | |
| import glob | |
| import tweepy | |
| import pickle | |
| import traceback | |
| for conffilename in glob.glob('conf\\*.toml'): | |
| with open(conffilename) as conffile: | |
| config = toml.loads(conffile.read()) | |
| APP_NAME = config["APP_NAME"] | |
| consumer_key = config["CONSUMER_KEY"] | |
| consumer_secret = config["CONSUMER_SECRET"] | |
| access_token = config["access_token"] | |
| access_token_secret = config["access_token_secret"] | |
| search_trms = sys.argv[1:] | |
| auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
| auth.set_access_token(access_token, access_token_secret) | |
| api = tweepy.API(auth) | |
| tweets = [] | |
| #override tweepy.StreamListener to add logic to on_status | |
| class MyStreamListener(tweepy.StreamListener): | |
| def on_status(self, status): | |
| tweets.append(status) | |
| try: | |
| myStreamListener = MyStreamListener() | |
| myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener) | |
| myStream.filter(track=search_trms) | |
| except: | |
| traceback.print_exc() | |
| #for tweet in tweets: | |
| # print(tweet) | |
| # print(tweet.text) | |
| # tweets.append(tweet) | |
| with open("twitter_stream.pickle","wb") as wp: | |
| wp.write(pickle.dumps(tweets)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment