Created
June 12, 2019 15:48
-
-
Save aphyr/65c5ef5ef264c335b4bff4e92449fd9e to your computer and use it in GitHub Desktop.
A small script to list which of the people you follow are visible in public twitter autocomplete searches.
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 | |
# This script tells you which of your friends (people you follow) are | |
# auto-completable in public twitter user searches, e.g. by typing | |
# @username" into the search box. It takes four arguments for twitter | |
# API credentials: consumer_key, consumer_secret, access_token, and | |
# access_token_secret. You can create an twitter API app at | |
# https://developer.twitter.com/. | |
# Output is a tab-separated list of accounts, one per line, where the | |
# fields are: visible-in-search, screen name, display name, description. | |
# | |
# For instance: | |
# | |
# false mancsbondage That guy in Manchester who does stuff with rope... | |
# true anarcho_slut 🌹june amelia rose🌹 anarchist leatherdyke fiction | |
require 'net/http' | |
require 'json' | |
require 'pp' | |
require 'twitter' | |
def findable_user? user | |
acct = user.screen_name | |
json = Net::HTTP.get(URI("https://twitter.com/i/search/typeahead.json?count=10&filters=false&q=%40#{acct}&result_type=topics%2Cusers&src=SEARCH_BOX")) | |
x = JSON.parse(json) | |
if x["users"].map { |u| u["screen_name"] }.find { |sn| sn == acct } | |
true | |
else | |
false | |
end | |
end | |
def client | |
consumer_key, consumer_secret, access_token, access_token_secret = ARGV | |
Twitter::REST::Client.new do |config| | |
config.consumer_key = consumer_key | |
config.consumer_secret = consumer_secret | |
config.access_token = access_token | |
config.access_token_secret = access_token_secret | |
end | |
end | |
# How many users can we fetch in a single call? | |
USERS_REQ_LIMIT = 100 | |
# Convert a collection of ids to full users. | |
def user_ids_to_users(c, ids) | |
users = {} | |
ids.each_slice(USERS_REQ_LIMIT) do |chunk| | |
c.users(chunk).each do |user| | |
users[user.id] = user | |
end | |
end | |
ids.map do |id| | |
users[id] | |
end | |
end | |
def user_friends(c, user) | |
user_ids_to_users(c, c.friend_ids.to_a) | |
end | |
c = client | |
u = client.user | |
user_friends(c, u).each do |u| | |
desc = u.description.gsub(/\n/, ' ') | |
puts "#{findable_user? u}\t#{u.screen_name}\t#{u.name}\t#{desc}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment