Skip to content

Instantly share code, notes, and snippets.

@sbp
Created May 24, 2011 15:37
Show Gist options
  • Save sbp/988931 to your computer and use it in GitHub Desktop.
Save sbp/988931 to your computer and use it in GitHub Desktop.
Atom feed script
#!/usr/bin/env python
'Atom Script, by Sean B. Palmer'
import os, re, cgi
r_title = re.compile('(?s)<title>(.*?), by (.*?)</title>')
r_entry = re.compile('(?s)<div([^>]+)>(.*?)</div>')
r_attrs = re.compile('(?s)([a-z-]+)="([^"]+)"')
with open('index.html') as f: bytes = f.read()
title, author = r_title.search(bytes).groups()
site = os.environ['SERVER_NAME'] + os.path.dirname(os.environ['SCRIPT_NAME'])
print 'Content-Type: application/atom+xml; charset=utf-8\n'
print '<feed xmlns="http://www.w3.org/2005/Atom">'
print ' <title>%s</title>' % title
print ' <link href="http://%s/"/>' % site
print ' <id>tag:sbp.so,2011:%s</id>' % site
print ' <author><name>%s</name></author>' % author
entries = [e for e in r_entry.findall(bytes) if 'datetime=' in e[0]]
for i, (attrs, content) in enumerate(entries[:20]):
attr = dict(r_attrs.findall(attrs)).get
if not i: print ' <updated>%s</updated>' % attr('datetime')
print ' <entry xml:base="http://%s/">' % site
print ' <id>tag:sbp.so,2011:%s:%s</id>' % (site, attr('datetime'))
print ' <title>%s</title>' % attr('title', attr('datetime')[:10])
print ' <updated>%s</updated>' % attr('datetime')
print ' <content type="html">%s</content>' % cgi.escape(content)
print ' </entry>'
print '</feed>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment