23 Jun

Feed Subscribers Without Feedburner

Here’s how to show the number of feed subscribers on your blog without going through FeedBurner or any other service, all in javascript.

First, most feed fetcher bots pass the number of subscribers in the User-Agent header, you can get this in you web server log:

209.85.238.248 - - [23/Jun/2010:11:36:45 -0700] "GET /blog.atom HTTP/1.1" 304 169 "-" "Feedfetcher-Google; (+http://www.google.com/feedfetcher.html; 540 subscribers; feed-id=8309483740725605993)"

Here’s a dirty script that will parse the Apache log file and extract the number of subscribers and add them up:

# Path to your Apache log
LOG_PATH = "logs/macournoyer.com/http/access.log"

cmd = "grep subscribers #{LOG_PATH} | " +
      %q{sed -rn 's/^.*GET (\/.*) HTTP.*"-" "([A-Z].*) .*; ([0-9].*[0-9]) subscribers.*$/\2\1--\3/ p'}
      # outputs BotName/URL--<# of subscribers>

counts = Hash.new(0)

`#{cmd}`.each_line do |line|
  ip, count = line.split("--")
  counts[ip] = count.to_i if count.to_i > counts[ip]
end

puts counts.values.inject(0) { |sum, i| sum += i }

Next, create a Cron job to run this each day and save it to a file accessible online:

0 0 * * * ruby feedcount.rb > PATH_TO_YOUR_SITE_ROOT/feedcount.txt

Finally, you want to update the info on your site using jQuery:

$("#feedcount").load("/feedcount.txt");

Boom!

Like what you just read? Subscribe to my newsletter to learn how I make a living selling my products online.

Or if you're into coding, join my free class on rebuilding a Ruby web server.

blog comments powered by Disqus