Skip to content

Instantly share code, notes, and snippets.

@nabla-c0d3
Created January 19, 2019 23:42
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 nabla-c0d3/85fb624e4509c15627af0f8c6814ce13 to your computer and use it in GitHub Desktop.
Save nabla-c0d3/85fb624e4509c15627af0f8c6814ce13 to your computer and use it in GitHub Desktop.
Delete old tweets
import tweepy
from datetime import datetime, timedelta
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
cutoff_date = datetime.utcnow() - timedelta(days=60)
delete_tweets = True
delete_favorites = True
if delete_tweets:
# get all timeline tweets
print("Retrieving timeline tweets")
timeline = tweepy.Cursor(api.user_timeline).items()
deletion_count = 0
ignored_count = 0
for tweet in timeline:
# where tweets are not in save list and older than cutoff date
if tweet.created_at < cutoff_date and 'on Keybase.io' not in tweet.text:
print("Deleting %d: [%s] %s" % (tweet.id, tweet.created_at, tweet.text))
api.destroy_status(tweet.id)
deletion_count += 1
else:
ignored_count += 1
print("Deleted %d tweets, ignored %d" % (deletion_count, ignored_count))
else:
print("Not deleting tweets")
if delete_favorites:
# get all timeline likes
print("Retrieving timeline likes")
timeline = tweepy.Cursor(api.favorites).items()
deletion_count = 0
ignored_count = 0
for like in timeline:
if like.created_at < cutoff_date:
print("Deleting %d: [%s] %s" % (like.id, like.created_at, like.text))
api.destroy_favorite(like.id)
deletion_count += 1
else:
ignored_count += 1
print("Deleted %d likes, ignored %d" % (deletion_count, ignored_count))
else:
print("Not deleting likes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment