public
Last active

Swhack logs script

  • Download Gist
logs.cgi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#!/usr/bin/env python
 
import cgitb
cgitb.enable()
 
import sys, os, re, glob, datetime
 
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 encode(text):
text = utf8ize(text)
text = text.replace('&', '&')
text = text.replace('<', '&lt;')
text = text.replace(' ', '&nbsp;&nbsp;')
return r_uri.sub(r'<a href="\1">\1</a>', text)
 
r_uri = re.compile(
r'((ftp|https?)://([^\s<>"\'\[\]),;:.&]|&(?![gl]t;)|[\[\]),;:.](?! ))+)'
)
 
def log(fn):
year = fn[0:4]
month = fn[5:7]
day = fn[8:10]
 
month = {
'01': 'January',
'02': 'February',
'03': 'March',
'04': 'April',
'05': 'May',
'06': 'June',
'07': 'July',
'08': 'August',
'09': 'September',
'10': 'October',
'11': 'November',
'12': 'December'
}.get(month, '?')
day = day.lstrip('0')
 
print 'Content-Type: text/html; charset=utf-8'
print
print '<!DOCTYPE html>'
print '<title>Swhack! %s %s %s</title>' % (day, month, year)
print '<meta charset="utf-8">'
# print '<link rel="stylesheet" href="logs2.css">'
print '''\
<style>
body { font: 14px/19px sans-serif; margin: 1em; max-width: 960px }
h1 { font-size: 24px }
.message .a, .action .a { color: #3636B2 }
.join .a, .join .b { color: #2A8C2A }
.quit .a, .quit .b, .part .a, .part .b { color: #64351F }
.when { color: #000; text-decoration: none }
.when:hover { text-decoration: underline }
/*
.when { padding-right: 5px }
.b a { color: #3636B2; text-decoration: none }
.b a:hover { text-decoration: underline }
*/
.b a { color: #000; text-decoration: underline }
.b a:hover { background: #ffc }
</style>
'''
print
print '<h1>Swhack! %s %s %s</h1>' % (day, month, year)
 
f = open(fn + '.txt')
for line in f:
line = line.rstrip('\n')
t, content = line.split(' ', 1)
if content.startswith('***') and ('quit' in content):
cls, (a, b) = 'quit', content[4:].split(' ', 1)
# '***', content[4:]
elif content.startswith('***') and ('part' in content):
cls, (a, b) = 'part', content[4:].split(' ', 1)
# '***', content[4:]
elif content.startswith('***') and ('join' in content):
cls, (a, b) = 'join', content[4:].split(' ', 1)
# '***', content[4:]
elif content.startswith('***'):
cls, a, b = 'event', '***', content[4:]
elif content.startswith('*'):
cls, (a, b) = 'action', content[2:].split(' ', 1)
a = '* ' + a
elif content.startswith('<'):
cls, (a, b) = 'message', content.split(' ', 1)
else: cls, a, b = 'unknown', '?', content
 
fragid = 'T' + t.replace(':', '-')
output = ['<span class="%s" id="%s">' % (cls, fragid)]
stamp = '<a href="#%s" title="%s" class="when">%s</a> '
output.append(stamp % (fragid, t, t))
output.append('<span class="a">' + encode(a) + '</span> ')
output.append('<span class="b">' + encode(b) + '</span>')
output.append('</span><br>')
print ''.join(output)
 
def formatnumber(n):
parts = list(str(n))
for i in range((len(parts) - 3), 0, -3):
parts.insert(i, ',')
return ''.join(parts)
 
def homepage():
print "Content-Type: text/html; charset=utf-8"
print
print """\
<!DOCTYPE html>
<title>Swhack Logs</title>
<style>
@import "http://fonts.googleapis.com/css?family=Lato";
body { font: 1.5em/1.5em Lato, sans-serif; margin: 3em 5em }
a { text-decoration: none }
a:hover { text-decoration: underline }
h1 { font: 2.5em/1em Lato }
h1, p { margin: 0 0 36px 0; padding: 0 }
p.nav { font-size: 1.5em }
.contribute { font-size: 16px }
table { margin-left: 1em }
.size { text-align: right; padding-left: 1em }
</style>
<script src="/jquery.js"></script>
<script>
$(function () {
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth() + 1;
var day = d.getUTCDate();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
$('a.today').attr('href', year + '-' + month + '-' + day);
});
</script>
<h1>Swhack Logs</h1>
<p class="nav"><a href="/">Home</a> &middot;
<a href="today" class="today">Today</a>
 
<table>
"""
filenames = glob.glob('*.txt')
filenames.sort()
filenames.reverse()
today = datetime.date.today()
for fn in filenames:
date = fn[:-4] # fn sans .txt
txt = date + '.txt'
sys.stdout.write('<tr><td><a href="./%s">%s</a>' % (date, date))
if os.path.isfile(txt):
size = os.stat(txt).st_size
sys.stdout.write('<td class="size">%s bytes' % formatnumber(size))
# txt
# print '</li>'
print """</table>
<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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.