From 1856781604aef3f339ec1e5c06d95c92a1ce300f Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Thu, 20 Nov 2008 10:18:21 +0000 Subject: Updated the search facilities. --- modules/oblique.py | 2 +- modules/search.py | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) (limited to 'modules') diff --git a/modules/oblique.py b/modules/oblique.py index a4aba17..a680b6d 100755 --- a/modules/oblique.py +++ b/modules/oblique.py @@ -24,7 +24,7 @@ def mappings(uri): command, template = item.split(' ', 1) if not template.startswith('http://'): continue - result[command] = template + result[command] = template.replace('&', '&') return result def o(phenny, input): diff --git a/modules/search.py b/modules/search.py index b1123b8..4fcaabc 100755 --- a/modules/search.py +++ b/modules/search.py @@ -22,24 +22,24 @@ def json(text): print text 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) +def search(query): + """Search using AjaxSearch, and return its JSON.""" + uri = 'http://ajax.googleapis.com/ajax/services/search/web' + args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8')) + bytes = web.get(uri + args) return json(bytes) def result(query): results = search(query) - if results['results']: - return results['results'][0]['url'] + if results['responseData']: + return results['responseData']['results'][0]['unescapedUrl'] return None def count(query): results = search(query) - if not results['results']: + if not results['responseData'] or not results['responseData']['cursor']: return '0' - return results['estimatedCount'] + return results['responseData']['cursor']['estimatedResultCount'] def formatnumber(n): """Format a number with beautiful commas.""" @@ -66,11 +66,10 @@ g.example = '.g swhack' def gc(phenny, input): """Returns the number of Google results for the specified input.""" - if input.nick == 'goatov': return query = input.group(2) if not query: return phenny.reply('.gc what?') - num = count(query) + num = formatnumber(count(query)) phenny.say(query + ': ' + num) gc.commands = ['gc'] gc.priority = 'high' @@ -88,7 +87,7 @@ def gcs(phenny, input): results = [] for i, query in enumerate(queries): query = query.strip('[]') - n = int((count(query) or '0').replace(',', '')) + n = int((formatnumber(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) -- cgit v1.2.3-1-g7c22