Skip to content

Instantly share code, notes, and snippets.

@neilzone
Forked from n-eq/deloldtweets.py
Last active March 21, 2021 13:57
Show Gist options
  • Save neilzone/34c8a5f7960542691f0cd9309c7de92d to your computer and use it in GitHub Desktop.
Save neilzone/34c8a5f7960542691f0cd9309c7de92d to your computer and use it in GitHub Desktop.
Delete old tweets based on twitter archive
#!/bin/python3
# Fork of https://gist.github.com/davej/113241
# Requirements:
# - twitter API credentials (replace the correponding variables)
# - tweet.js file you get by extracting your twitter archive zip file (located in data/)
# License : Unlicense http://unlicense.org/
import tweepy
import pytz
import json
from datetime import datetime, timedelta
from dateutil import parser
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
days_to_keep = 365 #This will keep the last year's worth of tweets, unless you change it
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
utc = pytz.UTC
cutoff_date = utc.localize(datetime.now() - timedelta(days=days_to_keep))
fp = open("tweet.js","r")
fp.seek(25) # skip to '[' which is the beginning of the json part
myjson = json.load(fp)
for i in range(len(myjson)):
parsed_date = parser.parse(myjson[i]['tweet']['created_at'])
# print(parsed_date)
if parsed_date < cutoff_date:
# beware, this operation is irreversible
try:
print("Destroying tweet %s" % myjson[i]['tweet']['id_str'])
api.destroy_status(myjson[i]['tweet']['id_str'])
except Exception as e:
print("There was an exception: %s." % e)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment