Created
October 30, 2018 11:58
-
-
Save mmotherwell/11a51472b7b46e11f3310bc98b37237d to your computer and use it in GitHub Desktop.
Ruby: Delete all your tweets older than this moth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :twitter_deleter do | |
desc 'Deletes all your tweets older than a month' | |
task delete_old_tweets: :environment do | |
your_username = 'thisisme' | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = "YOUR_CONSUMER_KEY" | |
config.consumer_secret = "YOUR_CONSUMER_SECRET" | |
config.access_token = "YOUR_ACCESS_TOKEN" | |
config.access_token_secret = "YOUR_ACCESS_SECRET" | |
end | |
def collect_with_max_id(collection=[], max_id=nil, &block) | |
response = yield(max_id) | |
collection += response | |
response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block) | |
end | |
def client.get_all_tweets(user) | |
collect_with_max_id do |max_id| | |
options = {count: 200, include_rts: true} | |
options[:max_id] = max_id unless max_id.nil? | |
user_timeline(user, options) | |
end | |
end | |
delete_before = DateTime.now.beginning_of_month | |
client.get_all_tweets(your_username).each do |tweet| | |
if tweet.created_at > delete_before | |
puts "#{tweet.id} tweeted at #{tweet.created_at} STAYS" | |
else | |
puts "#{tweet.id} tweeted at #{tweet.created_at} DELETEING" | |
begin | |
destroyed = client.destroy_tweet(tweet.id) | |
rescue Twitter::Error::Unauthorized => e | |
puts "NO GO UNAUTHORISED" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment