May 09, 2019

RSS to markdown converter (poor mans planet clone)


The first thing what we need is a RSS feed, a sample file can be found at https://www.feedforall.com/sample-feeds.htm Now, we need to install a python library:
pip install feedparser
The best thing to realize a RSS to markdown converter is not using existing software but interactively testing out a self-written program. This gives more control over the process. The first demo program is to test out if the feedparser library is working:
import feedparser
url="sample.xml"
feed = feedparser.parse( url )
Now we can create a simple mini program which goes through the file and prints out the headlines:

import feedparser
url="sample.xml"
feed = feedparser.parse( url )
items = feed["items"]
for item in items:
title = item[ "title" ]
url = item[ "link" ]
print title, url

Now it is time to introduce the markdown syntax.
import feedparser
url="sample.xml"
feed = feedparser.parse( url )
items = feed["items"]
for item in items:
title = item[ "title" ]
url = item[ "link" ]
print "#",title
a="["+url+"]("
b=url+")"
print a+b+"\n"

The interesting feature of the generated markdown output is, that the links are clickable. The only thing what is missing is a comment and upvote system which is available in the hackernews website. Another option is to copy the generated markdown output into a mediawiki installation. Mediawiki has a builtin comment system already.

No comments:

Post a Comment