Skip to content

Instantly share code, notes, and snippets.

@k0nserv
Forked from chrisalbon/tweet_deleting_script.py
Last active February 24, 2022 13:47
Show Gist options
  • Save k0nserv/d8c6f110f001ee960a5684357431f033 to your computer and use it in GitHub Desktop.
Save k0nserv/d8c6f110f001ee960a5684357431f033 to your computer and use it in GitHub Desktop.

Delete All your Tweets

This script it based on @chrisalbon's original script, but instead of fetching the users timeline via the Twitter API it uses the archive of the users data to workaround the 3200 tweet limitation.

Setup

  1. Create a virtualenv
  2. Run pip install -r requirements.txt
  3. Create a Twitter developer app.

Preparing the data

  1. Download an archive of your Twitter data from Twitter. (link)
  2. Copy data/tweet.js to your working directory and call the file all-tweets.json.
  3. Remove the window.YTD.tweet.part0 = part of the first line of the file.

Running

  1. Set the environment variables TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_ACCESS_TOKEN, and TWITTER_ACCESS_SECRET.
  2. Run python wipe.py
  3. ?
  4. Profit

Potentail Improvements

  • This process could be made faster with multiprocessing or threads to be faster.
certifi==2020.12.5
chardet==4.0.0
idna==2.10
oauthlib==3.1.0
PySocks==1.7.1
requests==2.25.1
requests-oauthlib==1.3.0
six==1.15.0
tweepy==3.10.0
urllib3==1.26.4
wheel==0.24.0
# -*- coding: utf-8 -*-
""" Deletes all tweets below a certain retweet threshold.
Original by @chrisalbon
Modified by @k0nserv
"""
from datetime import datetime
import os
import json
import tweepy
def from_env(key):
value = os.getenv(key, None)
if value is None:
raise ValueError(f"Env variable {key} must be provided")
return value
# Constants
CONSUMER_KEY = from_env('TWITTER_CONSUMER_KEY')
CONSUMER_SECRET = from_env('TWITTER_CONSUMER_SECRET')
ACCESS_TOKEN = from_env('TWITTER_ACCESS_TOKEN')
ACCESS_SECRET = from_env('TWITTER_ACCESS_SECRET')
# Connect To Your Twitter Account via Twitter API
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=3,
retry_delay=5,
retry_errors=set([401, 404, 500, 503]))
def load_deleted():
with open('deleted.json') as f:
return set(json.load(f))
def save_deleted(deleted):
with open('deleted.json', 'w') as f:
json.dump(list(deleted), f)
def delete_all_tweets(ids):
deleted = load_deleted()
for (idx, status_id) in enumerate(ids):
if status_id in deleted:
continue
print(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), 'Deleting', status_id)
try:
api.destroy_status(status_id)
deleted.add(status_id)
if idx % 10 == 0:
save_deleted(deleted)
except tweepy.error.TweepError as e:
if e.api_code == 144:
deleted.add(status_id)
save_deleted(deleted)
pass
else:
raise e
# Run main function
if __name__ == '__main__':
with open('all-tweets.json') as f:
data = json.load(f)
delete_all_tweets((tweet['tweet']['id'] for tweet in data))
@felipealfonsog
Copy link

felipealfonsog commented Feb 24, 2022

Hi,
I get this error:

Traceback (most recent call last): File "/Users/felipe/Documents/deltwitt.py", line 26, in <module> CONSUMER_KEY = from_env('xxxxxxxxxxxxxxxx') File "/Users/felipe/Documents/deltwitts/deltwitt.py", line 20, in from_env raise ValueError(f"Env variable {key} must be provided") ValueError: Env variable xxxxxxxxxxx must be provided

and, I inserted all the keys ....

Cheers,

@k0nserv
Copy link
Author

k0nserv commented Feb 24, 2022

Are you sure you actually managed to add all keys? That exception is only thrown if any are missing, in your case TWITTER_CONSUMER_KEY.

I'd suggest adding print(os.environ) on line 24 and checking the output carefully

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