Created
July 7, 2017 19:36
-
-
Save Ricky-Wilson/68056e28682575235cb5fdf6c6ecc95a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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