Skip to content

Instantly share code, notes, and snippets.

@sbp
Created January 27, 2011 13:40
Show Gist options
  • Select an option

  • Save sbp/798510 to your computer and use it in GitHub Desktop.

Select an option

Save sbp/798510 to your computer and use it in GitHub Desktop.
Service to search the Swhack logs
#!/usr/bin/python
import os, re, urllib, time
querystring = os.environ.get('QUERY_STRING', '')
pattern = urllib.unquote(querystring)
r_pattern = re.compile('(?i)' + pattern)
os.chdir('logs')
matches = []
a = time.time()
for name in sorted(os.listdir('.'), reverse=True)[:1000]:
if not name.endswith('.txt'): continue
with open(name) as f:
lines = f.readlines()
for line in reversed(lines):
if r_pattern.search(line):
if '.o swhack' in line: continue
if '<phenny> 2' in line: continue
text = name[:10] + ' ' + line[:5] + line[8:].rstrip()
text = text.replace('Monty', 'M-nty')
matches.append(text)
if len(matches) >= 3: break
if len(matches) >= 3: break
b = time.time()
print 'Content-Type: text/plain; charset=utf-8'
print
if matches:
print ' | '.join(matches)
else:
args = (pattern, round(b - a, 2))
print 'No results for /%s/. (Search took %s seconds)' % args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment