2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
search.py - Phenny Web Search Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import web
|
|
|
|
|
|
|
|
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
|
|
|
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
|
|
|
env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
|
|
|
|
|
|
|
|
def json(text):
|
|
|
|
"""Evaluate JSON text safely (we hope)."""
|
|
|
|
if r_json.match(r_string.sub('', text)):
|
|
|
|
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
|
|
|
return eval(text.strip(' \t\r\n'), env, {})
|
|
|
|
raise ValueError('Input must be serialised JSON.')
|
|
|
|
|
|
|
|
def search(query, n=1):
|
|
|
|
"""Search using SearchMash, return its JSON."""
|
|
|
|
q = web.urllib.quote(query.encode('utf-8'))
|
|
|
|
uri = 'http://www.searchmash.com/results/' + q + '?n=' + str(n)
|
|
|
|
bytes = web.get(uri)
|
|
|
|
return json(bytes)
|
|
|
|
|
|
|
|
def result(query):
|
|
|
|
results = search(query)
|
2008-02-23 07:16:43 -05:00
|
|
|
if results['results']:
|
|
|
|
return results['results'][0]['url']
|
|
|
|
return None
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
def count(query):
|
|
|
|
results = search(query)
|
2008-02-23 07:16:43 -05:00
|
|
|
if not results['results']:
|
|
|
|
return '0'
|
2008-02-21 07:06:33 -05:00
|
|
|
return results['estimatedCount']
|
|
|
|
|
|
|
|
def formatnumber(n):
|
|
|
|
"""Format a number with beautiful commas."""
|
|
|
|
parts = list(str(n))
|
|
|
|
for i in range((len(parts) - 3), 0, -3):
|
|
|
|
parts.insert(i, ',')
|
|
|
|
return ''.join(parts)
|
|
|
|
|
|
|
|
def g(phenny, input):
|
2008-02-23 07:16:43 -05:00
|
|
|
query = input.group(2)
|
|
|
|
uri = result(query)
|
|
|
|
if uri:
|
|
|
|
phenny.reply(uri)
|
|
|
|
else: phenny.reply("No results found for '%s'." % query)
|
2008-02-21 07:06:33 -05:00
|
|
|
g.commands = ['g']
|
|
|
|
g.priority = 'high'
|
|
|
|
|
|
|
|
def gc(phenny, input):
|
|
|
|
query = input.group(2)
|
|
|
|
num = count(query)
|
|
|
|
phenny.say(query + ': ' + num)
|
|
|
|
gc.commands = ['gc']
|
|
|
|
gc.priority = 'high'
|
|
|
|
|
|
|
|
r_query = re.compile(
|
|
|
|
r'\+?"[^"\\]*(?:\\.[^"\\]*)*"|\[[^]\\]*(?:\\.[^]\\]*)*\]|\S+'
|
|
|
|
)
|
|
|
|
|
2008-02-23 07:16:43 -05:00
|
|
|
def gcs(phenny, input):
|
2008-02-21 07:06:33 -05:00
|
|
|
queries = r_query.findall(input.group(2))
|
|
|
|
if len(queries) > 6:
|
|
|
|
return phenny.reply('Sorry, can only compare up to six things.')
|
|
|
|
|
|
|
|
results = []
|
|
|
|
for i, query in enumerate(queries):
|
|
|
|
query = query.strip('[]')
|
|
|
|
n = int((count(query) or '0').replace(',', ''))
|
|
|
|
results.append((n, query))
|
|
|
|
if i >= 2: __import__('time').sleep(0.25)
|
|
|
|
if i >= 4: __import__('time').sleep(0.25)
|
|
|
|
|
|
|
|
results = [(term, n) for (n, term) in reversed(sorted(results))]
|
|
|
|
reply = ', '.join('%s (%s)' % (t, formatnumber(n)) for (t, n) in results)
|
|
|
|
phenny.say(reply)
|
2008-02-23 07:16:43 -05:00
|
|
|
gcs.commands = ['gcs']
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|