PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/define.py

https://github.com/xray7224/MegBot
Python | 44 lines | 21 code | 6 blank | 17 comment | 5 complexity | ea99528cf5dfa4e81f7a9c653ca4f72f MD5 | raw file
Possible License(s): GPL-3.0
  1. # -*- coding: utf-8 -*-
  2. ##
  3. #This file is part of MegBot.
  4. #
  5. # MegBot is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # MegBot is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with MegBot. If not, see <http://www.gnu.org/licenses/>.
  17. ##
  18. from urllib2 import urlopen
  19. import re
  20. import json
  21. def main(connection):
  22. if not Info.args:
  23. Channel.send("Please supply the word you want to be defined.")
  24. return
  25. query_original = " ".join(Info.args)
  26. query_string = "+".join(Info.args)
  27. url = "http://api.duckduckgo.com/?q=%s&format=json" % query_string
  28. request = urlopen(url)
  29. output = json.loads(request.read())
  30. if not 'Definition' in output or output['Definition'] == '':
  31. Channel.send("Couldn't find a definition for %s", query_original)
  32. else:
  33. Channel.send(u"[%s] %s",
  34. Format.Bold(query_original),
  35. output['Definition']
  36. )
  37. help = u"Use DuckDuckGo to define a word or phrase"