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.

75 lines
3.1 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. if data.kwargs and "project" in data.kwargs and "lang" in data.kwargs:
  30. project, lang = data.kwargs["project"], data.kwargs["lang"]
  31. site = self.get_site(data, project, lang)
  32. if not site:
  33. return
  34. elif data.args:
  35. if len(data.args) > 1:
  36. name = " ".join(data.args)
  37. self.reply(data, "unknown site: \x0302{0}\x0F.".format(name))
  38. return
  39. name = data.args[0]
  40. if "." in name:
  41. lang, project = name.split(".")[:2]
  42. elif ":" in name:
  43. project, lang = name.split(":")[:2]
  44. else:
  45. try:
  46. site = self.bot.wiki.get_site(name)
  47. except exceptions.SiteNotFoundError:
  48. msg = "unknown site: \x0302{0}\x0F.".format(name)
  49. self.reply(data, msg)
  50. return
  51. site = self.get_site(data, project, lang)
  52. if not site:
  53. return
  54. else:
  55. site = self.bot.wiki.get_site()
  56. msg = "\x0302{0}\x0F: Toolserver replag is {1} seconds; database maxlag is {2} seconds"
  57. msg = msg.format(site.name, site.get_replag(), site.get_maxlag())
  58. self.reply(data, msg)
  59. def get_site(self, data, project, lang):
  60. try:
  61. site = self.bot.wiki.get_site(project=project, lang=lang)
  62. except exceptions.SiteNotFoundError:
  63. try:
  64. site = self.bot.wiki.add_site(project=project, lang=lang)
  65. except exceptions.APIError:
  66. msg = "site \x0302{0}:{1}\x0F not found."
  67. self.reply(data, msg.format(project, lang))
  68. return
  69. return site