emwendelin (owner)

Revisions

gist: 226407 Download_button fork
public
Public Clone URL: git://gist.github.com/226407.git
Embed All Files: show embed
check_feedburner.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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)