Skip to content

Instantly share code, notes, and snippets.

@mattly
Forked from robinsloan/langoliers.rb
Last active March 21, 2016 19:37
Show Gist options
  • Save mattly/e0ea598f470759d50b12 to your computer and use it in GitHub Desktop.
Save mattly/e0ea598f470759d50b12 to your computer and use it in GitHub Desktop.
Rolling Tweet Deletion
#!/usr/bin/env ruby
require 'rubygems'
require 'twitter'
require 'json'
require 'faraday'
# things you must configure
TWITTER_USER = "you"
# get these from dev.twitter.com
CONSUMER_KEY = "-"
CONSUMER_SECRET = "-"
OAUTH_TOKEN = "aka access key"
OAUTH_TOKEN_SECRET = "aka access secret"
# things you might want to change
MAX_AGE_IN_DAYS = 10 # anything older than this is deleted / unfavorited
DELAY_BETWEEN_DELETES = 0.5 # in seconds
DELAY_BETWEEN_REQS = 45
# save tweets that have been massively favorited or retweeted??? nah. all is vanity.
POINT_THRESHOLD = 9999
# you don't need to mess with this
API_TWEET_MAX = 3200
TWEETS_PER_PAGE = 200 # api max is 250 or something... 200 seems like a nice round number
NUM_PAGES = (API_TWEET_MAX / TWEETS_PER_PAGE).to_i
MAX_AGE_IN_SECONDS = MAX_AGE_IN_DAYS*24*60*60
NOW_IN_SECONDS = Time.now
FIELDS = [:id,
:created_at,
:text,
:retweet_count,
:in_reply_to_screen_name,
:in_reply_to_user_id,
:in_reply_to_status_id]
# tweet methods
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
def delete_from_twitter(tweet,client)
begin
client.destroy_status(tweet.id)
rescue StandardError => e
puts e.inspect
puts "Error deleting #{tweet.id} "
# exit
else
puts "Deleted"
end
end
# init twitter
error_count = 0
del_count = 0
timeline = []
begin
tweet_max = client.user(TWITTER_USER).statuses_count < 3200 ? client.user(TWITTER_USER).statuses_count : API_TWEET_MAX
oldest_page = (tweet_max / TWEETS_PER_PAGE).to_i # we go to this page -- the older tweets -- first
puts "#{tweet_max} tweets available, Oldest page is #{oldest_page}"
rescue StandardError => e
puts e
puts "Error getting info about @#{TWITTER_USER}. Try again."
exit
end
(1..oldest_page).each do |i|
begin
puts "Requesting tweets #{i*TWEETS_PER_PAGE}-#{(i+1)*TWEETS_PER_PAGE}"
timeline = timeline.concat(client.user_timeline(TWITTER_USER,{:count=>TWEETS_PER_PAGE,:page=>i,:include_entities=>true,:include_rts=>true}))
rescue
error_count += 1
if (error_count > 5) then
puts "Too many errors! Try again later."
exit
else
puts "Error getting tweets #{oldest_page*TWEETS_PER_PAGE}-#{tweet_max}, retrying after #{2**error_count} seconds..."
sleep(2**error_count)
retry
end
end
sleep DELAY_BETWEEN_DELETES
end
puts "Got #{timeline.size} tweets"
puts ""
timeline.each do |tweet|
tweet_age = NOW_IN_SECONDS - tweet.created_at
if (tweet_age < MAX_AGE_IN_SECONDS) then
puts "Ignored a tweet #{(tweet_age/(24*60*60)).round} days old"
elsif (tweet.favorite_count + tweet.retweet_count >= POINT_THRESHOLD) and (! tweet.text.match(/^RT/)) then
puts "Ignored a tweet with #{tweet.favorite_count} faves and #{tweet.retweet_count} RTs"
else
puts "Preparing to delete a tweet #{(tweet_age/(24*60*60)).round} days old"
delete_from_twitter(tweet,client)
del_count += 1
end
puts " #{tweet.text}"
puts ""
sleep DELAY_BETWEEN_DELETES
end
puts "deleted #{del_count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment