Skip to content

Instantly share code, notes, and snippets.

@Ricky-Wilson
Created July 7, 2017 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ricky-Wilson/68056e28682575235cb5fdf6c6ecc95a to your computer and use it in GitHub Desktop.
Save Ricky-Wilson/68056e28682575235cb5fdf6c6ecc95a to your computer and use it in GitHub Desktop.
# -*- 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment