Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete (very) old tweets obtained from a twitter archive
#!/bin/python3
# Largely copied from http://www.mathewinkson.com/2015/03/delete-old-tweets-selectively-using-python-and-tweepy
# However, Mathew's script cannot delete tweets older than something like a year (these tweets are not available from the twitter API)
# This script is a complement on first use, to delete old tweets. It uses your twitter archive to find tweets' ids to delete
# How to use it :
# - download and extract your twitter archive (tweet.js will contain all your tweets with dates and ids)
# - put this script in the extracted directory
# - complete the secrets to access twitter's API on your behalf and, possibly, modify days_to_keep
# - delete the few junk characters at the beginning of tweet.js, until the first '[' (it crashed my json parser)
# - review the script !!!! It has not been thoroughly tested, it may have some unexpected behaviors...
# - run this script
# - forget this script, you can now use Mathew's script for your future deletions
#
# License : Unlicense http://unlicense.org/
import tweepy
import json
from datetime import datetime, timedelta, timezone
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)
cutoff_date = datetime.now(timezone.utc) - timedelta(days=days_to_keep)
print(cutoff_date)
fp = open("tweet.js","r")
myjson = json.load(fp)
for tweet in myjson:
d = datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S %z %Y")
if d < cutoff_date:
print(tweet['created_at'] + " " + tweet['id_str'])
try:
api.destroy_status(tweet['id_str'])
except:
pass
@scallen1892

This comment has been minimized.

Show comment
Hide comment
@scallen1892

scallen1892 Jul 19, 2018

Hello friend! I'm receiving this error when I run the script:

  File "goodbye.py", line 41, in <module>
    d = datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S %z %Y")
KeyError: 'created_at'

I changed nothing whatsoever about the script before running it. Is there any idea what might be wrong?

scallen1892 commented Jul 19, 2018

Hello friend! I'm receiving this error when I run the script:

  File "goodbye.py", line 41, in <module>
    d = datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S %z %Y")
KeyError: 'created_at'

I changed nothing whatsoever about the script before running it. Is there any idea what might be wrong?

@KennardGit

This comment has been minimized.

Show comment
Hide comment
@KennardGit

KennardGit Jul 20, 2018

I had the same problem,
Try change line 41 to:

d = datetime.strptime(tweet['created_at'], "%Y-%m-%d %H:%M:%S %z")

Worked for me!

KennardGit commented Jul 20, 2018

I had the same problem,
Try change line 41 to:

d = datetime.strptime(tweet['created_at'], "%Y-%m-%d %H:%M:%S %z")

Worked for me!

@prasket

This comment has been minimized.

Show comment
Hide comment
@prasket

prasket Oct 21, 2018

I downloaded my twitter archive to try this out. After unarchiving I could not find a "tweet.js" file but under data/js I do see a tweet_index.js file which is a mapping of data/js/tweets which has many files in year_month.js format.
\I tried dumping the contents of each file other than that in the top line until [ just like oringial instructions but its just throwing errors.
myjson = json.load(fp) File "/usr/lib/python3.6/json/__init__.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "/usr/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib/python3.6/json/decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 23 column 2 (char 642)

Anyone else ran into this?

prasket commented Oct 21, 2018

I downloaded my twitter archive to try this out. After unarchiving I could not find a "tweet.js" file but under data/js I do see a tweet_index.js file which is a mapping of data/js/tweets which has many files in year_month.js format.
\I tried dumping the contents of each file other than that in the top line until [ just like oringial instructions but its just throwing errors.
myjson = json.load(fp) File "/usr/lib/python3.6/json/__init__.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "/usr/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib/python3.6/json/decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 23 column 2 (char 642)

Anyone else ran into this?

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