Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

43 rader
1.5 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. #
  4. # Public domain, 2013 Legoktm
  5. #
  6. from json import loads
  7. import re
  8. from urllib import quote
  9. from urllib2 import urlopen
  10. from earwigbot.commands import Command
  11. class UrbanDictionary(Command):
  12. """Get the definition of a word or phrase using Urban Dictionary."""
  13. name = "urban"
  14. commands = ["urban", "urbandictionary", "dct", "ud"]
  15. def process(self, data):
  16. if not data.args:
  17. self.reply(data, "What do you want to define?")
  18. return
  19. url = "http://api.urbandictionary.com/v0/define?term={0}"
  20. q = quote(' '.join(data.args), safe="")
  21. query = urlopen(url.format(q)).read()
  22. res = loads(query)
  23. if res['result_type'] == 'exact':
  24. definition = re.sub(r"\s+", " ", res['list'][0]['definition'])
  25. example = re.sub(r"\s+", " ", res['list'][0]['example'])
  26. url = res['list'][0]['permalink'].rsplit('/', 1)[0]
  27. if definition and definition[-1] not in (".", "!", "?"):
  28. definition += "."
  29. msg = '{0} \x02Example\x0F: {1} {2}'.format(
  30. definition.encode("utf8"), example.encode("utf8"), url)
  31. self.reply(data, msg)
  32. elif res['result_type'] == 'fulltext':
  33. L = [i['word'] for i in res['list']]
  34. msg = 'Here are some close matches: {0}.'
  35. self.reply(data, msg.format(u", ".join(L).encode("utf8")))
  36. else:
  37. self.reply(data, 'Sorry, no results found.')