I wrote this script that downloads an RSS feed and converts it to HTML. One of my only concerns is my variable naming because I suck at naming things.
# -*- coding: utf-8 -*-
"""Simple RSS to HTML converter."""
__version__ = "0.0.2"
__author__ = "Ricky L Wilson"
from bs4 import BeautifulSoup
from feedparser import parse as parse_feed
TEMPLATE = u"""
<h2 class='title'>{title}</h2>
<a class='link' href='{link}'>{title}</a>
<span class='description'>{summary}</span>
"""
def entry_to_html(**kwargs):
"""Formats feedparser entry."""
return TEMPLATE.format(**kwargs).encode('utf-8')
def convert_feed(url):
"""Main loop."""
html_fragments = [entry_to_html(**entry) for entry in parse_feed(url).entries]
return BeautifulSoup("\n".join(html_fragments), 'lxml').prettify()
def save_file(url, filename):
"""Saves data to disc."""
with open(filename, 'w') as file_object:
file_object.write(convert_feed(url).encode('utf-8'))
if __name__ == '__main__':
save_file('http://stackoverflow.com/feeds', 'index.html')
with open('index.html') as fobj:
print fobj.read()