module DeleteMyTweets

Delete my tweets

Project constants

Constants

ACCESS_TOKEN
ACCESS_TOKEN_SECRET
CONFIG
CONSUMER_KEY
CONSUMER_SECRET
DELETE_APP_CONSUMER_KEY
DELETE_APP_CONSUMER_SECRET
OPT_CONFIG
PROJECT
VERSION

Public Class Methods

cli() click to toggle source
# File lib/delete_my_tweets/cli.rb, line 9
def cli
  puts "#{PROJECT} #{VERSION}"

  if ARGV.include? OPT_CONFIG
    config_create
    exit
  end

  if config_missing? CONFIG
    puts "config missing, type: #{PROJECT} #{OPT_CONFIG}"
    exit
  end

  c = config_get(CONFIG)

  twitter_delete(c) do |o|
    puts o
  end
  puts 'all done 🐤'
end
config_create() click to toggle source
# File lib/delete_my_tweets/config.rb, line 6
def config_create()
  c = TwitterOAuthToken::consumer(DELETE_APP_CONSUMER_KEY, DELETE_APP_CONSUMER_SECRET)
  request_token = TwitterOAuthToken::request_token(c)
  url = TwitterOAuthToken::authorize_url(request_token)

  puts "open this url in your browser (sign in and authorize): #{url}"

  print "enter pin: "
  pin = STDIN.gets.strip

  begin
    access_token = TwitterOAuthToken::access_token(request_token, pin)
  rescue => e
    puts "error: #{e}"
    exit
  end

  config = {
    CONSUMER_KEY => DELETE_APP_CONSUMER_KEY,
    CONSUMER_SECRET => DELETE_APP_CONSUMER_SECRET,
    ACCESS_TOKEN => access_token.token,
    ACCESS_TOKEN_SECRET => access_token.secret
  }

  File.open(CONFIG, 'w') { |f| f.write config.to_yaml }
  puts "wrote #{CONFIG} 🐤"
end
config_get(file) click to toggle source
# File lib/delete_my_tweets/config.rb, line 34
def config_get(file)
  f = File.read(file)
  YAML.load(f)
end
config_missing?(file) click to toggle source
# File lib/delete_my_tweets/config.rb, line 39
def config_missing?(file)
  !File.exist?(file)
end
twitter_delete(t) click to toggle source
# File lib/delete_my_tweets/twitter.rb, line 6
def twitter_delete(t)
  client = Twitter::REST::Client.new do |config|
    config.consumer_key        = t[CONSUMER_KEY]
    config.consumer_secret     = t[CONSUMER_SECRET]
    config.access_token        = t[ACCESS_TOKEN]
    config.access_token_secret = t[ACCESS_TOKEN_SECRET]
  end

  options = {count: 200, include_rts: true}
  user = client.user.screen_name

  begin
    exclude = t['filter']['exclude']
  rescue
    exclude = []
  end
  puts "excluding: #{exclude}" if block_given? && exclude.count>0

  begin
    tweets = client.user_timeline(user, options)
    puts "found #{tweets.count} tweets" if block_given?
    tweets.each_with_index do |t, i|
      r = exclude.any? { |i| t.text.include? i }
      if r == false
        puts "#{i+1} 👋  deletin #{t.text}" if block_given?
        client.destroy_status t
      end
    end
  rescue Twitter::Error::TooManyRequests => error
    sleep error.rate_limit.reset_in + 1
    retry
  end
end