Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Last active February 8, 2023 11:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robinsloan/e1c59db85180e142e157cf0a5e91671c to your computer and use it in GitHub Desktop.
Save robinsloan/e1c59db85180e142e157cf0a5e91671c to your computer and use it in GitHub Desktop.
Tweet delete script using your Twitter archive as an index
require "rubygems"
require "twitter"
require "json"
require "csv"
#require "date"
# things you must configure
TWITTER_USER = "your_twitter_username"
MAX_AGE_IN_DAYS = 7 # anything older than this is deleted
# get these from dev.twitter.com
CONSUMER_KEY = "your_consumer_key"
CONSUMER_SECRET = "your_consumer_secret"
OAUTH_TOKEN = "your_oauth_token"
OAUTH_TOKEN_SECRET = "your_oauth_secret"
# full path to your unzipped tweet archive -- a directory
ARCHIVE_PATH = "/Users/your_computer_username/Downloads/some_long_cryptic_string"
# any special favorites?
IDS_TO_SAVE_FOREVER = [369855688836538368] # my panini tweet
# this flag is here so you can do a test run first
ACTUALLY_DELETE = false
### you shouldn't have to change anything below this line ###
MAX_AGE_IN_SECONDS = MAX_AGE_IN_DAYS*24*60*60
NOW_IN_SECONDS = Time.now
### A METHOD ###
def delete_from_twitter(tweet_id, client)
if ACTUALLY_DELETE
begin
client.destroy_status(tweet_id)
rescue StandardError => e
puts e.inspect
puts "Error deleting #{tweet_id}; exiting"
exit
else
puts "Deleted #{tweet_id}"
end
end
end
### WE BEGIN ###
client = Twitter::REST::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = OAUTH_TOKEN
config.access_token_secret = OAUTH_TOKEN_SECRET
end
puts ""
puts "What's that sound...?"
puts ""
tweets = CSV.read(File.join(ARCHIVE_PATH, "tweets.csv"))[1 .. -1] # skip header row
tweets.each do |tweet|
begin
created_at = Time.parse(tweet[3])
tweet_id = tweet[0].to_i
tweet_age = NOW_IN_SECONDS - created_at
tweet_age_in_days = (tweet_age/(24*60*60)).round
if (tweet_age < MAX_AGE_IN_SECONDS) then
puts "Ignored a tweet #{tweet_age_in_days} days old"
elsif IDS_TO_SAVE_FOREVER.include?(tweet_id) then
puts "Ignored a tweet that is to be saved forever"
else
if ACTUALLY_DELETE
puts "Deleting a tweet #{tweet_age_in_days} days old"
delete_from_twitter(tweet_id, client)
else
puts "Would have deleted a tweet #{tweet_age_in_days} days old"
end
end
puts " #{tweet[5]}" # tweet text
puts ""
rescue Twitter::Error::TooManyRequests => e
puts "Hit the rate limit; pausing for #{e.rate_limit.reset_in} seconds"
sleep e.rate_limit.reset_in
retry
rescue StandardError => e
puts e.inspect
exit
end
end
@jaimeiniesta
Copy link

It looks like this no longer will work - now from https://twitter.com/settings/your_twitter_data we get JS files, not CSV.

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