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