Skip to content

Instantly share code, notes, and snippets.

@musale
Forked from k0nserv/README.md
Created August 19, 2021 10:17
Show Gist options
  • Save musale/5da195147df3f7cbd23fec8a0a5578b9 to your computer and use it in GitHub Desktop.
Save musale/5da195147df3f7cbd23fec8a0a5578b9 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment