Skip to content

Instantly share code, notes, and snippets.

@tut-tuuut
Last active May 17, 2018 20:12
Show Gist options
  • Save tut-tuuut/62a5d0da0adb23f1adeda5be99dc99a1 to your computer and use it in GitHub Desktop.
Save tut-tuuut/62a5d0da0adb23f1adeda5be99dc99a1 to your computer and use it in GitHub Desktop.
Delete every tweet contained in your tweet archive. Forked from https://gist.github.com/robinsloan/3688616
require "rubygems"
require "twitter"
require "json"
# things you must configure
TWITTER_USER = ""
TWITARCHIVE_TWEETS = "/eg/path/to/your/twitarchive/data/js/tweets"
# get these from dev.twitter.com
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""
# any special favorites?
IDS_TO_SAVE_FOREVER = [699343110787198976] # my panini tweet
### you shouldn't have to change anything below this line ###
TWEETS_PER_REQUEST = 200
### A METHOD ###
def delete_from_twitter(tweet, client)
begin
client.destroy_status(tweet['id'])
rescue StandardError => e
puts e.inspect
puts "Error deleting #{tweet['id']}; continue anyway"
#exit
else
puts "Deleted #{tweet['id']}"
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 ""
Dir.glob("#{TWITARCHIVE_TWEETS}/*.js") do |filepath|
tweets = []
got_tweets = true
oldest_tweet_id = 9000000000000000000
puts "Parsing #{filepath}…"
contents = File.read(filepath)
contents.gsub!(/Grailbird.data.tweets_20\d\d_\d\d = /, '')
tweets=JSON.parse(contents)
puts ""
puts "Got #{tweets.length} tweets total"
puts ""
deleted_tweets = 0
ignored_tweets = 0
tweets.each do |tweet|
begin
if IDS_TO_SAVE_FOREVER.include?(tweet['id']) then
puts "Ignored a tweet that is to be saved forever."
ignored_tweets += 1
else
puts "Deleting tweet #{tweet['id']}"
delete_from_twitter(tweet, client)
deleted_tweets += 1
end
puts 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
puts "--------------- DONE! -----------------------"
puts "File #{filepath}"
puts "Deleted #{deleted_tweets} tweets."
puts "Ignored #{ignored_tweets} tweets."
puts "---------------------------------------------"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment