Skip to content

Instantly share code, notes, and snippets.

@Patrick5455
Created October 2, 2020 19:27
Show Gist options
  • Save Patrick5455/a4f6918d689bea688f6dc9eb27d73f87 to your computer and use it in GitHub Desktop.
Save Patrick5455/a4f6918d689bea688f6dc9eb27d73f87 to your computer and use it in GitHub Desktop.
get all tweets between two dates from a specified twitter user
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy
import datetime
import xlsxwriter
import sys
# credentials from https://apps.twitter.com/
consumerKey = "CONSUMER_KEY"
consumerSecret = "CONSUMER_SECRET"
accessToken = "ACCESS_TOKEN"
accessTokenSecret = "ACCESS_TOKEN_SECRET"
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
username = sys.argv[1]
startDate = datetime.datetime(2014, 6, 1, 0, 0, 0)
endDate = datetime.datetime(2015, 1, 1, 0, 0, 0)
tweets = []
tmpTweets = api.user_timeline(username)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
while (tmpTweets[-1].created_at > startDate):
print("Last Tweet @", tmpTweets[-1].created_at, " - fetching some more")
tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
for tweet in tmpTweets:
if tweet.created_at < endDate and tweet.created_at > startDate:
tweets.append(tweet)
workbook = xlsxwriter.Workbook(username + ".xlsx")
worksheet = workbook.add_worksheet()
row = 0
for tweet in tweets:
worksheet.write_string(row, 0, str(tweet.id))
worksheet.write_string(row, 1, str(tweet.created_at))
worksheet.write(row, 2, tweet.text)
worksheet.write_string(row, 3, str(tweet.in_reply_to_status_id))
row += 1
workbook.close()
print("Excel file ready")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment