Python is just a lovely email@example.com (Gareth) Published: March 21, 2009
I've used quite a few dynamic scripting languages over the last couple of years including groovy, ruby and python, but I keep coming back to python. I think this time it's due to Peter Butler's (a guy I worked with a while ago) complete love of the language and I think I'm starting to see why.
Over the last week I've been bashing away working on improving the rather outdated www.logicalpractice.com and it occurred to me that it would be a bad idea to generate a sitemap xml for google and the other search bots.
The following code is my solution, I'm sure it's not the best python in the world but I do just kinda like the look.
from __future__ import with_statement
from datetime import datetime
from xml.dom.minidom import parse as parseDom
from xml.dom.minidom import Node
def url_element(xml, loc,lastmod,changefreq="weekly", priority=0.5):
xml.loc("http://www.logicalpractice.com%s" % loc)
last_mod = os.path.getmtime(os.path.join(basedir,file_name))
basedir = os.path.join(os.path.dirname(sys.argv), "..","..")
xml = xmlbuilder.builder(version="1.0",encoding="utf-8")
url_element(xml,"/news.jsp", lastmod("news.jsp"), priority=0.8)
url_element(xml,"/projects.jsp", lastmod("projects.jsp"), priority=0.5)
url_element(xml,"/profile.jsp", lastmod("profile.jsp"), priority=0.5)
# generate elements from the news.rss
rss = parseDom(os.path.join(basedir,"news.rss"))
for node in rss.getElementsByTagName("item"):
link = node.getElementsByTagName("link").firstChild.data
strdate = node.getElementsByTagName("pubDate").firstChild.data
date = datetime.strptime(strdate, "%a, %d %b %Y %H:%M:%S +0000")
url_element(xml, link, date, priority=0.5)
the xmlbuilder used is from Jonas Galvez via github seems a very simple and elegant solution for building xml documents
How do I know that python must be a good thing? Well anything that I get up at 5 in the morning to code a bit more of before work has to be a good thing.