Skip to content

Instantly share code, notes, and snippets.

@karlstolley
Last active January 8, 2018 22:39
Show Gist options
  • Save karlstolley/5698592 to your computer and use it in GitHub Desktop.
Save karlstolley/5698592 to your computer and use it in GitHub Desktop.
Quick Twitter username/id scraper for Bill. Writes username,id to a CSV file. Ugly, but workable.

Should be pretty easy to set up:

  1. Install the twitter gem with gem install twitter (you may have to do sudo gem install twitter).
  2. Look at the comments in sn-to-id.rb for where you'll go to set up an app with Twitter (the URLs they ask for don't matter, in this case). Put in all of the keys, tokens, and secrets.
  3. On line 23, you'll paste in your list of usernames. You are limited to 100 usernames for each time you run the script. And I think you're limited to running the script 60 times an hour. Check the docs on the latter point tho. This is fairly permissive; as long as you have usernames separated by commas, you'll be golden (it doesn't matter if there's a space or not after the commas).
  4. Create a text file (like above) called my_users.csv in the same directory as the sn-to-id.rb file; put username,id as its first line. Every time you run the sn-to-id.rb script, your new batch of usernames/ids will be appended to the file. NOTE: the script is not smart enough to filter out duplicate usernames, so you'll get duplicate entries if a username is entered more than once.
  5. You might need to run chmod 744 sn-to-id.rb on your command line to make sure its executable.
  6. Once you've done all that, on the command line, just run ./sn-to-id.rb in the directory where it (and the my_users.csv) lives. After it's run, open up my_users.csv in your editor of choice, and you should see a list that looks something like:
username,id
billwolff,2493291
LizaPotts,4406381
karlstolley,16049663
#! /usr/bin/env ruby
require 'rubygems'
require 'csv'
require 'twitter'
Twitter.configure do |config|
# Create a new app at https://dev.twitter.com/apps/new
# Put the consumer key & consumer secret keys here,
# Inside of single quotes
config.consumer_key = 'KEY' # E.g. 'asdfsdf'
config.consumer_secret = 'SECRET' # E.g., 'jkljkljkl'
# At the bottom of your new app page on dev.twitter.com, click the
# 'Create my access token' button; you may need to reload
# the page after a minute to see this information listed.
# Again, put the information inside of single quotes:
config.oauth_token = 'TOKEN'
config.oauth_token_secret = 'SECRET'
end
# Paste in your list of usernames here, between the quotes;
# you're limited to 100 names per request.
def my_usernames
"karlstolley,LizaPotts,billwolff"
end
def my_userinfo(names)
Twitter.users(names)
end
def my_userhash(users)
userhash = {}
users.each do |user|
userhash[user.user_name] = user.id.to_s
end
return userhash
end
def my_users
my_userhash(my_userinfo(my_usernames))
end
def my_csv(my_users)
CSV.open('./my_users.csv','a+') do |csv|
my_users.each do |k,v|
csv << [k,v]
end
end
end
my_csv(my_users)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment