#!/usr/bin/env python
# http://inamidst.com/whits/src/
import cgitb; cgitb.enable()
import sys, re, glob, time, datetime
r_tag = re.compile(r'<[^>]+>')
now = datetime.datetime.utcnow()
delta = datetime.timedelta(seconds=(3600 * 3))
format = open('src/format').read().rstrip('\r\n')
def process(month, post):
f = open(month)
for line in f:
if line.startswith('
'):
heading = r_tag.sub('', f.next().rstrip('\r\n'))
date = datetime.datetime(*(time.strptime(heading, format)[0:6]))
if (now - date) > delta: post.append(date)
elif line.startswith('
') and post:
yield post
post = []
elif post: post.append(line)
f.close()
def posts():
for month in (fn for fn in reversed(sorted(glob.glob('*/*.html')))):
for post in reversed(list(process(month, []))):
yield post
def main():
print 'Content-Type: application/atom+xml\n'
sys.stdout.write(open('src/head.atom').read())
template = open('src/post.atom').read()
for post in __import__('itertools').islice(posts(), 20):
date, lines = post[0], post[1:]
ivars = {'updated': date.strftime('%Y-%m-%dT%H:%M:%SZ'),
'date': date.strftime(format).rstrip(':.'),
'post': date.strftime('%Y/%m#N%d%H%M'),
'content': ''.join(lines).replace(']]>', ']]>]]>'