Skip to content

Instantly share code, notes, and snippets.

@thouis
Last active August 15, 2019 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thouis/b6f91507378c01292df774c3c905bbbb to your computer and use it in GitHub Desktop.
Save thouis/b6f91507378c01292df774c3c905bbbb to your computer and use it in GitHub Desktop.
import sys
import twitter
import time
# see https://python-twitter.readthedocs.io/en/latest/getting_started.html
api = twitter.Api(consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN,
access_token_secret=ACCESS_SECRET)
def iterate_over_tweets_reverse(user_id, include_retweets=False):
tweets = api.GetUserTimeline(user_id=user_id, count=100, trim_user=True)
while True:
if not tweets:
return
for t in tweets:
# I assume there's a better way to check this
if include_retweets or (not t.text.startswith("RT @")):
yield t
# get the next batch
max_id = min(t.id for t in tweets)
tweets = api.GetUserTimeline(user_id=user_id, count=100, trim_user=True, max_id=max_id)
if min(t.id for t in tweets) == max_id:
return
if __name__ == '__main__':
assert len(sys.argv) == 3, "Usage: python {} USERNAME DAYS_OLD".format(sys.argv[0])
user_id = api.GetUser(screen_name=sys.argv[1]).id
age_to_delete = int(sys.argv[2])
now = time.time()
for tweet in iterate_over_tweets_reverse(user_id):
age_in_days = int((now - tweet.created_at_in_seconds) / (24 * 60 * 60))
if age_in_days > age_to_delete:
print(age_in_days, tweet.text)
api.DestroyStatus(tweet.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment