Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# -*- coding: utf-8 -*-
""" Deletes all tweets below a certain retweet threshold.
import tweepy
from datetime import datetime
# Constants
# Connect To Your Twitter Account via Twitter API
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth,
retry_errors=set([401, 404, 500, 503]))
# For the account name
def wipe(account_name=USER_NAME, favorite_threshold=100, days=62):
# Get the current datetime
current_date = datetime.utcnow()
# For each tweet
for status in tweepy.Cursor(api.user_timeline, screen_name='@'+account_name).items():
# Get the tweet id
status_id = status._json['id']
print("%d/%m/%Y %H:%M:%S"), 'Examining', status_id)
# Get the number of favorites
status_favorites = status._json['favorite_count']
# 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:
# If the number of favorites is lower than the favorite threshold
if status_favorites < favorite_threshold:
# If you haven't favorited the tweet yourself
if status_favorited == False:
# Delete the tweet
print("%d/%m/%Y %H:%M:%S"), 'Deleting', status_id)
# Run main function
if __name__ == '__main__':
wipe(account_name='chrisalbon', favorite_threshold=100, days=62)
Copy link

regularperson2586 commented Feb 13, 2021

is there any way to surpass the 3200 limit?

Copy link

felipealfonsog commented Feb 24, 2022

I got several errors;

Traceback (most recent call last): File "/Users/felipe/Documents/deltwitts/", line 10, in <module> CONSUMER_KEY = from_env('xxxxxxxxxxxxx') NameError: name 'from_env' is not defined

and, I inserted all the keys ...


Copy link

askpatrickw commented Feb 25, 2022

@felipealfonsog the function from_env() is not part of this sample. I suggest you look at os.getenv() in the standard library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment