PageRenderTime 94ms CodeModel.GetById 70ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/search.py

https://github.com/mutantmonkey/phenny
Python | 200 lines | 185 code | 7 blank | 8 comment | 4 complexity | b46a93f93eb29fbe02b9ed76c2d548f1 MD5 | raw file
  1#!/usr/bin/env python
  2"""
  3search.py - Phenny Web Search Module
  4Copyright 2008-9, Sean B. Palmer, inamidst.com
  5Licensed under the Eiffel Forum License 2.
  6
  7http://inamidst.com/phenny/
  8"""
  9
 10import re
 11import web
 12
 13
 14r_google = re.compile(r'href="\/url\?q=(http.*?)&')
 15
 16def generic_google(query):
 17    query = web.quote(query)
 18    uri = 'https://google.co.uk/search?q=%s' % query
 19    return web.get(uri)
 20
 21def google_search(query): 
 22    bytes = generic_google(query)
 23    m = r_google.search(bytes)
 24    if m:
 25        uri = web.decode(m.group(1))
 26        return web.unquote(uri)
 27
 28r_google_count = re.compile(r'id="resultStats">About (.*?) ')
 29
 30def google_count(query): 
 31    query = web.quote(query)
 32    uri = 'https://google.co.uk/search?q=%s' % query
 33    bytes = web.get(uri)
 34    m = r_google_count.search(bytes)
 35    if m:
 36        result = web.decode(m.group(1)).replace(',', '')
 37        return int(result)
 38    else: return 0
 39    
 40def formatnumber(n): 
 41    """Format a number with beautiful commas."""
 42    parts = list(str(n))
 43    for i in range((len(parts) - 3), 0, -3):
 44        parts.insert(i, ',')
 45    return ''.join(parts)
 46
 47def g(phenny, input): 
 48    """Queries Google for the specified input."""
 49    query = input.group(2)
 50    if not query: 
 51        return phenny.reply('.g what?')
 52    uri = google_search(query)
 53    if uri: 
 54        phenny.reply(uri)
 55        if not hasattr(phenny.bot, 'last_seen_uri'):
 56            phenny.bot.last_seen_uri = {}
 57        phenny.bot.last_seen_uri[input.sender] = uri
 58    else: phenny.reply("No results found for '%s'." % query)
 59g.commands = ['g']
 60g.priority = 'high'
 61g.example = '.g swhack'
 62
 63def gc(phenny, input): 
 64    """Returns the number of Google results for the specified input."""
 65    query = input.group(2)
 66    if not query: 
 67        return phenny.reply('.gc what?')
 68    num = formatnumber(google_count(query))
 69    phenny.say(query + ': ' + num)
 70gc.commands = ['gc']
 71gc.priority = 'high'
 72gc.example = '.gc extrapolate'
 73
 74r_query = re.compile(
 75    r'\+?"[^"\\]*(?:\\.[^"\\]*)*"|\[[^]\\]*(?:\\.[^]\\]*)*\]|\S+'
 76)
 77
 78def gcs(phenny, input): 
 79    """Compare the number of Google results for the specified paramters."""
 80    if not input.group(2):
 81        return phenny.reply("Nothing to compare.")
 82    queries = r_query.findall(input.group(2))
 83    if len(queries) > 6: 
 84        return phenny.reply('Sorry, can only compare up to six things.')
 85    results = []
 86    for i, query in enumerate(queries): 
 87        query = query.strip('[]')
 88        n = int((formatnumber(google_count(query)) or '0').replace(',', ''))
 89        results.append((n, query))
 90        if i >= 2: __import__('time').sleep(0.25)
 91        if i >= 4: __import__('time').sleep(0.25)
 92
 93    results = [(term, n) for (n, term) in reversed(sorted(results))]
 94    reply = ', '.join('%s (%s)' % (t, formatnumber(n)) for (t, n) in results)
 95    phenny.say(reply)
 96gcs.commands = ['gcs', 'comp']
 97gcs.example = '.gcs Ronaldo Messi'
 98
 99r_bing = re.compile(r'<h2><a href="([^"]+)"')
