Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

jaimeiniesta commented Jul 3, 2018

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
You can’t perform that action at this time.