A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.0 KiB

  1. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from earwigbot import exceptions
  21. from earwigbot.commands import Command
  22. class Lag(Command):
  23. """Return replag or maxlag information on specific databases."""
  24. name = "lag"
  25. commands = ["lag", "replag", "maxlag"]
  26. def process(self, data):
  27. site = self.get_site(data)
  28. if not site:
  29. return
  30. if data.command == "replag":
  31. base = "\x0302{0}\x0f: {1}."
  32. msg = base.format(site.name, self.get_replag(site))
  33. elif data.command == "maxlag":
  34. base = "\x0302{0}\x0f: {1}."
  35. msg = base.format(site.name, self.get_maxlag(site))
  36. else:
  37. base = "\x0302{0}\x0f: {1}; {2}."
  38. msg = base.format(site.name, self.get_replag(site), self.get_maxlag(site))
  39. self.reply(data, msg)
  40. def get_replag(self, site):
  41. return f"SQL replag is {self.time(site.get_replag())}"
  42. def get_maxlag(self, site):
  43. return f"API maxlag is {self.time(site.get_maxlag())}"
  44. def get_site(self, data):
  45. if data.kwargs and "project" in data.kwargs and "lang" in data.kwargs:
  46. project, lang = data.kwargs["project"], data.kwargs["lang"]
  47. return self.get_site_from_proj_and_lang(data, project, lang)
  48. if not data.args:
  49. return self.bot.wiki.get_site()
  50. if len(data.args) > 1:
  51. name = " ".join(data.args)
  52. self.reply(data, f"Unknown site: \x0302{name}\x0f.")
  53. return
  54. name = data.args[0]
  55. if "." in name:
  56. lang, project = name.split(".")[:2]
  57. elif ":" in name:
  58. project, lang = name.split(":")[:2]
  59. else:
  60. try:
  61. return self.bot.wiki.get_site(name)
  62. except exceptions.SiteNotFoundError:
  63. msg = f"Unknown site: \x0302{name}\x0f."
  64. self.reply(data, msg)
  65. return
  66. return self.get_site_from_proj_and_lang(data, project, lang)
  67. def get_site_from_proj_and_lang(self, data, project, lang):
  68. try:
  69. site = self.bot.wiki.get_site(project=project, lang=lang)
  70. except exceptions.SiteNotFoundError:
  71. try:
  72. site = self.bot.wiki.add_site(project=project, lang=lang)
  73. except exceptions.APIError:
  74. msg = "Site \x0302{0}:{1}\x0f not found."
  75. self.reply(data, msg.format(project, lang))
  76. return
  77. return site
  78. def time(self, seconds):
  79. parts = [
  80. ("year", 31536000),
  81. ("day", 86400),
  82. ("hour", 3600),
  83. ("minute", 60),
  84. ("second", 1),
  85. ]
  86. msg = []
  87. for name, size in parts:
  88. num = seconds / size
  89. seconds -= num * size
  90. if num:
  91. chunk = "{} {}".format(num, name if num == 1 else name + "s")
  92. msg.append(chunk)
  93. return ", ".join(msg) if msg else "0 seconds"