Skip to content

Instantly share code, notes, and snippets.

@ickas
Last active September 2, 2023 18:06
Show Gist options
  • Save ickas/115b0ddb2e7b659b6b041a354ca16e70 to your computer and use it in GitHub Desktop.
Save ickas/115b0ddb2e7b659b6b041a354ca16e70 to your computer and use it in GitHub Desktop.
Delete tweets older than X
# Tweepy is an easy-to-use Python library for accessing the Twitter API
# https://www.tweepy.org
from datetime import datetime, timedelta
import tweepy
import sys, os, time
import json
# The best way to manage your Twitter data is through the archive of all your account data
# Request archive at https://twitter.com/settings/download_your_data
# Read js archive file as json
with open("data/tweets.js") as f:
archive = json.load(f)['window.YTD.tweets.part0']
# Create a new Twitter app at https://developer.twitter.com
# On "keys and tokens" tab generate Consumer Keys and Authentication Tokens
consumer_key="your_api_key"
consumer_secret="you_api_secret"
access_token="your_api_token"
access_token_secret="your_api_access_token"
# Calling the API and set authorization
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Delete tweets before a specific date
# Or use a cutoff date based on days delete_date = datetime.now() - timedelta(days=30)
# Or use start and end dates after_date = datetime(2017, 12, 31)
before_date = datetime(2009, 1, 1)
# Check how many tweets match the rule
# Use start and end dates to choose between dates interval after_date.timestamp() < [...] < before_date.timestamp()
# Create a new array with matching tweets
matching_tweets = []
for tweet in archive:
if datetime.strptime(tweet["tweet"]["created_at"], "%a %b %d %H:%M:%S %z %Y").timestamp() < before_date.timestamp():
matching_tweets.append(tweet)
total_matching_tweets = len(matching_tweets)
print(f"Start doing magic with {total_matching_tweets} tweets...")
time.sleep(5)
counter = 0
for tweet in matching_tweets:
counter += 1
id = tweet["tweet"]["id"]
created_at = datetime.strptime(tweet["tweet"]["created_at"], "%a %b %d %H:%M:%S %z %Y").timestamp()
try:
# if created_at > after_date.timestamp() and created_at < before_date.timestamp():
if created_at < before_date.timestamp():
api.destroy_status(id)
print(f"Deleted: {str(id)} ({counter}/{total_matching_tweets})")
time.sleep(3)
except tweepy.errors.TweepyException as e:
if e.response.status_code == 404:
print(f"Tweet not found: {str(id)} ({counter}/{total_matching_tweets})")
continue
else:
print("Error with: " + str(id))
print("Done! ✅")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment