Navigate back to the homepage

Using the Twitter API to Find People

June 17th, 2013 · 3 min read


Over the last year I’ve become a huge Twitter fan. So much so that I had to move it off the front page of my phone so I wouldn’t be (as) distracted throughout the day. In addition to my personal account, I have several accounts for projects I’m working on. I use these accounts to discover and interact with people that may be interested in these apps.

To discover people that I think may be interested, I look at recent followers of similar apps. While this process worked alright, it was a very manual. Also, one of the apps I’m working on is only available in NYC, and there was no way to search for people only living in NYC and the surrounding area.

Using the Twitter API

I decided that I could use the Twitter API to help automate this process. For example, let’s pretend that I am creating starting a Ruby Motion meetup in NYC. I’d like to find people in the NYC area that are interested in Ruby Motion. Using Twitter, I can find these people a couple of different ways:

  1. Look for people that follow @RubyMotion
  2. Look for people that recently tweeted “#rubymotion”

In addition to finding people that match either of the criteria above, I also want to make sure they live in the NYC area.

Getting Started

I first looked into the best way to access the Twitter API from a ruby environment. As Twitter is a very popular app, I figured there was likely a good gem. After searching for ‘Twitter’ on www.ruby-toolbox.com, I ended up deciding to go with the ‘Twitter’ gem. After installing this gem in the terminal, I required the rubygems gem and twitter gem in my ruby file.

1require 'rubygems'
2require 'twitter'

Next I read the gem documentation on github, and followed the steps to register and authorize my app. After registering my app on the Twitter Developer site, it was just a few lines of code before I was up and running.

1Twitter.configure do |config|
2 config.consumer_key = "**********************"
3 config.consumer_secret = "*****************************************"
4 config.oauth_token = "**************************************************"
5 config.oauth_token_secret = "*****************************************"

Now I was up and running! To tweet about the new meetup was just a simple line away:

1Twitter.update("Come on out to our Ruby Motion meetup next Tuesday @ 6:30pm!")

Now for finding relevant people:

1nyc_accounts = {}
2index = 0
3cursor = -1
5# Loop through pages 1-10
6while index <15 do
8 # Iterate through followes
9 followers = Twitter.followers("RubyMotion", :cursor => -1)
11 # Save the users
12 followers.users.each do |user|
13 rubymotion_accounts[user.screen_name] = user.location if user.location.downcase =~ /(nyc|york)/i
14 end
16 cursor = followers.next_cursor
17 index+=1

The code above makes a call to the Twitter API to grab the followers for a specific Twitter account, in this case RubyMotion. The way the Twitter API works, each call returns an object that contains an array of 20 seperate hashes, which contain information about each of the followers. Because the API rate limits of the Twitter API are relatively small, I decided to loop through only 15 pages of followers.

Next, I went through each Twitter object and added the username and location of the follower if the location contained either nyc or york (using regular expressions). The result is a hash where the keys are usernames and the values are locations for, containing information of people that follow RubyMotion and have a location that contains either ‘NYC’ or ‘York’.

On to the next one:

1rubymotion_accounts_2 = {}
3Twitter.search("#rubymotion", :count => 100, :result_type => "recent").results.each do |status|
4 rubymotion_accounts_2[status.attrs[:user][:screen_name]]=status.attrs[:user][:location] if status.attrs[:user][:location].downcase =~ /(nyc|york)/i

For the second call, I a utilizing the search method, and querying the most recent 100 tweets that contain the #rubymotion tag. This call returns a Twitter object that contains an array of all the tweets. Each item of the array contains a hash with information about the user. I then saved the username and location of the tweeter if the location of that person contained ‘NYC’ or ‘York’ again.

Saving the data into a database:

1require "sqlite3"
2require 'open-uri'
4db = SQLite3::Database.new "twitter-bot.db"
6db.execute <<-SQL
8 create table twitter_handles (
10 twitter_handle TEXT,
11 city TEXT
12 );
15rubymotion_accounts.each do |handle, city|
16 db.execute("INSERT INTO twitter_handles (twitter_handle, city)
17 VALUES (?, ?)", [handle, city])
20rubymotion_accounts_2.each do |handle, city|
21 db.execute("INSERT INTO twitter_handles (twitter_handle, city)
22 VALUES (?, ?)", [handle, city])

Lastly, I wrote a schema.rb file to create a table and iterate through the 2 hashes I created above and insert the information into the new data.

Areas of Future Exploration

One problem I kept on running into was exceeding my API rate limit. For example, in the first API call, the reason I only scroll through 15 pages is because the Twitter API only allows me to make 15 calls within a 15 minute window. While finding 300 relevant people seems like a lot, when you add the location constraint it ends up not being that many people. Similarly in the 2nd API call, I only grab the last 100 tweets because that is all the API lets me grab at a time. This made it really hard to find many people that were relevant for this specific search. When I ran this search I actually ended up with no results because, presumably because of the 100 tweets grabbed, no users were located in New York.

Adam Waxman

Product Design at SeatGeek

@ajwaxman  ·  awaxman.com

You may also like

Ready. Set. Code. - Resources for Learning to Program

June 12th, 2013 · 5 min read

Quitting My Job and Learning to Program

June 3rd, 2013 · 2 min read