Skip to content

Instantly share code, notes, and snippets.

@flerpadoo
Last active August 19, 2019 22:52
Show Gist options
  • Save flerpadoo/6c793aa1c774dc84387167b5fba73e19 to your computer and use it in GitHub Desktop.
Save flerpadoo/6c793aa1c774dc84387167b5fba73e19 to your computer and use it in GitHub Desktop.
I'm a bad person. I should have just fixed the script in the repo of the original author, but opted to steal the TweetDestroyer module instead and wrap it with updated code to support the twitter json (not csv) - thanks @koenrh !
from datetime import datetime
import json, os, twitter, io, time
string_input_with_date = "01/01/2018"
file_name = 'tweet.js'
TWITTER_CONSUMER_KEY=""
TWITTER_CONSUMER_SECRET=""
TWITTER_ACCESS_TOKEN=""
TWITTER_ACCESS_TOKEN_SECRET=""
class TweetDestroyer(object):
def __init__(self, twitter_api):
self.twitter_api = twitter_api
def destroy(self, tweet_id):
try:
print("delete tweet %s" % tweet_id)
self.twitter_api.DestroyStatus(tweet_id)
time.sleep(0.5)
except twitter.TwitterError as err:
print("Exception: %s\n" % err.message)
api = twitter.Api(consumer_key=TWITTER_CONSUMER_KEY,
consumer_secret=TWITTER_CONSUMER_SECRET,
access_token_key=TWITTER_ACCESS_TOKEN,
access_token_secret=TWITTER_ACCESS_TOKEN_SECRET)
destroyer = TweetDestroyer(api)
inputDate = datetime.strptime(string_input_with_date, "%d/%m/%Y")
json_data = json.loads(io.open(file_name, mode='r', encoding='utf-8').read()[25:])
count = 0
for tweet in json_data:
tweetDate = datetime.strptime(tweet['created_at'],"%a %b %d %H:%M:%S +0000 %Y")
if tweetDate < inputDate:
destroyer.destroy(tweet['id'])
count += 1
print('Deleted ' + str(count) + ' tweets!')
@iamselasie
Copy link

Could you please write a detailed guide on how to use this script? Thanks

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