askcompu

Untitled

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