Easy Statistics For Twitter Users

You can get some public information about twitter users easily without any need to use oauth or anything like that. We’ll use the grackle ruby library, but there are a bunch of libraries out there.

The basic statistics for twitter users that you can get are number of tweets, number of favorite tweets, number of followers, and how many users they are following.

Here’s the class:

{% highlight ruby %} #!/usr/bin/env ruby

require ‘rubygems’ require ‘grackle’ require ‘json’

class TwitterStats def initialize @client = Grackle::Client.new @user_info = {} end

    def uinfo(username)
            unless @user_info.has_key?(username)
                    @user_info[username] = @client.users.show.json?(:screen_name => username)
            end
            return @user_info[username]
    end

    def followers_count(username)
            uinfo(username).followers_count
    end

    def following_count(username)
            uinfo(username).friends_count
    end

    def tweet_count(username)
            uinfo(username).statuses_count
    end

    def favorite_count(username)
            uinfo(username).favourites_count
    end

end {% endhighlight %}

Here’s an example printing out all of @stat_hat’s info:

{% highlight ruby %} tstats = TwitterStats.new username = ‘stat_hat’ puts “@#{username} number of followers: #{tstats.followers_count(username)}” puts “@#{username} number following: #{tstats.following_count(username)}” puts “@#{username} number of tweets: #{tstats.tweet_count(username)}” puts “@#{username} number of favorites: #{tstats.favorite_count(username)}” {% endhighlight %}


Comments? Send us a tweet.

Permalink:

Previous:
Save Money with Temporary EC2 Deploy Server
Next:
treap - Open Source Balanced Binary Search Tree in Go