-
-
Save drownranger/575765424de9fa380e050bbd20109586 to your computer and use it in GitHub Desktop.
Delete old tweets based on twitter archive
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
#!/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 | |
# License : Unlicense http://unlicense.org/ | |
# Don't forget to remove the first variable on tweet.js | |
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 | |
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", encoding='UTF-8') | |
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: | |
api.destroy_status(myjson[i]['tweet']['id_str']) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment