diff --git a/search.py b/search.py index 994ce13007..3d82289844 100644 --- a/search.py +++ b/search.py @@ -17,35 +17,6 @@ import time -def google_ajax(query): - """Search using AjaxSearch, and return its JSON.""" - uri = 'http://ajax.googleapis.com/ajax/services/search/web' - args = '?v=1.0&safe=off&q=' + query - bytes = web.get(uri + args) - return json.loads(bytes) - - -def google_search(query): - results = google_ajax(query) - try: - return results['responseData']['results'][0]['unescapedUrl'] - except IndexError: - return None - except TypeError: - return False - - -def google_count(query): - results = google_ajax(query) - if not 'responseData' in results: - return '0' - if not 'cursor' in results['responseData']: - return '0' - if not 'estimatedResultCount' in results['responseData']['cursor']: - return '0' - return results['responseData']['cursor']['estimatedResultCount'] - - def formatnumber(n): """Format a number with beautiful commas.""" parts = list(str(n)) @@ -53,64 +24,6 @@ def formatnumber(n): parts.insert(i, ',') return ''.join(parts) - -@commands('g', 'google') -@example('.g swhack') -def g(bot, trigger): - """Queries Google for the specified input.""" - query = trigger.group(2) - if not query: - return bot.reply('.g what?') - uri = google_search(query) - if uri: - bot.reply(uri) - if 'last_seen_url' in bot.memory: - bot.memory['last_seen_url'][trigger.sender] = uri - elif uri is False: - bot.reply("Problem getting data from Google.") - else: - bot.reply("No results found for '%s'." % query) - - -@commands('gc') -@example('.gc extrapolate') -def gc(bot, trigger): - """Returns the number of Google results for the specified input.""" - query = trigger.group(2) - if not query: - return bot.reply('.gc what?') - num = formatnumber(google_count(query)) - bot.say(query + ': ' + num) - -r_query = re.compile( - r'\+?"[^"\\]*(?:\\.[^"\\]*)*"|\[[^]\\]*(?:\\.[^]\\]*)*\]|\S+' -) - - -@commands('gcs', 'comp') -@example('.gcs foo bar') -def gcs(bot, trigger): - """Compare the number of Google search results""" - if not trigger.group(2): - return bot.reply("Nothing to compare.") - queries = r_query.findall(trigger.group(2)) - if len(queries) > 6: - return bot.reply('Sorry, can only compare up to six things.') - - results = [] - for i, query in enumerate(queries): - query = query.strip('[]') - n = int((formatnumber(google_count(query)) or '0').replace(',', '')) - results.append((n, query)) - if i >= 2: - time.sleep(0.25) - if i >= 4: - 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) - bot.say(reply) - r_bing = re.compile(r'

250: - gu = '(extremely long link)' if len(bu) > 150: bu = '(extremely long link)' if len(du) > 150: du = '(extremely long link)' - result = '%s (g), %s (b), %s (d)' % (gu, bu, du) + result = '%s (b), %s (d)' % (bu, du) bot.reply(result)