Analyzing blog.py
What?
This is a script to generate RSS for me. There are some other alternative systems like Luke smith's LB script that generate RSS for you and it's really cool. I have wanted for a long time to have something like this. So I created my own script in python. This is how it works.
Time
In a previous version of this script I had to manuely add Time This was annoying since I have trouble with dates. With this time module it will generate the current time for me.
# I need to get time instead of asking manuelly for it. from time import gmtime, strftime
Inputs
Here I get two peices of data from the user the title
and Link
of the blog entry. Then I get the date using something I found on python time module documentation. Also I specify the rssfile to be rss.xml. Usually you will always have the rss file called rss.xml but I think it's a good idea to have it as a variable anyways. For now the htmlfile variable is commented out because I can't figure out how to properly clean up html files and put them into the description.
# Inputs Title = input("Title: ") Link = input("Link: ") Date = str(strftime("%a, %d %b %Y %X")) #htmlfile = input("HTML file: ") rssfile = "rss.xml"
Delete channel and rss tags
The </channel>
and </rss>
tags placed at the bottom get in the way of inserting entries also at the bottom of the rss file. So I delete them. I also have some code I previously I had for the html file clean up for putting in the description.
# cdata ~> Description ~> Reads in rss reader I hope :-) # I need to figure out how to get data from <p> in html file # htmlfile = str("<![CDATA[ "+open(htmlfile).read().replace('\n', ' ')+" ]]>") # Delete channel and rss tags with open(rssfile) as r: text = r.read().replace("\n</channel>\n</rss>", " ") with open(rssfile, "w") as w: w.write(text)
Input all the information for the entry.
This is the most important part of the script. Using all the gathered variables I open the rss file and start appending text to it with f.write. For now the description isn't really awesome with the whole article easily accessible.
# Add stuff with open(rssfile, 'a') as f: f.write("\n<item>") f.write("\n<title>"+Title+"</title>") f.write("\n<link>"+Link+"</link>") f.write("\n<guid>"+Link+"</guid>") f.write("\n<pubDate>"+Date+"</pubDate>") f.write("\n<description>") f.write("\n<![CDATA[ You can consume this content in any app you like! ]]>") f.write("\n</description>") f.write("\n</item>") f.write("\n") f.write("\n</channel>") f.write("\n</rss>")
# f.write("\n<item>\n<title>"+Title+"</title>\n<link>"+Link+"</link>\n<guid>"+Link+"</guid>\n<pubDate>"+Date+"</pubDate>\n<description>\n"+htmlfile+"\n</description>\n</item>\n\n</channel>\n</rss>")