Using Python to update your FeedBurner stats

Ever since I’ve moved to my own server for my websites, I’ve wanted to reduce the number of HTTP requests per user as much as possible. Here is how I (and you) can use Python to shave 1 more request off that number.

I can do this (and remove a DNS lookup) by updating my Feedburner count using an automated script on my server instead of having each client request it.

Using the FeedBurner Awareness API

Most of the time you only care about getting your total subscribers at the moment. The FeedBurner Awareness API is far more capable than just doing that, but we’re going to keep it simple today.

For the simple case you just need your feed ID or URI. Try:

curl -s 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YOUR_FEED_NAME'

You receive an XML response like:

<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
  <!--This information is part of the FeedBurner Awareness API. If you want to hide this information, you may do so via your FeedBurner Account.-->
  <feed id="foo0nbta7tscktjrgddc95gg3s" uri="EricWendelin">
    <entry date="2009-11-03" circulation="1181" hits="3901" reach="21" />
  </feed>
</rsp>

Now we just need to parse out the “circulation” which is your subscriber count.

Quick and dirty bash script

This is what I used to use until FeedBurner started returning 0s or blanks in the XML returned:

#!/bin/bash
FEED_COUNT=`curl -s https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=EricWendelin | egrep -o circulation=\"[0-9]+\" | egrep -o [0-9]+`

sed -r -i "s/(\"numsubscribers\">)[^<]+</\1$FEED_COUNT</g" /my/path/to/sidebar.php

echo $FEED_COUNT

You can use cron to replace some HTML, log the count, etc. every so often.

The Python

The Python version is much lengthier but has error checking and does not have to do file-replacement. The following is also available on GitHub for your extending pleasure ;).

#!/usr/bin/env python

# Usage: ./check_feedburner.py MyFeedName

import re, sys, urllib, fileinput
from xml.dom import minidom

API_URL = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=%s'
# To replace the feed count in a file, put it here
FEED_COUNT_FILE = '/path/to/your/file.php'
# HTML to replace with feed count - REPLACE ME
HTML_REGEX = r'("numsubscribers">)[^<]+<'
REPLACE_STRING = r'"numsubscribers">%s<'

def get_circulation(feed_uri):
    dom = minidom.parse(urllib.urlopen(API_URL % feed_uri))
    entry = dom.getElementsByTagName('entry')[0]
    count = entry.getAttribute('circulation')
    if count == '' or count == '0':
     print 'Error getting feed count'
     sys.exit(1)
    return count

def replace_feedburner_count(count):
    '''Replaces feedburner count in FEED_COUNT_FILE'''
    try:
        for line in fileinput.input(FEED_COUNT_FILE, inplace=1):
     # Comma at the end prevents explicitly writing a newline
            print re.sub(HTML_REGEX, REPLACE_STRING % count, line, 1),
    except OSError, ose:
     print 'File "%s" not found' % FEED_COUNT_FILE

if __name__ == '__main__':
    # First arg is URI
    feed_count = get_circulation(sys.argv[1])
    print feed_count
    if FEED_COUNT_FILE:
        replace_feedburner_count(feed_count)

Something like this would be especially cool if you could set thresholds or use optparse for advanced options. Useful? Hope so!

If you liked this post, please help me share it

Response (1)

  1. [...] Before run this script, Feedburner awareness API must be activated. This is script is inspired by Eric Wendelin. Usage: python [...]

Leave a Reply