Twitter OAuth: a Simple Ruby Script

June 3rd, 2010 § 0 comments § permalink

We all want shortcuts.
As of the end of this month, Twitter will deprecate support for http authentication (provide username and password explicitly) in API request. The alternative is called OAuth and Twitter has a comprehensive guide here. However, it appears too complex to us, and what we need is a method as similar to http auth as possible, since for researchers like us, the sole purpose of using API request is to get data via a robot account.
Here is the simplest shortcut, illustrated in Ruby.

  1. Register an app at http://dev.twitter.com/apps/new, and you’ll get a consumer key and a consumer secret. From the page “my access token”, you can find an access token and an access secret.
  2. Write the four strings into a config file, e.g. $HOME/.twitter. Below is a yaml config example.
    #!/usr/bin/ruby -w
    # require 'pp'
    require 'yaml'
    
    CToken   = "AAAAAAAAAAAAAAAAAAAAA"
    CSecret  = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
    AToken   = "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
    ASecret  = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
    
    config = {"ctoken" => CToken, "csecret"=>CSecret, "atoken"=>AToken, "asecret"=>ASecret}
    File.open("#{ENV["HOME"]}/.twitter", "w") do |f|
      f.puts config.to_yaml
    end
    
  3. Authorize your application in a script, using twitter gem.
    #!/usr/bin/ruby -w
    
    require 'pp'
    require 'yaml'
    require 'twitter'
    require 'mysql'
    
    conf = {}
    File.open("#{ENV["HOME"]}/.twitter", "r") { |f| conf = YAML.load(f) }
    
    # twitter authentication
    oauth  = Twitter::OAuth.new(conf["ctoken"], conf["csecret"])
    oauth.authorize_from_access(conf["atoken"], conf["asecret"])
    client = Twitter::Base.new(oauth)
    
    client.home_timeline.each { |tweet| pp tweet }
    exit
    

[In Data] The Patterns of ReTweet Paths

April 30th, 2010 § 2 comments § permalink

We looked at Twitter Retweet API recently and have been collecting retweets time series for a few weeks. One interesting exercise is to back up the paths by which the tweet goes from its original writer to all the subsequent retweeters, and we’ve been able to produce a few graphs.

  • Perhaps this is the most common pattern in our sample. Most of the retweets come from the immediate followers of the root (who writes the tweet) and there are a few more done by the followers of 1st layer retweeters.
  • Here the root’s followers are still the group of people who contribute most, and that’s why the root is clearly identifiable at first sight. But we do see a lot of longer paths.
  • This one is more interesting, since the “landscape” looks so “even” as opposed to the previous two. The root is no longer instantly identifiable. (Actually it’s at middle left.) Also the average path length from root to leaf is longer.
  • The fourth might be another extreme. The “landscape” is still uneven, but in this case the peak is not root. The root(at lower left)’s immediate followers only contribute 7 retweets. The tweet travels 5 steps to arrive at a user whose followers’ retweeting champions all other sources.

retweet
Generally, during the period of retweeting, the root’s number of followers increases. Here is a graph showing the relation b/w number of retweets and the increase of followers.
retweet_follower_inc_blog
Of course these are a few facts, but not conclusions by any sense. Economic analysis is on the way.

Update: you can find a pdf file of nearly 900 retweet graphs on the Project page.