#!/usr/bin/env python import pyds_to_atom import metakit import html_cleaner enc = pyds_to_atom default_db = '/home/www-pycs/pycs/var/lib/pycs/data/settings.dat' def load_url_map(url_map): f = open(url_map) mapping = {} for line in f: line = line.strip() if not line: continue id, url, tag = line.split(' ', 2) mapping[id] = (url, tag) return mapping def read_entries(db_filename, base_url, url_map, limit=0, **kw): limit = int(limit) mapping = load_url_map(url_map) db = metakit.storage(db_filename, 1) comment_articles = db.view('comments') entries = [] total = 0 for comment in comment_articles: post_para_id = comment.paragraph for i, note in enumerate(comment.notes): total += 1 if limit and total > limit: return entries author_name = note.name author_email = note.email author_url = note.url date_string = note.date.replace(' ', 'T') if pyds_to_atom.timezone: date_string += pyds_to_atom.timezone id_date = date_string.split('T')[0] comment_text = note.comment comment_html = html_cleaner.cleanHtml(comment_text) post_url = mapping[comment.paragraph][0] post_tag = mapping[comment.paragraph][1] comment_link_url = post_url + 'C%s' % i entries.append(pyds_to_atom.make_entry( id=pyds_to_atom.make_id(comment_link_url, id_date), parent_id=post_tag, title='Comment', url=None, issued=date_string, modified_date=date_string, content=comment_html, content_type='text/html', author_name=author_name, author_email=author_email, author_url=author_url)) return entries help = """\ Usage: python pycs_to_atom ATTRIBUTES > atom.xml ATTRIBUTES are key=value arguments. The keys (* means required): title: The title of the feed (default 'Comments'). url*: The URL of the *site*. db_filename: The filename of the settings.dat PyCS database. url_map*: The filename of the url_map from running pyds_to_atom.py url_map=true """ if __name__ == '__main__': required = '''url'''.split() data = pyds_to_atom.parse_args( {'db_filename': default_db, 'title': 'Comments', 'url': '', 'author_name': '', 'author_email': '', 'tagline': ''}, required, help) if data.get('timezone'): pyds_to_atom.timezone = data['timezone'] entries = read_entries(**data) print pyds_to_atom.make_feed(entries, **data)