#!/usr/local/bin/python "CGI script to print out a sorted hotlist, and update the statistics." import pg, cgi, sys, os, string, traceback, StringIO db = pg.connect('hotlist', user='mwm', host='sql') # Data to format the results list head_format = """Content-type: text/html Mike's sorted hotlist - %s """ type_names = ('count', 'followed') prelude_format = """

Mike's sorted hotlist

Sort the other way

The Web for pages with of

Amazon.com for with

    """ # printed with % dictionary with type & script_name. item_format = '
  1. %(description)s ' \ '(%(count)s, %(followed)s)
  2. ' postlude_format = """

You can also check the status of the links on the hot list. Note that this could require waiting for a link to time out, which will take a while.

""" error_format = """Content-type: text/html Server error

Server error

Please report the following error to the site admin

%s
""" class handler: "Object to handle a CGI query" def __init__(my): "Get and save the form data." my.form = cgi.FieldStorage() my.vals = {'script_name': os.environ['SCRIPT_NAME']} my.db = db my.type_names = ('count', 'followed') my.header = head_format my.prelude = prelude_format my.postlude = postlude_format my.item = item_format my.error = error_format def get_list(my, type): "Returns the list of items, sorted properly" my.db.query("set datestyle = 'iso'") query = my.db.query('select id, description, count, ' 'substr(followed, 1, 10) as followed ' 'from hotlist order by %s desc' % my.type_names[type]) return query.dictresult() def display_item(my, link): "Return the string for an item in the list" dict = {} dict.update(my.vals) dict.update(link) return my.item % dict def display_list(my, list): "Returns the HTML text for the list." return string.join(map(my.display_item, list)) def display_page(my, type): "Returns the html page for a list orderd by type specifie in my query" my.vals['type'] = 1 - type return (my.header % my.type_names[type]) + (my.prelude % my.vals) + \ my.display_list(my.get_list(type)) + my.postlude def get_type(my): "Returns the integer for my type." if my.form.has_key('TYPE'): return string.atoi(my.form['TYPE'].value) else: return 0 def run(my): "Run the query, returning the result as a string." try: path = os.environ.get('PATH_INFO') if path: while path[0] == '/': path = path[1:] (command, arg) = string.split(path, '/', 1) return getattr(my, 'do_' + string.lower(command))(arg) else: return my.display_page(my.get_type()) except: trace = StringIO.StringIO() traceback.print_exc(file = trace) return my.error % trace.getvalue() # do_XXX is used to run commands that come back to handler via path_info, # as in ....py/XXX/arguments def do_goto(my, id): "Return the string to redirect to an id." my.db.query('begin transaction') query = my.db.query("select url from hotlist where id = %s" % id) my.db.query("update hotlist set followed = 'now', count = count + 1 " "where id = %s" % id) my.db.query('end transaction') return "Location: %s\n\n" % query.getresult()[0][0] if __name__ == '__main__': print handler().run()