URI.LV

After my last invoice got paid I rewarded myself with Panic’s Status Board and were thinking about, what I actually want to put on it. Since Google is sunsetting Google Reader in july, I lost my last bit of faith in Feedburner1, but since I like me some stats, I stumbled upon URI.LV as an alternative. URI seems much less intrusive as Feedburner, is easier too use, has good documentation and a super simple API. They even generate a JSON file for every feed, which you could use in Status Board for a graph, but since I’m not keen on having a panel for every feed, I took the DIY route.

First we need to prepare URI so that we can use the API. Click on ‘API’ in the navigation and then on ‘New app’ in the sidebar. Having created the app we get an API key and can generate a token, which we’ll both need.

The return of the API call will look something like that:

{
  "stats": [
    {
      "day": 1364425200,
      "greader": 742,
      "other": 13,
      "direct": 186,
      "newsletter": 21
    },
  ],
  "code": 1
}

We now write a little ruby script, which will do all the heavy lifting:

require 'net/https'
require 'uri'
require 'json'
require 'csv'
require 'titleize'

USERNAME = 'Code-Chimp'
API_KEY = '5b87661cd79e'
API_TOKEN = 'b0daafa44009'

feeds = ['code-chimp', 'celluloid', 'recordcase', 'tales', 'escaping-to-neverland']
uri = URI.parse("http://api.uri.lv/feeds/subscribers.json")

CSV.open("/Users/#{USERNAME}/Dropbox/Public/uri_statusboard.csv", "w") do |csv|
  csv << ['80%', '20%']
  feeds.each do |feed|
    params = { :key => API_KEY, :token => API_TOKEN, :feed => feed }
    uri.query = URI.encode_www_form(params)
    response = Net::HTTP.get_response(uri)
    stats = JSON.parse(response.body)['stats'].first
    subscribers = stats['greader'] + stats['other'] + stats['direct']
    csv << [feed.titleize, subscribers]
  end
end

As you can see I first created an array with the URI feed names, then I opened an CSV file in my public Dropbox folder, made an API call for every feed, added the subscribers from the various sources together and wrote them in the CSV file. Notice that you’ll have to fill in your own data for the USERNAME, API_KEY and API_TOKEN values.2

With this we can already load the data in the standard Status Board table. Just add a table to your Status Board and point it at the url of the csv file. All that is left to do is schedule the script to update the file. I just created a new job with Lingon named it org.code-chimp.uri_statusboard and told it to run /usr/bin/ruby /YOUR/PATH/uri_status_board.rb every 20 minutes.3 Of course you could also create a launchd job with in terminal.

That’s pretty much it. I personally found the table a bit big so I created a HTML file that loads the data from the CSV via JavaScript. Since I’m not that good with JavaScript I more or less piggybacked on this blog post by Gunther Groenwege to load the CSV and this example by Panic to get the auto-refresh running.

The finished product looks something like this in Status Board:

URI Status Board Panel

Feel free to use the file.

It’s kind of nice to have some new feeds in there to see the subscriber count rise right from the beginning…4

Update

@maximevalette5 told me on APN, that URI.LV’s analytics refresh every day and there even is a countdown on the feed’s dashboard.6 So letting the script run every 20 minutes would be a bit redundant. Just check when the next update will be and schedule the script to run a couple of minutes after the update every day.

  1. Not that Google has done much with lately anyway…

  2. Lines 7-9

  3. URI Premium refreshes the feed every 20 minutes. I’m not really sure how often the statistics are being updated.

  4. Or at least I hope they do.

  5. The creator of URI.LV.

  6. Yup, apperently I’m blind.