Last active
August 19, 2019 22:52
-
-
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 !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please write a detailed guide on how to use this script? Thanks