Created
November 4, 2009 21:30
-
-
Save eriwen/226407 to your computer and use it in GitHub Desktop.
check_feedburner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Usage: ./check_feedburner.py MyFeedName | |
import re, sys, urllib, fileinput | |
from xml.dom import minidom | |
AWARENESS_API_URL = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=%s' | |
# If you want to replace the feedburner count in a given file, define it here | |
FEED_COUNT_FILE = '/path/to/your/file.php' | |
# HTML to replace with feed count | |
COUNT_HTML_REGEX = r'REPLACEME' | |
COUNT_REPLACE_STRING = r'REPLACEME%s' | |
def get_circulation(feed_uri): | |
dom = minidom.parse(urllib.urlopen(AWARENESS_API_URL % feed_uri)) | |
count = dom.getElementsByTagName('entry')[0].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 of the string prevents explicitly writing a newline | |
print re.sub(COUNT_HTML_REGEX, COUNT_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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment