Skip to content

Instantly share code, notes, and snippets.

@markmals
Last active September 18, 2020 05:33
Show Gist options
  • Save markmals/b9bca136843b04f22d7b21dbe934b432 to your computer and use it in GitHub Desktop.
Save markmals/b9bca136843b04f22d7b21dbe934b432 to your computer and use it in GitHub Desktop.
import tweepy # pip3 install tweepy
import json
from datetime import datetime, timedelta, timezone
CONSUMER_KEY = "**********"
CONSUMER_SECRET = "**********"
def oauthLogin(consumerKey, consumerSecret):
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
authURL = auth.get_authorization_url()
verifyCode = input(f"Authenticate at {authURL} and then enter you verification code here > ")
auth.get_access_token(verifyCode)
return tweepy.API(auth)
def dumpJSON(api, directory):
out = []
for status in tweepy.Cursor(api.user_timeline).items():
out.append(status._json)
with open(f"{directory}/tweets.json", "w", encoding = "utf-8") as file:
json.dump(out, file, ensure_ascii = False, indent = 4)
def confirmDelete(api):
print(f"You are about to Delete all tweets from the account @{api.verify_credentials().screen_name}.")
print("Does this sound ok? There is no undo! Type yes to carry out this action.")
shouldDelete = input("> ")
if shouldDelete.lower() == "yes" or shouldDelete.lower() == "y":
return True
else:
return False
def destroyTweet(id, text):
try:
api.destroy_status(id)
print(f"Deleted: {id}")
print(f"{text}")
print()
except:
print(f"Failed to delete: {id}")
print(f"{text}")
print()
def batchDelete(api):
excludedIDs = []
flaggedKeywords = []
for status in tweepy.Cursor(api.user_timeline).items():
# Constants
id = status.id
text = status._json["text"]
isOriginalTweet = not status._json["retweeted"]
twoDaysAgo = datetime.now(timezone.utc) - timedelta(days = 2)
tweetCreatedAt = datetime.strptime(status._json["created_at"], "%a %b %d %H:%M:%S %z %Y")
if confirmDelete(api):
# Only delete tweets I don't want to keep
if id not in excludedIDs:
# Delete my own tweets
if isOriginalTweet:
# Delete inflamatory tweets
if any(x in text for x in flaggedKeywords):
destroyTweet(id, text)
# Delete retweets
else:
# From more than 2 days ago
if (tweetCreatedAt < twoDaysAgo):
destroyTweet(id, text)
if __name__ == "__main__":
api = oauthLogin(consumerKey = CONSUMER_KEY, consumerSecret = CONSUMER_SECRET)
print("Authenticated as: %s" % api.me().screen_name)
# dumpJSON(api, "~/Desktop")
batchDelete(api)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment