/modules/search.py

https://github.com/apertium/phenny · Python · 97 lines · 75 code · 12 blank · 10 comment · 16 complexity · 7e1c4e87b83feab766db3e2dc1275681 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. import requests
  11. from tools import truncate
  12. from modules import more
  13. from web import catch_timeout, is_up, REQUEST_TIMEOUT
  14. ddg_uri = 'https://api.duckduckgo.com/?format=json&pretty=1&q='
  15. suggest_uri = 'http://suggestqueries.google.com/complete/search?client=firefox&hl=en&q='
  16. def gsearch(phenny, input):
  17. phenny.reply('.g is deprecated. Try .search powered by Duck Duck Go instead.')
  18. gsearch.commands = 'g'
  19. def topics(phenny, input):
  20. if not is_up('https://api.duckduckgo.com'):
  21. return phenny.say('Sorry, DuckDuckGo API is down.')
  22. if not input.group(1):
  23. return phenny.reply('.topics about what?')
  24. query = input.group(1)
  25. r = requests.get(ddg_uri + query, timeout=REQUEST_TIMEOUT).json()
  26. topics = r['RelatedTopics']
  27. if len(topics) == 0:
  28. return phenny.say('Sorry, no topics found.')
  29. topics_list = []
  30. counter = 0
  31. for topic in r['RelatedTopics'][:10]:
  32. try:
  33. topics_list.append(topic['Text'] + ' - ' + topic['FirstURL'])
  34. except KeyError:
  35. continue
  36. phenny.say(topics_list[0])
  37. phenny.reply('Check PM for more topics.')
  38. more.add_messages(phenny, input.nick, topics_list[1:])
  39. topics.commands = ['topics']
  40. def search(phenny, input):
  41. if not input.group(1):
  42. return phenny.reply('.search for what?')
  43. query = input.group(1)
  44. if not is_up('https://api.duckduckgo.com'):
  45. return phenny.say('Sorry, DuckDuckGo API is down.')
  46. r = requests.get(ddg_uri + query, timeout=REQUEST_TIMEOUT).json()
  47. try:
  48. answer = r['AbstractText']
  49. answer_url = r['AbstractURL']
  50. if answer == '':
  51. answer = r['RelatedTopics'][0]['Text']
  52. answer_url = r['RelatedTopics'][0]['FirstURL']
  53. if answer == '':
  54. return phenny.say('Sorry, no result.')
  55. except:
  56. return phenny.say('Sorry, no result.')
  57. # Removes html tags, if exist
  58. answer = re.sub('<.+?>', '', answer)
  59. phenny.say(truncate(answer, '{} - ' + answer_url))
  60. search.commands = ['search']
  61. def suggest(phenny, input):
  62. ''' Shows first 10 results from Google Suggestion API.'''
  63. if not input.group(1):
  64. return phenny.reply("No query term.")
  65. query = input.group(1)
  66. answer = requests.get(suggest_uri + query)
  67. suggestions = answer.json()[1][:10]
  68. phenny.reply(suggestions[0])
  69. phenny.reply('Check PM for more.')
  70. more.add_messages(phenny, input.nick, suggestions[1:])
  71. suggest.commands = ['suggest']
  72. def lmgtfy(phenny, input):
  73. if not input.group(1):
  74. phenny.reply('.lmgtfy what f who?')
  75. try:
  76. (who, what) = input.group(1).split(' ', 1)
  77. response = "%s: http://lmgtfy.com/?q=%s"
  78. what = web.quote(what)
  79. phenny.say(response % (who, what))
  80. except ValueError:
  81. phenny.reply('.lmgtfy what for who? (enter a nick and a query)')
  82. lmgtfy.commands = ['lmgtfy']
  83. if __name__ == '__main__':
  84. print(__doc__.strip())