rendered paste body#!/usr/bin/python
import cgi
import urllib2, urllib
import re
from xml.dom import minidom, Node
print "Content-type: text/html\n"
print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
print "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">"
print "<head>"
print " <title>CMPT 470 Assignment 2</title>"
print "</head>"
print "<body>"
print "<div id=\"container\">\n"
content = ""
#Parsers
get_url = re.compile("<link href=\"(?P<url>.*?)\"")
get_type = re.compile("<content type=\"(?P<type>.*?)\"")
get_content = re.compile("<content.*?>(?P<content>.*?)</content>")
#Decoder: from http://snippets.dzone.com/posts/show/4569
def substitute_entity(match):
ent = match.group(2)
if match.group(1) == "#":
return unichr(int(ent))
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp)
else:
return match.group()
def decode_htmlentities(string):
entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
return entity_re.subn(substitute_entity, string)[0]
#End http://snippets.dzone.com/posts/show/4569
# Get url entered by user from form
form = cgi.FieldStorage()
url = form["url"].value
#url = urllib2.quote_plus(url)
# Open url and read the feed
formdata = urllib2.urlopen(url)
blog = minidom.parse(formdata)
formdata.close
rootNode = blog.documentElement
#print rootNode.toxml()
for node in rootNode.childNodes:
if (node.nodeName == "title"):
for title_node in node.childNodes:
if (title_node.nodeType == node.TEXT_NODE):
print " <h2 id=\"feed-title\">", title_node.nodeValue, "</h2>"
print " <hr class=\"seperator\" />"
if (node.nodeName == "entry"):
for entry_node in node.childNodes:
if (entry_node.nodeName == "title"):
for name_node in entry_node.childNodes:
if (name_node.nodeType == node.TEXT_NODE):
entry_title = name_node.nodeValue
elif (entry_node.nodeName == "link"):
match = get_url.search(entry_node.toxml())
print " <a href=\"", match.groupdict()['url'], "\">", entry_title, "</a> <br />"
elif (entry_node.nodeName == "author"):
for author_node in entry_node.childNodes:
if (author_node.nodeName == "name"):
for authname_node in author_node.childNodes:
if (authname_node.nodeType == node.TEXT_NODE):
print " <p class=\"author\"> Author:", authname_node.nodeValue, "</p> <br />"
elif (entry_node.nodeName == "content"):
match = get_type.search(entry_node.toxml())
content_type = match.groupdict()['type']
match = get_content.search(entry_node.toxml())
content = match.groupdict()['content']
if (content_type == "html"):
html_content = decode_htmlentities(content.encode("ascii"))
print " <p class=\"content\">", html_content, "</p><br />"
elif (content_type == "xhtml"):
print " <p class=\"content\">", content.encode("ascii"), "</p><br />"
else:
print " <p class=\"content\">", content.encode("ascii"), "</p><br />"
elif (entry_node.nodeName == "summary"):
if not len(content):
print "<p>get summary</p>"
print "\n</div>"
print "</body>"
print "</html>"