Paste number 28974: swhack.com -> logs -> index.cgi

Paste number 28974: swhack.com -> logs -> index.cgi
Pasted by: sbp
2 years, 2 months ago
#swhack
Paste contents:
Raw Source | XML | Display As
#!/usr/bin/env python
# Log Maker CGI
# @@ fix multiple timestamp ID problem

import cgitb; cgitb.enable()
import sys, os, re, cgi, glob, datetime

r_uri = re.compile(
   r'((ftp|https?)://([^\s<>"\'\[\]),;:.&]|&(?![gl]t;)|[\[\]),;:.](?! ))+)'
)
day = datetime.timedelta(days=1)

def days(start, finish):
   startdate = datetime.date(*[int(n) for n in start.split('-')])
   finishdate = datetime.date(*[int(n) for n in finish.split('-')])
   if startdate > finishdate:
      raise ValueError("Finish date must come after the start date.")

   currentdate = startdate
   while currentdate <= finishdate:
      yield currentdate.isoformat()
      currentdate += day

def utf8ize(s):
   nuggets = []
   for nugget in s.split(' '):
      try: nuggets.append(unicode(nugget, 'utf-8'))
      except:
         try: nuggets.append(unicode(nugget, 'iso-8859-1'))
         except: nuggets.append(unicode(nugget))
   return ' '.join([n.encode('utf-8') for n in nuggets])

def htmlize(line):
   line = line.rstrip('\r\n')
   timestamp, content = line.split(' ', 1)
   content = utf8ize(content)
   content = cgi.escape(content)
   content = r_uri.sub(r'<a href="\1">\1</a>', content)

   lineid = 'T' + timestamp.replace(':', '-')
   result = '<a id="' + lineid + '" href="#' + lineid + '">'
   result += timestamp + '</a>' + ' ' + content + '<br />'
   return result

def redirect(fn):
   print "Status: 307"
   print "Location: http://jibbering.com/svglogs/swhack/%s.txt" % fn
   print "Content-Type: text/html"
   print
   print "<a href='http://jibbering.com/svglogs/swhack/%s.txt'>go!</a>" % fn

def log(fn):
   # date = datetime.date(*[int(n) for n in fn.split('-')])
   # if date >= datetime.date(2005, 5, 6) and \
   #    date <= datetime.date(2005, 5, 18):
   #    redirect(fn)
   #    sys.exit()

   try: f = open(fn + '.txt')
   except IOError, e:
      print "Status: 404"
      print "Content-Type: text/html; charset=utf-8"
      print
      print str(e)
      return

   print "Content-Type: text/html; charset=utf-8"
   print
   print '<html xmlns="http://www.w3.org/1999/xhtml">'
   print '<head>'
   print '<title>%s Swhack IRC Log</title>' % fn
   print '<style type="text/css">body { font-family: sans-serif; }'
   print 'h1 a { color: #000; text-decoration: none; }</style>'
   print '<link rel="icon" href="/favicon.ico" type="image/x-icon" />'
   print '</head>'
   print '<body>'
   print '<h1>%s <a href="/">Swhack</a> IRC Log</h1>' % fn      
   for line in f:
      print htmlize(line)
   f.close()
   print '</body>'
   print '</html>'

def homepage():
   print "Content-Type: text/html; charset=utf-8"
   print
   print """<html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Swhack Logs</title>
        <link rel="stylesheet" type="text/css" href="/style.css" />
      </head>
      <body>
        <h1>Swhack Logs</h1>
     <ul>
   """
   filenames = glob.glob('*.txt')
   filenames.sort()
   filenames.reverse()
   today = datetime.date.today()
   # for day in reversed(list(days('2005-05-06', '2005-05-18'))):
   #    print ('<li><a href="http://jibbering.com/svglogs/swhack/' + day +
   #           '.txt">' + day + '</a> (at jibbering.com)</li>')
   for fn in filenames:
      date = fn[:-4] # fn sans .txt
      txt = date + '.txt'
      sys.stdout.write('<li><a href="./%s">%s</a>' % (date, date))
      if os.path.isfile(txt):
         size = os.stat(txt).st_size
         sys.stdout.write(' (%s bytes: <a href="./%s" rel="nofollow">txt</a>)' % (size, txt))
      print '</li>'
   print """</ul>
     <address>
        <a href="/">swhack.com</a>
     </address>
     </body>
     </html>
   """

def main():
   fn = os.environ.get('REQUEST_URI', '/').split('/').pop()
   if not fn: homepage()
   else: log(fn)

if __name__=="__main__":
   main()

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.