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] 一些中文 Twitter 用户的 Follower Similarity Index

February 18th, 2010 § 8 comments § permalink

In an ongoing research project, we consider Twitter, the social network service, as a market, in which people produce and consume information by tweeting and following others. Let’s say user A and user B are two frequent tweeters. One interesting question would be “how similar” is the information contained in A’s and B’s tweets. One way to solve this question is of course do a semantic analysis of the two’s tweets. Another way, as we propose, is look at their respective followers. Presumably everyone has a preference on what kind of information she consumes, so her following someone should tell that, to some extent, she likes the one’s tweets. Therefore we can predict that if A’s and B’s tweets are similar, then they should have followers of similar preferences; inversely if A’s and B’s tweets are quite different, then the followers they attract should have quite different preferences.
We define Followers-Similarity-Index (FSI) of A and B as

For fun, I computed the FSIs for the following Chinese twitter users @flypig, @junyu, @turingbook, @mozhixu, @glif, @williamlong, @DashHuang, @Stefsunyanzi, @virushuo, @WangShuo, @xiaolai, @zuola, @wglxh, @wangpei, @gaojiamin, @ag108lau, @arthur369, @mranti, @songshinan, @hecaitou, @duanzi, @isaac, @shizhao, @luoyonghao, @jaqi, @jason5ng32, @maoz, @izlmichael, @roseluqiu, @livid, @onlyswan, @aiww, @fzhenghu, @zhangfacai. There are 34 people and hence C_{34}^{2}=561 combinations. I used the data collected at 0:00 Feb 17 CST. Below are the 561 computed FSIs sorted.