Created
December 26, 2012 21:59
-
-
Save sbp/4383394 to your computer and use it in GitHub Desktop.
Swhack search service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, os, time, urllib, mmap | |
print 'Content-Type: text/plain; charset=utf-8' | |
query = os.environ['QUERY_STRING'] | |
query = urllib.unquote(query) | |
now = time.time() | |
day = 24 * 60 * 60 | |
sensitive = False | |
parts = query.split(' ', 1) | |
if parts[0].startswith(':'): | |
if parts[0][1:].isdigit() and len(parts) == 2: | |
sensitive = True | |
if not sensitive: | |
query = query.lower() | |
for i in xrange(28): | |
offset = day * i | |
d = time.gmtime(now - offset) | |
name = 'logs/%04i-%02i-%02i.txt' % (d[0], d[1], d[2]) | |
try: f = open(name) | |
except IOError, OSError: continue | |
for line in reversed(list(f)): | |
if query in line.lower(): | |
if '.o swhack' in line: continue | |
if '.swhack' in line: continue | |
if '<phenny> 2' in line: continue | |
print '%04i-%02i-%02i' % (d[0], d[1], d[2]), line | |
f.close() | |
sys.exit(0) | |
f.close() | |
print 'No result in past month for "%s".' % query | |
else: | |
days = int(parts[0][1:]) | |
if days > 365: | |
print 'Can only search up to and including 365 days.' | |
sys.exit(0) | |
query = parts[1] | |
for i in xrange(days): | |
offset = day * i | |
d = time.gmtime(now - offset) | |
name = 'logs/%04i-%02i-%02i.txt' % (d[0], d[1], d[2]) | |
try: f = open(name, 'rb') | |
except (IOError, OSError), e: | |
# print e | |
continue | |
map = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) | |
pos = map.size() | |
while pos > -1: | |
pos = map.rfind(query, 0, pos) | |
if pos < 0: break | |
start = map.rfind('\n', 0, pos) | |
end = map.find('\n', pos) | |
line = map[start:end].strip() | |
pos = start | |
if '.o swhack' in line: continue | |
if '.swhack' in line: continue | |
if '<phenny> 2' in line: continue | |
print '%04i-%02i-%02i' % (d[0], d[1], d[2]), line | |
f.close() | |
sys.exit(0) | |
f.close() | |
print 'No result in past %s days for "%s".' % (days, query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment