Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

65 líneas
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from json import loads
  23. from urllib2 import urlopen, HTTPError
  24. from earwigbot.commands import Command
  25. class Stars(Command):
  26. """Get the number of stargazers for a given GitHub repository."""
  27. name = "stars"
  28. commands = ["stars", "stargazers"]
  29. API_URL = "https://api.github.com/repos/{repo}"
  30. EXAMPLE = "!stars earwig/earwigbot"
  31. def process(self, data):
  32. if not data.args:
  33. msg = "Which repository should I look up? Example: \x0306{0}\x0F."
  34. self.reply(data, msg.format(self.EXAMPLE))
  35. return
  36. repo = data.args[0]
  37. info = self.get_repo(repo)
  38. if info is None:
  39. self.reply(data, "Repository not found. Is it private?")
  40. else:
  41. count = int(info["stargazers_count"])
  42. plural = "" if count == 1 else "s"
  43. msg = "\x0303{0}\x0F has \x02{1}\x0F stargazer{2}: {3}"
  44. self.reply(data, msg.format(
  45. info["full_name"], count, plural, info["html_url"]))
  46. def get_repo(self, repo):
  47. """Return the API JSON dump for a given repository.
  48. Return None if the repo doesn't exist or is private.
  49. """
  50. try:
  51. query = urlopen(self.API_URL.format(repo=repo)).read()
  52. except HTTPError:
  53. return None
  54. res = loads(query)
  55. if res and "id" in res and not res["private"]:
  56. return res
  57. return None