From 9505eae02ec38624c0e2a58247f59b65f87ddb3d Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Sat, 21 Jul 2012 13:04:31 +0100 Subject: Twitter module, gc augmentation, and various small fixes --- modules/search.py | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) (limited to 'modules/search.py') diff --git a/modules/search.py b/modules/search.py index b884d1c..89e57b2 100755 --- a/modules/search.py +++ b/modules/search.py @@ -53,6 +53,9 @@ def formatnumber(n): parts.insert(i, ',') return ''.join(parts) +def old_gc(query): + return formatnumber(google_count(query)) + def g(phenny, input): """Queries Google for the specified input.""" query = input.group(2) @@ -71,7 +74,7 @@ g.commands = ['g'] g.priority = 'high' g.example = '.g swhack' -def gc(phenny, input): +def oldgc(phenny, input): """Returns the number of Google results for the specified input.""" query = input.group(2) if not query: @@ -79,9 +82,8 @@ def gc(phenny, input): query = query.encode('utf-8') num = formatnumber(google_count(query)) phenny.say(query + ': ' + num) -gc.commands = ['gc'] -gc.priority = 'high' -gc.example = '.gc extrapolate' +oldgc.commands = ['ogc', 'oldgc'] +oldgc.example = '.oldgc extrapolate' r_query = re.compile( r'\+?"[^"\\]*(?:\\.[^"\\]*)*"|\[[^]\\]*(?:\\.[^]\\]*)*\]|\S+' @@ -114,13 +116,12 @@ def bing_search(query, lang='en-GB'): query = web.urllib.quote(query) base = 'http://www.bing.com/search?mkt=%s&q=' % lang bytes = web.get(base + query) - m = r_bing.search(bytes) - if m: return m.group(1) + for result in r_bing.findall(bytes): + if "r.msn.com/" in result: continue + return result def bing(phenny, input): """Queries Bing for the specified input.""" - return phenny.reply("Sorry, .bing is disabled pending a QA review.") - query = input.group(2) if query.startswith(':'): lang, query = query.split(' ', 1) @@ -200,5 +201,41 @@ def suggest(phenny, input): else: phenny.reply('Sorry, no result.') suggest.commands = ['suggest'] +def new_gc(query): + uri = 'https://www.google.com/search?hl=en&q=' + uri = uri + web.urllib.quote(query).replace('+', '%2B') + if '"' in query: uri += '&tbs=li:1' + bytes = web.get(uri) + if "did not match any documents" in bytes: + return "0" + for result in re.compile(r'(?ims)([0-9,]+) results?').findall(bytes): + return result + return None + +def ngc(phenny, input): + if not input.group(2): + return phenny.reply("No query term.") + query = input.group(2).encode('utf-8') + result = new_gc(query) + if result: + phenny.say(query + ": " + result) + else: phenny.reply("Sorry, couldn't get a result.") + +ngc.commands = ['ngc'] +ngc.priority = 'high' +ngc.example = '.ngc extrapolate' + +def gc(phenny, input): + if not input.group(2): + return phenny.reply("No query term.") + query = input.group(2).encode('utf-8') + old = old_gc(query) or "?" + new = new_gc(query) or "?" + phenny.say(query + ": " + old + " / " + new) + +gc.commands = ['gc'] +gc.priority = 'high' +gc.example = '.gc extrapolate' + if __name__ == '__main__': print __doc__.strip() -- cgit v1.2.3-1-g7c22