#!/usr/bin/python import cgi import urllib2 import re from xml.dom import minidom, Node print "Content-type: text/html\n" print "" print "" print "" print " CMPT 470 Assignment 2" print "" print "" print "
\n" content = "" #Parsers get_url = re.compile(".*?)\"") get_type = re.compile(".*?)\"") get_content = re.compile("(?P.*?)") # 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 "

", title_node.nodeValue, "

" print "
" 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 " ", entry_title, "
" 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 "

Author:", authname_node.nodeValue, "


" 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"): print "

", urllib2.unquote(content), "


" elif (content_type == "xhtml"): print "

", content.encode("ascii"), "


" else: print "

", content.encode("ascii"), "


" elif (entry_node.nodeName == "summary"): if not len(content): print "

get summary

" print "\n
" print "" print ""