Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stars.py 2.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 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
  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. msg = "\x0303{0}\x0F has \x02{1}\x0F stargazers: {2}"
  42. self.reply(data, msg.format(
  43. info["full_name"], info["stargazers_count"], info["html_url"]))
  44. def get_repo(self, repo):
  45. """Return the API JSON dump for a given repository.
  46. Return None if the repo doesn't exist or is private.
  47. """
  48. try:
  49. query = urlopen(self.API_URL.format(repo=repo)).read()
  50. except HTTPError:
  51. return None
  52. res = loads(query)
  53. if res and "id" in res and not res["private"]:
  54. return res
  55. return None