Skip to content

Instantly share code, notes, and snippets.

Last active January 30, 2017 23:41
What would you like to do?
Fetch tweets from Twitter using Python module Tweepy
# Get details about own user
me =
friends = api.friends_ids(
# Initialize data structure
tweets = {}
# Fetch lists recent tweets for each of the user IDs in the list 'friends'
for user in friends:
# Only query Twitter for data not already cached
if db.tweets.find({'user_id': user}).count() == 0:
print('Get recent tweets for user {}...'.format(user))
tweets[user] = []
# Query Twitter API for 2 pages (= 40 tweets)
for page in tweepy.Cursor(api.user_timeline, id=user).pages(2):
print(' Got {} tweets so far...'.format(len(tweets[user])))
# API is rate limited (5 sec sleep = 180 reqs in 15 min)
# Save each tweet to database
for tweet in tweets[user]:
db.tweets.insert_one({'user_id': user, 'tweet': tweet._json})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment