Updated the search facilities.

master
Sean B. Palmer 2008-11-20 10:18:21 +00:00
parent fe16d21212
commit 1856781604
2 changed files with 12 additions and 13 deletions

View File

@ -24,7 +24,7 @@ def mappings(uri):
command, template = item.split(' ', 1) command, template = item.split(' ', 1)
if not template.startswith('http://'): continue if not template.startswith('http://'): continue
result[command] = template result[command] = template.replace('&', '&')
return result return result
def o(phenny, input): def o(phenny, input):

View File

@ -22,24 +22,24 @@ def json(text):
print text print text
raise ValueError('Input must be serialised JSON.') raise ValueError('Input must be serialised JSON.')
def search(query, n=1): def search(query):
"""Search using SearchMash, return its JSON.""" """Search using AjaxSearch, and return its JSON."""
q = web.urllib.quote(query.encode('utf-8')) uri = 'http://ajax.googleapis.com/ajax/services/search/web'
uri = 'http://www.searchmash.com/results/' + q + '?n=' + str(n) args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8'))
bytes = web.get(uri) bytes = web.get(uri + args)
return json(bytes) return json(bytes)
def result(query): def result(query):
results = search(query) results = search(query)
if results['results']: if results['responseData']:
return results['results'][0]['url'] return results['responseData']['results'][0]['unescapedUrl']
return None return None
def count(query): def count(query):
results = search(query) results = search(query)
if not results['results']: if not results['responseData'] or not results['responseData']['cursor']:
return '0' return '0'
return results['estimatedCount'] return results['responseData']['cursor']['estimatedResultCount']
def formatnumber(n): def formatnumber(n):
"""Format a number with beautiful commas.""" """Format a number with beautiful commas."""
@ -66,11 +66,10 @@ g.example = '.g swhack'
def gc(phenny, input): def gc(phenny, input):
"""Returns the number of Google results for the specified input.""" """Returns the number of Google results for the specified input."""
if input.nick == 'goatov': return
query = input.group(2) query = input.group(2)
if not query: if not query:
return phenny.reply('.gc what?') return phenny.reply('.gc what?')
num = count(query) num = formatnumber(count(query))
phenny.say(query + ': ' + num) phenny.say(query + ': ' + num)
gc.commands = ['gc'] gc.commands = ['gc']
gc.priority = 'high' gc.priority = 'high'
@ -88,7 +87,7 @@ def gcs(phenny, input):
results = [] results = []
for i, query in enumerate(queries): for i, query in enumerate(queries):
query = query.strip('[]') query = query.strip('[]')
n = int((count(query) or '0').replace(',', '')) n = int((formatnumber(count(query)) or '0').replace(',', ''))
results.append((n, query)) results.append((n, query))
if i >= 2: __import__('time').sleep(0.25) if i >= 2: __import__('time').sleep(0.25)
if i >= 4: __import__('time').sleep(0.25) if i >= 4: __import__('time').sleep(0.25)