Follow Twitter Influencers using Trstrank

Trstrank is an application that helps Twitter users filter their followers based on activity characteristics. Social media applications could integrate a similar system to help users filter their new followers.

Scenario

The @Infochimps Twitter account just received a large number of Twitter followers. The company wants to analyze these new followers and

  • Follow back enthusiastic Twitter users.
  • Directly engage with the most influential new followers.
  • Find Twitter users similar to these new followers.

APIs used

Download the code

follow-twitter-influencers.zip

App Overview

  1. Infochimps Influence Metrics API request -- Returns influencer metrics including enthusiasm
  2. Infochimps Trstrank API request -- Returns trstrank score
  3. Infochimps StrongLinks API request -- Returns a weighted list of Twitter user_ids

Gemfile

Our app uses the json and twitter gems.

/Gemfile

  source 'http://rubygems.org'

gem 'rails', '3.1.0'
gem 'sqlite3'

gem 'json'
gem 'twitter'

1. Infochimps Influence Metrics API request

Our example Rails app has one controller and a single view for displaying results. The trstrank controller has three methods that each use specific Infochimps APIs: influencemetrics, trstrank, and stronglinks.

For @Infochimps to follow back enthusiastic Twitter users, we will use the Infochimps Influence Metrics API.

The first method influencemetrics retrieves the influence metric enthusiasm for each username and places the results in the @table array. Enthusiasm is a measure of the positive nature of a Twitter user and is calculated by retweets_out / tweets_out. After retrieving results for each user, we sort by highest enthusiasm and present the results in a table view. We also output a declarative sentence summarizing the results.

/app/controllers/trstrank_controller.rb

  class TrstrankController < ApplicationController

  # Your Infochimps API Key
  API_KEY = 'XXXXXXXXXXXXXXXXXXXXX'  

  # A hash of Twitter usernames that recently followed our account
  USERNAMES = %w{aseever dhruvbansal HustonHoburg JimEngland josephkelly misswinnie mrflip nickducoff sinned TimGasper}

  # Use the Infochimps Influence Metrics API to get influence metrics
  # localhost/trstrank/influencemetrics
  def influencemetrics  
    result = {}
    @table = []

    USERNAMES.each do |u|        
      query = 'http://api.infochimps.com/social/network/tw/influence/metrics?apikey=' + API_KEY + '&screen_name=' + u    
      buffer = open(query).read
      result[u] = JSON.parse(buffer)
      @table << [u.to_s, result[u]['sway']]          
    end

    #Sort the table by sway
    @table.sort!{ |x,y| y[1]<=>x[1] }

    @sentence = "The @Infochimps Twitter account should follow back " + @table[0...3].collect{|row| '@'+row.first }.to_sentence + "."

    render 'trstrank/table'
  end

For the code above to work, you will have to replace the API_KEY variable with your Infochimps API key. The results are stored in the @table variable and displayed in a simple table view.

/app/views/trstrank/table.html.erb

  <table>
  <% @table.each do |u| %>
    <tr>
      <td><%= u[0] %></td>
      <td><%= u[1] %></td>
    </tr>
  <% end %>
</table>

<br/><br/>

<p><%= @sentence %></p>

2. Infochimps Trstrank API request

For @Infochimps to find the most influential users, we will use the Infochimps Trstrank API.

The trstrank method is similar in structure to the influencemetrics method. We iterate through each username, only this time, we collect the trstrank score of the user, sorting the table by highest trstrank. Trstrank measures Twitter user reputation, importance and influence and is a sophisticated measure of a user’s relative importance among the entire Twitter network.

/app/controllers/trstrank_controller.rb

  # Use the Infochimps Trstrank API to get trstrank score
# localhost/trstrank/trstrank
def trstrank
  result = {}
  @table = []

  USERNAMES.each do |u|
    query = 'http://api.infochimps.com/social/network/tw/influence/trstrank?apikey=' + API_KEY + '&screen_name=' + u
    buffer = open(query).read
    result[u] = JSON.parse(buffer)
    @table << [u.to_s, result[u]['trstrank']]        
  end  

  #Sort the table by trstrank
  @table.sort!{ |x,y| y[1]<=>x[1] }

  @sentence = "The @Infochimps Twitter account should @reply and thank " + @table[0...3].collect{|row| '@'+row.first }.to_sentence + "."

  render 'trstrank/table'  
end

3. Infochimps StrongLinks API request

Our final method uses the Infochimps StrongLinks API to find the connection strength between Twitter users. The strong_links call returns a list of up to 1,000 Twitter user IDs sorted by connection strength. We will use these strong connections to recommend users for @Infochimps to follow.

While the stronglinks method returns up to 1,000 connections, we are only going to use the top three strongest connections for each user. From there, we will query the Twitter API to see if the strong connection is following @Infochimps on Twitter.

Note: @Infochimps' Twitter user_id is 15748351; you can find this information by viewing an XML response from the Twitter API.

For example, running this method on @aseever will return @anthonyrstevens, a close connection that does not follow @Infochimps on Twitter.

/app/controllers/trstrank_controller.rb

  # Use the Infochimps StrongLinks API to get strong connections
# localhost/trstrank/stronglinks
def stronglinks
  result = {}
  @table = []

  USERNAMES.each do |u|  
    query = 'http://api.infochimps.com/social/network/tw/graph/strong_links?apikey=' + API_KEY + '&screen_name=' + u    
    buffer = open(query).read
    result[u] = JSON.parse(buffer)

    result[u]['strong_links'][0...3].each do |link|        
      begin
        name = Twitter.user(link[0])['screen_name'] unless Twitter.friendship?(link[0], 15748351) 
        rescue Twitter::Error   
        ensure
        @table << [u, name] if name   
      end                
    end

  end

  @sentence = "The @Infochimps Twitter account should follow " + @table.collect{|row| '@'+row.second }.uniq.to_sentence + "."

  render 'trstrank/table'  
end

The results are again displayed in the table with the user on the left and the strong connection on the right.