Created
September 4, 2018 17:04
-
-
Save movitto/5ed58fa6a43040fe4dc1fb452af52bdd to your computer and use it in GitHub Desktop.
Small script to retrieve a twitter account's followers and report on new followers and 'unfollows'
This file contains 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
#!/usr/bin/ruby | |
# Small script to retrieve a twitter account's followers | |
# and report on new followers and 'unfollows' | |
require 'twitter' | |
require_relative '../lib/wipple/config' | |
ACCOUNT = 'change_me' | |
AUTH = { | |
:consumer_key => 'change me', | |
:consumer_secret => 'change me', | |
:access_token => 'change me', | |
:access_token_secret => 'change me' | |
} | |
twitter ||= Twitter::REST::Client.new do |tconfig| | |
tconfig.consumer_key = AUTH[:consumer_key] | |
tconfig.consumer_secret = AUTH[:consumer_secret] | |
tconfig.access_token = AUTH[:access_token] | |
tconfig.access_token_secret = AUTH[:access_token_secret] | |
end | |
MAX_ATTEMPTS = 3 | |
def try | |
num_attempts = 0 | |
begin | |
yield | |
rescue Twitter::Error::TooManyRequests => error | |
if num_attempts < MAX_ATTEMPTS | |
time = error.rate_limit.reset_in | |
puts "Rate Limit Exceeded, Sleeping for #{time} seconds" | |
sleep time | |
retry | |
end | |
end | |
end | |
followers = nil | |
try do | |
followers = twitter.followers(ACCOUNT) | |
end | |
path = File.expand_path("~/.twitter.followers") | |
existing = File.read(path).split("\n") | |
updated = [] | |
try do | |
followers.each { |follower| | |
updated << follower.screen_name | |
} | |
end | |
File.write(path, updated.join("\n")) | |
(existing - updated).each { |name| | |
puts "Missing: #{name}" | |
} | |
(updated - existing).each { |name| | |
puts "New: #{name}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment