askcompu

Untitled

Jul 22nd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. search.py - Phenny Web Search Module
  4. Copyright 2008-9, Sean B. Palmer, inamidst.com
  5. Licensed under the Eiffel Forum License 2.
  6.  
  7. http://inamidst.com/phenny/
  8. """
  9.  
  10. import re
  11. import web
  12.  
  13. class Grab(web.urllib.request.URLopener):
  14.     def __init__(self, *args):
  15.         self.version = 'Mozilla/5.0 (Phenny)'
  16.         web.urllib.request.URLopener.__init__(self, *args)
  17.         self.addheader('Referer', 'https://github.com/sbp/phenny')
  18.     def http_error_default(self, url, fp, errcode, errmsg, headers):
  19.         return web.urllib.addinfourl(fp, [headers, errcode], "http:" + url)
  20.  
  21. def google_ajax(query):
  22.     """Search using AjaxSearch, and return its JSON."""
  23.     if isinstance(query, str):
  24.         query = query.encode('utf-8')
  25.     uri = 'http://ajax.googleapis.com/ajax/services/search/web'
  26.     args = '?v=1.0&safe=off&q=' + web.quote(query)
  27.     handler = web.urllib.request._urlopener
  28.     web.urllib.request._urlopener = Grab()
  29.     bytes = web.get(uri + args)
  30.     web.urllib.request._urlopener = handler
  31.     return web.json(bytes)
  32.  
  33. def google_search(query):
  34.     results = google_ajax(query)
  35.     try: return results['responseData']['results'][0]['unescapedUrl']
  36.     except IndexError: return None
  37.     except TypeError:
  38.         print(results)
  39.         return False
  40.  
  41. def google_count(query):
  42.     results = google_ajax(query)
  43.     if 'responseData' not in results: return '0'
  44.     if 'cursor' not in results['responseData']: return '0'
  45.     if 'estimatedResultCount' not in results['responseData']['cursor']:
  46.         return '0'
  47.     return results['responseData']['cursor']['estimatedResultCount']
  48.  
  49. def formatnumber(n):
  50.     """Format a number with beautiful commas."""
  51.     parts = list(str(n))
  52.     for i in range((len(parts) - 3), 0, -3):
  53.         parts.insert(i, ',')
  54.     return ''.join(parts)
  55.  
  56. def g(phenny, input):
  57.     """Queries Google for the specified input."""
  58.     query = input.group(2)
  59.     if not query:
  60.         return phenny.reply('.g what?')
  61.     uri = google_search(query)
  62.     if uri:
  63.         phenny.reply(uri)
  64.         if not hasattr(phenny.bot, 'last_seen_uri'):
  65.             phenny.bot.last_seen_uri = {}
  66.         phenny.bot.last_seen_uri[input.sender] = uri
  67.     elif uri is False: phenny.reply("Problem getting data from Google.")
  68.     else: phenny.reply("No results found for '%s'." % query)
  69. g.commands = ['g']
  70. g.priority = 'high'
  71. g.example = '.g swhack'
  72.  
  73. def gc(phenny, input):
  74.     """Returns the number of Google results for the specified input."""
  75.     query = input.group(2)
  76.     if not query:
  77.         return phenny.reply('.gc what?')
  78.     num = formatnumber(google_count(query))
  79.     phenny.say(query + ': ' + num)
  80. gc.commands = ['gc']
  81. gc.priority = 'high'
  82. gc.example = '.gc extrapolate'
  83.  
  84. r_query = re.compile(
  85.     r'\+?"[^"\\]*(?:\\.[^"\\]*)*"|\[[^]\\]*(?:\\.[^]\\]*)*\]|\S+'
  86. )
  87.  
  88. def gcs(phenny, input):
  89.     if not input.group(2):
  90.         return phenny.reply("Nothing to compare.")
  91.     queries = r_query.findall(input.group(2))
  92.     if len(queries) > 6:
  93.         return phenny.reply('Sorry, can only compare up to six things.')
  94.  
  95.     results = []
  96.     for i, query in enumerate(queries):
  97.         query = query.strip('[]')
  98.         n = int((formatnumber(google_count(query)) or '0').replace(',', ''))
  99.         results.append((n, query))
  100.         if i >= 2: __import__('time').sleep(0.25)
  101.         if i >= 4: __import__('time').sleep(0.25)
  102.  
  103.     results = [(term, n) for (n, term) in reversed(sorted(results))]
  104.     reply = ', '.join('%s (%s)' % (t, formatnumber(n)) for (t, n) in results)
  105.     phenny.say(reply)
  106. gcs.commands = ['gcs', 'comp']
  107.  
  108. r_bing = re.compile(r'<h3><a href="([^"]+)"')
  109.  
  110. def bing_search(query, lang='en-GB'):
  111.     query = web.quote(query)
  112.     base = 'http://www.bing.com/search?mkt=%s&q=' % lang
  113.     bytes = web.get(base + query)
  114.     m = r_bing.search(bytes)
  115.     if m: return m.group(1)
  116.  
  117. def bing(phenny, input):
  118.     """Queries Bing for the specified input."""
  119.     query = input.group(2)
  120.     if query.startswith(':'):
  121.         lang, query = query.split(' ', 1)
  122.         lang = lang[1:]
  123.     else: lang = 'en-GB'
  124.     if not query:
  125.         return phenny.reply('.bing what?')
  126.  
  127.     uri = bing_search(query, lang)
  128.     if uri:
  129.         phenny.reply(uri)
  130.         if not hasattr(phenny.bot, 'last_seen_uri'):
  131.             phenny.bot.last_seen_uri = {}
  132.         phenny.bot.last_seen_uri[input.sender] = uri
  133.     else: phenny.reply("No results found for '%s'." % query)
  134. bing.commands = ['bing']
  135. bing.example = '.bing swhack'
  136.  
  137. r_duck = re.compile(r'nofollow" class="[^"]+" href="(http.*?)">')
  138.  
  139. def duck_search(query):
  140.     query = query.replace('!', '')
  141.     query = web.quote(query)
  142.     uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
  143.     bytes = web.get(uri)
  144.     m = r_duck.search(bytes)
  145.     if m: return web.decode(m.group(1))
  146.  
  147. def duck(phenny, input):
  148.     query = input.group(2)
  149.     if not query: return phenny.reply('.ddg what?')
  150.  
  151.     uri = duck_search(query)
  152.     if uri:
  153.         phenny.reply(uri)
  154.         if not hasattr(phenny.bot, 'last_seen_uri'):
  155.             phenny.bot.last_seen_uri = {}
  156.         phenny.bot.last_seen_uri[input.sender] = uri
  157.     else: phenny.reply("No results found for '%s'." % query)
  158. duck.commands = ['duck', 'ddg']
  159.  
  160. def search(phenny, input):
  161.     if not input.group(2):
  162.         return phenny.reply('.search for what?')
  163.     query = input.group(2)
  164.     gu = google_search(query) or '-'
  165.     bu = bing_search(query) or '-'
  166.     du = duck_search(query) or '-'
  167.  
  168.     if (gu == bu) and (bu == du):
  169.         result = '%s (g, b, d)' % gu
  170.     elif (gu == bu):
  171.         result = '%s (g, b), %s (d)' % (gu, du)
  172.     elif (bu == du):
  173.         result = '%s (b, d), %s (g)' % (bu, gu)
  174.     elif (gu == du):
  175.         result = '%s (g, d), %s (b)' % (gu, bu)
  176.     else:
  177.         if len(gu) > 250: gu = '(extremely long link)'
  178.         if len(bu) > 150: bu = '(extremely long link)'
  179.         if len(du) > 150: du = '(extremely long link)'
  180.         result = '%s (g), %s (b), %s (d)' % (gu, bu, du)
  181.  
  182.     phenny.reply(result)
  183. search.commands = ['search']
  184.  
  185. def suggest(phenny, input):
  186.     if not input.group(2):
  187.         return phenny.reply("No query term.")
  188.     query = input.group(2)
  189.     uri = 'http://websitedev.de/temp-bin/suggest.pl?q='
  190.     answer = web.get(uri + web.quote(query).replace('+', '%2B'))
  191.     if answer:
  192.         phenny.say(answer)
  193.     else: phenny.reply('Sorry, no result.')
  194. suggest.commands = ['suggest']
  195.  
  196. if __name__ == '__main__':
  197.     print(__doc__.strip())
Advertisement
Add Comment
Please, Sign In to add comment