100
101def bing_search(query, lang='en-GB'): 
102    query = web.quote(query)
103    base = 'https://www.bing.com/search?mkt=%s&q=' % lang
104    bytes = web.get(base + query)
105    m = r_bing.search(bytes)
106    if m: return m.group(1)
107
108def bing(phenny, input): 
109    """Queries Bing for the specified input."""
110    query = input.group(2)
111    if query.startswith(':'): 
112        lang, query = query.split(' ', 1)
113        lang = lang[1:]
114    else: lang = 'en-GB'
115    if not query:
116        return phenny.reply('.bing what?')
117    uri = bing_search(query, lang)
118    if uri: 
119        phenny.reply(uri)
120        if not hasattr(phenny.bot, 'last_seen_uri'):
121            phenny.bot.last_seen_uri = {}
122        phenny.bot.last_seen_uri[input.sender] = uri
123    else: phenny.reply("No results found for '%s'." % query)
124bing.commands = ['bing']
125bing.example = '.bing swhack'
126
127r_duck = re.compile(r'web-result.*?nofollow.*?href=".+?(http.*?)"', re.DOTALL)
128
129def duck_search(query): 
130    query = query.replace('!', '')
131    query = web.quote(query)
132    uri = 'https://duckduckgo.com/html/?q=%s&kl=uk-en' % query
133    bytes = web.get(uri)
134    m = r_duck.search(bytes)
135    if m:
136        uri = web.decode(m.group(1))
137        return web.unquote(uri)
138
139def duck_api(query):
140    uri = 'https://api.duckduckgo.com/?q=%s&format=json&no_redirect=1' % query
141    bytes = web.get(uri)
142    json = web.json(bytes)
143    if query[:1] == '!':
144        return json['Redirect']
145    elif json['Abstract']:
146        return json['AbstractURL'] + ' : ' + json['Abstract']
147    else: return json['AbstractURL']
148
149def duck(phenny, input):
150    """Queries DuckDuckGo for specified input.""" 
151    query = input.group(2)
152    if not query: return phenny.reply('.ddg what?')
153    uri = duck_api(query)
154    if not uri:
155        uri = duck_search(query)
156    if uri: 
157        phenny.reply(uri)
158        if not hasattr(phenny.bot, 'last_seen_uri'):
159            phenny.bot.last_seen_uri = {}
160        phenny.bot.last_seen_uri[input.sender] = uri
161    else: phenny.reply("No results found for '%s'." % query)
162duck.commands = ['duck', 'ddg']
163duck.example = '.duck football'
164
165def newton_api(operation, expression):
166    expression = web.quote(expression, safe='')
167    uri = "https://newton.now.sh/{}/{}".format(operation, expression)
168    bytes = web.get(uri)
169    json = web.json(bytes)
170    if 'result' in json:
171        return str(json['result'])
172    return None
173
174def search(phenny, input): 
175    if not input.group(2): 
176        return phenny.reply('.search for what?')
177    query = input.group(2)
178    gu = google_search(query) or '-'
179    bu = bing_search(query) or '-'
180    du = duck_search(query) or '-'
181
182    if (gu == bu) and (bu == du): 
183        result = '%s (g, b, d)' % gu
184    elif (gu == bu): 
185        result = '%s (g, b), %s (d)' % (gu, du)
186    elif (bu == du): 
187        result = '%s (b, d), %s (g)' % (bu, gu)
188    elif (gu == du): 
189        result = '%s (g, d), %s (b)' % (gu, bu)
190    else: 
191        if len(gu) > 250: gu = '(extremely long link)'
192        if len(bu) > 150: bu = '(extremely long link)'
193        if len(du) > 150: du = '(extremely long link)'
194        result = '%s (g), %s (b), %s (d)' % (gu, bu, du)
195
196    phenny.reply(result)
197search.commands = ['search']
198
199if __name__ == '__main__': 
200    print(__doc__.strip())