Skip to content

Instantly share code, notes, and snippets.

@jkeirstead
Forked from chrisalbon/tweet_deleting_script.py
Last active January 7, 2021 21:39
Show Gist options
  • Save jkeirstead/97d0e1eba9adb4e47890fd0b026f0280 to your computer and use it in GitHub Desktop.
Save jkeirstead/97d0e1eba9adb4e47890fd0b026f0280 to your computer and use it in GitHub Desktop.
credentials.py
__pycache__/
archived_tweets*.json
# -*- coding: utf-8 -*-
""" Deletes all tweets below a certain retweet threshold.
"""
import tweepy
import json
import credentials as cred
from datetime import datetime
# Connect To Your Twitter Account via Twitter API
auth = tweepy.OAuthHandler(cred.CONSUMER_KEY, cred.CONSUMER_SECRET)
auth.set_access_token(cred.ACCESS_TOKEN, cred.ACCESS_SECRET)
api = tweepy.API(auth,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=3,
retry_delay=5,
retry_errors=set([401, 404, 500, 503]))
# For the account name
def wipe(account_name=cred.USER_NAME, favorite_threshold=100, days=62):
# Get the current datetime
current_date = datetime.utcnow()
# Create an empty list of tweets to save
archived_tweets = []
total_tweets = 0
# For each tweet
for status in tweepy.Cursor(api.user_timeline, screen_name='@'+account_name).items():
# Increment counter
total_tweets += 1
# Get the tweet id
status_id = status._json['id']
print(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), 'Examining', status_id)
# Get the number of favorites and retweets
status_favorites = status._json['favorite_count']
is_retweet = status._json['retweeted']
# Get the datetime of the tweet
status_date = datetime.strptime(status._json['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
# Get whether you have favorited the tweet yourself
status_favorited = status._json['favorited']
# If the difference between the current datetime and the tweet's
# is more than a day threshold
if (datetime.utcnow() - status_date).days > days:
# Archive the tweet if
# - it's a retweet, or
# - the number of favourites is lower than the favourite threshold
if is_retweet or (status_favorites < favorite_threshold):
# If you haven't favorited the tweet yourself
if status_favorited == False:
print(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), 'Unloved message', status_id)
# Save the tweet
archived_tweets = archived_tweets + [status]
# Save the archived tweets
print('Archiving %d out of %d tweets' % (len(archived_tweets), total_tweets))
with open('archived_tweets.json', 'w') as f:
json.dump([status._json for status in archived_tweets], f, ensure_ascii=False, indent=4)
# Delete them from Twitter
confirm = input("Confirm deletion? Y/N ")
if (confirm.upper() == 'Y'):
for status in archived_tweets:
print('Deleting message %s' % status._json['id'])
try:
api.destroy_status(status._json['id'])
except Exception as e:
print(str(e))
# Run main function
if __name__ == '__main__':
wipe(account_name='jameskeirstead', favorite_threshold=1, days=90)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment