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.

102 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  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 earwigbot import exceptions
  23. from earwigbot.commands import Command
  24. class Lag(Command):
  25. """Return the replag for a specific database on the Toolserver."""
  26. name = "lag"
  27. commands = ["lag", "replag", "maxlag"]
  28. def process(self, data):
  29. site = self.get_site(data)
  30. if not site:
  31. return
  32. if data.command == "replag":
  33. base = "\x0302{0}\x0F: {1}."
  34. msg = base.format(site.name, self.get_replag(site))
  35. elif data.command == "maxlag":
  36. base = "\x0302{0}\x0F: {1}."
  37. msg = base.format(site.name, self.get_maxlag(site).capitalize())
  38. else:
  39. base = "\x0302{0}\x0F: {1}; {2}."
  40. msg = base.format(site.name, self.get_replag(site),
  41. self.get_maxlag(site))
  42. self.reply(data, msg)
  43. def get_replag(self, site):
  44. return "Toolserver replag is {0}".format(self.time(site.get_replag()))
  45. def get_maxlag(self, site):
  46. return "database maxlag is {0}".format(self.time(site.get_maxlag()))
  47. def get_site(self, data):
  48. if data.kwargs and "project" in data.kwargs and "lang" in data.kwargs:
  49. project, lang = data.kwargs["project"], data.kwargs["lang"]
  50. return self.get_site_from_proj_and_lang(data, project, lang)
  51. if not data.args:
  52. return self.bot.wiki.get_site()
  53. if len(data.args) > 1:
  54. name = " ".join(data.args)
  55. self.reply(data, "Unknown site: \x0302{0}\x0F.".format(name))
  56. return
  57. name = data.args[0]
  58. if "." in name:
  59. lang, project = name.split(".")[:2]
  60. elif ":" in name:
  61. project, lang = name.split(":")[:2]
  62. else:
  63. try:
  64. return self.bot.wiki.get_site(name)
  65. except exceptions.SiteNotFoundError:
  66. msg = "Unknown site: \x0302{0}\x0F.".format(name)
  67. self.reply(data, msg)
  68. return
  69. return self.get_site_from_proj_and_lang(data, project, lang)
  70. def get_site_from_proj_and_lang(self, data, project, lang):
  71. try:
  72. site = self.bot.wiki.get_site(project=project, lang=lang)
  73. except exceptions.SiteNotFoundError:
  74. try:
  75. site = self.bot.wiki.add_site(project=project, lang=lang)
  76. except exceptions.APIError:
  77. msg = "Site \x0302{0}:{1}\x0F not found."
  78. self.reply(data, msg.format(project, lang))
  79. return
  80. return site
  81. def time(self, seconds):
  82. parts = [("year", 31536000), ("day", 86400), ("hour", 3600),
  83. ("minute", 60), ("second", 1)]
  84. msg = []
  85. for name, size in parts:
  86. num = seconds / size
  87. seconds -= num * size
  88. if num:
  89. chunk = "{0} {1}".format(num, name if num == 1 else name + "s")
  90. msg.append(chunk)
  91. return ", ".join(msg) if msg else "0 seconds"