Skip to content

Instantly share code, notes, and snippets.

@coilysiren
Last active August 6, 2016 14:41
Show Gist options
  • Save coilysiren/6b13e2528508349e05d6 to your computer and use it in GitHub Desktop.
Save coilysiren/6b13e2528508349e05d6 to your computer and use it in GitHub Desktop.
Unfollows: (a) people who dont follow you back or (b) people you haven't talked to publically or (c) people who haven't posted in 3 months
---
# start working from % of your most recent follows
# ex: for 1000 follows, 5 = ignore the last 50 accounts you followed
offset: 5
# what you use at https://twitter.com/login
# DONT LET THIS GET POSTED PUBLICLY !!!!!!!!!!!!!!!!!!!!!!!!!!!!
username: "XXXXXXXXX"
password: "XXXXXXXXX"
# from https://apps.twitter.com/
# DONT LET THIS GET POSTED PUBLICLY !!!!!!!!!!!!!!!!!!!!!!!!!!!!
consumer_key: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
consumer_secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
access_token: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
access_token_secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# people you dont want to unfollow, no matter what
# For me this is really good friends (who don't post much) and news orgs
exceptions:
- "twitter"
- "POTUS"
# install ruby, and then:
gem install rails twitter capybara
ruby twitter_unfollow.rb
require 'yaml'
require 'twitter'
require 'rails/all'
require 'capybara'
require 'capybara/dsl'
config = YAML.load_file('config.yaml')
#############
# FUNCTIONS #
#############
class Login
include Capybara::DSL
def do(username, password)
visit '/login'
find(".js-username-field").set(username)
find(".js-password-field").set(password)
find(".signin-wrapper .submit").click
end
end
class CheckForIF
include Capybara::DSL
def you_havent_talked
begin
visit("/search?f=tweets&q=@#{$you.screen_name}+@#{$them.screen_name}")
find("ol#stream-items-id li.stream-item:first-child")
return false
rescue Capybara::ElementNotFound
return true
end
end
def they_dont_follow_you
begin
visit("/@#{$them.screen_name}")
find(".ProfileHeaderCard .FollowStatus")
return false
rescue Capybara::ElementNotFound
return true
end
end
def they_havent_tweeted_recently
three_months_ago = Time.now.to_i - 3*60*60*24*31
tweet_created = $t.user_timeline($them, :include_rts=>false)[0].created_at.to_i
return tweet_created < three_months_ago
end
end
def unfollow_because(msg)
puts "#{$them.name} (@#{$them.screen_name}) http://twitter.com/@#{$them.screen_name}"
puts "\t#{msg}"
$t.unfollow($them.screen_name)
end
#########
# SETUP #
#########
# webkit
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.twitter.com'
Capybara.default_max_wait_time = 10
Login.new.do(config['username'], config['password'])
# api
$t = Twitter::REST::Client.new do |c|
c.consumer_key = config['consumer_key']
c.consumer_secret = config['consumer_secret']
c.access_token = config['access_token']
c.access_token_secret = config['access_token_secret']
end
$you = $t.user
following = $t.friend_ids($you.id).to_a
################
# DO THE THING #
################
e = 0
offset = following.length*config['offset']/100
following = following.slice(offset, following.length)
puts "Starting script offset by the #{offset} (#{config['offset']}%) most recent follows"
time_start = Time.now
following.shuffle.each_with_index do |id,i|
include ActionView::Helpers::DateHelper
$them = $t.user(id)
percent = 100*(i)/(following.length)
time = distance_of_time_in_words_to_now(time_start)
puts "#{i}/#{following.length} (#{percent}%) -- Started #{time} ago"
if config['exceptions'].include? $them.screen_name
next
end
if CheckForIF.new.they_dont_follow_you
unfollow_because "They don't follow you"
elsif CheckForIF.new.they_havent_tweeted_recently
unfollow_because "They haven't tweeted in 3 months"
elsif CheckForIF.new.you_havent_talked
unfollow_because "You haven't talked publically"
end
end
@coilysiren
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment