Skip to content

Instantly share code, notes, and snippets.

@martjanz
Last active November 12, 2020 00:09
Show Gist options
  • Save martjanz/4dbb2bd7e25298ec2bc0228a8755d3af to your computer and use it in GitHub Desktop.
Save martjanz/4dbb2bd7e25298ec2bc0228a8755d3af to your computer and use it in GitHub Desktop.
Deletuips: just another bulk tweet deletion tool using requested Twitter archive
#!/bin/python3
#
# Requirements:
# - Python 3
# - tweepy Python library (pip install tweepy)
# - Twitter API keys and secrets (from https://developer.twitter.com/)
#
# Instructions:
# - Request and download your Twitter archive (from https://twitter.com/settings/your_twitter_data)
# - Rename downloaded zip file to twitter-archive.zip
# - Put this script and twitter-archive.zip on the same directory
# - Replace (****) with your Twitter API keys and secrets
# - Check how many days from today to the past you want to keep. Default: 365 days (that's one year, most of the time)
# - Run this script (python deletuips.py)
# - After finished keep API keys, secrets and archive safe, or delete everything.
# - That's all.
# License : Unlicense http://unlicense.org/
import json
import shutil
from datetime import datetime, timedelta, timezone
import tweepy
from zipfile import ZipFile
consumer_key = '****************'
consumer_secret = '****************'
access_token = '****************'
access_token_secret = '****************'
days_to_keep = 365
tweets_file = 'tmp-archive/data/tweet.js'
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)
def get_confirmation():
# raw_input returns the empty string for "enter"
yes = {'yes', 'y', 'ye'}
no = {'no', 'n', ''}
choice = input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
sys.stdout.write("Please respond with 'yes' or 'no'")
print('WARNING: This script will delete ALL tweets until {}. Are you REALLY sure? (yes/no)'.format(cutoff_date))
if not get_confirmation():
print('Confirmation denied. NO DELETED TWEETS. Bye.')
exit()
zip_name = 'twitter-archive.zip'
zip_obj = ZipFile(zip_name, 'r')
zip_obj.extractall('tmp-archive')
## Edit file
file_obj = open(tweets_file, "r")
data = file_obj.read()
file_obj.close()
data = data[25:]
file_obj = open(tweets_file, "w")
file_obj.write(data)
file_obj.close()
fp = open(tweets_file,"r")
json_tweets = json.load(fp)
for item in json_tweets:
tweet = item['tweet']
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
shutil.rmtree('tmp-archive')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment