Skip to content

Instantly share code, notes, and snippets.

@0x263b
Last active July 14, 2016 15:07
Show Gist options
  • Save 0x263b/7b391a1617fcbbabc57fb1e705884a11 to your computer and use it in GitHub Desktop.
Save 0x263b/7b391a1617fcbbabc57fb1e705884a11 to your computer and use it in GitHub Desktop.
Twitter Goggles
#!/usr/bin/env ruby
# encoding: utf-8
# Ruby version of Twitter Goggles
# https://github.com/ardubs/goggles
require 'oauth'
require 'json'
# Create a twitter app with Read and Write, then fill the values below
# http://iag.me/socialmedia/how-to-create-a-twitter-app-in-8-easy-steps/
TWITTER_CONSUMER_KEY = ""
TWITTER_CONSUMER_SECRET = ""
TWITTER_ACCESS_TOKEN = ""
TWITTER_ACCESS_TOKEN_SECRET = ""
def prepare_access_token(oauth_token, oauth_token_secret)
consumer = OAuth::Consumer.new(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, {:site => "https://api.twitter.com", :scheme => :header })
token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }
access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
return access_token
end
def open(link)
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ # Windows
system "start #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/ # Mac
system "open #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ # GNU/Linux and BSD
system "xdg-open #{link}"
end
end
def get_friends(screen_name)
begin
raise "“#{screen_name}” is not a valid twitter handle" if !!(screen_name =~ /[^\w]/)
# Generate OAuth access token
access_token = prepare_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET)
# Get following list for `screen_name`
request = access_token.request(:get, "https://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name=#{screen_name}&count=5000")
raise "Could not retrieve followed accounts" if request.code != "200"
response = JSON.parse(request.body, {:symbolize_names => true})
ids = response[:ids]
total = ids.length
raise "Could not retrieve followed accounts" if total == 0
"Retrieved followers"
# Get our lists
request = access_token.request(:get, "https://api.twitter.com/1.1/lists/list.json")
response = JSON.parse(request.body, {:symbolize_names => true})
# Check if we already have a list
list = response.find{ |list| list[:slug] == screen_name }
# Otherwise make one
if list.nil?
request = access_token.request(:post, "https://api.twitter.com/1.1/lists/create.json?name=#{screen_name}&mode=private&description=List%20generated%20by%20Otherside%20for%20Twitter%20https%3A%2F%2Fotherside.site")
list = JSON.parse(request.body, {:symbolize_names => true})
end
list_id = list[:id_str]
uri = list[:uri]
raise "Failed to create list" if list_id.nil?
puts "Created list: https://twitter.com#{uri}"
# Add our target to the list so we can see their replies
request = access_token.request(:post, "https://api.twitter.com/1.1/lists/members/create.json?list_id=#{list_id}&screen_name=#{screen_name}")
raise "Could not modify list" if request.code != "200"
puts "Adding #{total} users..."
# Iterate over the list of accounts
complete = 0
ids.each_slice(100) do |slice|
complete += slice.length
user_list = slice.join(",")
access_token.request(:post, "https://api.twitter.com/1.1/lists/members/create_all.json?list_id=#{list_id}&user_id=#{user_list}")
end
puts "Done."
# Open a browser window of the list when complete
open("https://twitter.com#{uri}")
rescue Exception => ex
puts "Error: #{ex.message}"
end
end
get_friends(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment