Created
December 18, 2021 21:10
-
-
Save BolajiOlajide/ed963b2e2fb50a13816726c726ed3ed9 to your computer and use it in GitHub Desktop.
Delete your tweets from your account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const tweet_1 = require('./data/tweet.js'); | |
const tweet_2 = require('./data/tweet-part1.js'); | |
const totalTweets = [...tweet_1, ...tweet_2].map(({ tweet }) => ({ | |
id: tweet.id_str, | |
createdAt: tweet.created_at | |
})); | |
fs.writeFile('./tweets.json', JSON.stringify(totalTweets, null, 2), err => { | |
if (err) { | |
console.log('File read failed:', err); | |
return; | |
} | |
console.log('File data is written'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
""" | |
A Python script to delete tweets. | |
• It uses tweet ids from a Twitter archive to bypass the Twitter API limit. Use this script if you need to delete more | |
than 3200 tweets and what's in your archive is a tweets.csv file. | |
• If you have more than 3200 tweets but your archive has a tweets.json file use this: | |
https://gist.github.com/flesueur/bcb2d9185b64c5191915d860ad19f23f | |
• If you are deleting less than 3200 tweets use this: | |
https://www.mathewinkson.com/2015/03/delete-old-tweets-selectively-using-python-and-tweepy | |
• This script is built upon the above 2 scripts, please note I haven't ran any of the above should | |
there be issues. | |
• Script runs on Python3. | |
• To preserve more recent tweets, specify the number of days to keep on line 55. To preserve specific tweets in the | |
to delete category add them to the tweets_to_keep list on line 56 | |
Requires: | |
• tweepy installed | |
• The following credentials from your Twitter app: consumer_key, consumer_secret, access_token and access_token_secret. More | |
on how to create a Twitter app here: https://developer.twitter.com/en/docs/basics/apps/overview.html | |
• An archive of your tweets. Add the path to the tweets.csv file on line 48. More on how to do this here: | |
https://help.twitter.com/en/managing-your-account/how-to-download-your-twitter-archive | |
""" | |
from datetime import datetime, timedelta | |
import pytz | |
import json | |
import tweepy | |
# Keys | |
consumer_key = '' | |
consumer_secret = '' | |
access_token = '' | |
access_token_secret = '' | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
print('Authorization request sent') | |
api = tweepy.API(auth) | |
print('Authorization successful, loading tweets archive') | |
json_obj = [] | |
with open('./tweets.json') as f: | |
tweets_list = list(json.loads(f.read())) | |
days_to_keep = 300 # number of days you want to keep, if you want to keep tweets from the last year the number will be 365 | |
tweets_to_keep = ['1175044879279411200', '1175420148511121408'] # ids of tweets you don't want to delete | |
cutoff_date = datetime.utcnow() - timedelta(days=days_to_keep) | |
failed_tweets = [] | |
for tweet in tweets_list: | |
tweet_id = tweet.get('id') # tweet[0] is the tweet_id column | |
tweet_timestamp = datetime.strptime(tweet.get('createdAt'), "%a %b %d %H:%M:%S %z %Y") # tweet[3] is the timestamp column | |
print(f'processing tweet with ID {tweet_id}') | |
if (tweet_id not in tweets_to_keep and tweet_timestamp < cutoff_date.replace(tzinfo=pytz.UTC)): | |
try: | |
# Delete tweet | |
api.destroy_status(tweet_id) | |
print('[ - ] tweet with id %s deleted' %(tweet_id)) | |
except Exception as e: | |
print(e.args, e) | |
failed_tweets.append(tweet) | |
print(f"An error occurred with tweet with ID: {tweet_id}") | |
with open('./ft.json', 'w') as f: | |
json.dump(tweets_to_keep, f) | |
print('Done deleting tweets!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment