/modules/duckduckgo.py

https://github.com/jesopo/bitbot · Python · 26 lines · 15 code · 6 blank · 5 comment · 5 complexity · 959d7ef4b5a19ac2b9133e3246e5f29c MD5 · raw file

  1. #--depends-on commands
  2. from src import ModuleManager, utils
  3. URL_DDG = "https://api.duckduckgo.com"
  4. class Module(ModuleManager.BaseModule):
  5. _name = "DDG"
  6. @utils.hook("received.command.ddg", min_args=1)
  7. def duckduckgo(self, event):
  8. """
  9. :help: Get first DuckDuckGo result for a given search term
  10. :usage: [search term]
  11. """
  12. phrase = event["args"] or event["target"].buffer.get()
  13. if phrase:
  14. page = utils.http.request(URL_DDG, get_params={
  15. "q": phrase, "format": "json", "no_html": "1",
  16. "no_redirect": "1"}).json()
  17. if page and page["AbstractURL"]:
  18. event["stdout"].write(page["AbstractURL"])
  19. else:
  20. event["stderr"].write("No results found")