Created
March 16, 2015 20:13
-
-
Save mchow01/7d9f439a6eb16dee7fee to your computer and use it in GitHub Desktop.
Store tweets from a Twitter timeline into a Mongo database using Tweepy
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 tweepy | |
import pymongo | |
# Tweepy API doc here: http://pythonhosted.org/tweepy/html/api.html | |
# PyMongo API doc here: https://pypi.python.org/pypi/pymongo/ | |
# Keys | |
consumer_key = '' | |
consumer_secret = '' | |
access_token = '' | |
access_token_secret = '' | |
# Twitter initialization | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
# Mongo initialization | |
client = pymongo.MongoClient("localhost", 27017) | |
db = client.financials | |
try: | |
statuses = api.list_timeline(api.me().screen_name, '<NAME OF TIMELINE?>') | |
for s in statuses: | |
if db.news.find_one({'text':s.text}) == None: # prevent duplicate tweets being stored | |
news = {'text':s.text, 'id':s.id, 'created_at':s.created_at,'screen_name':s.author.screen_name,'author_id':s.author.id} | |
db.news.save(news) | |
except tweepy.error.TweepError: | |
print "Whoops, could not fetch news!" | |
except UnicodeEncodeError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what could be the example of timeline in above code