A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

157 строки
6.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by 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. import logging
  23. from threading import Lock, Thread
  24. from time import sleep, time
  25. from earwigbot.config import BotConfig
  26. from earwigbot.irc import Frontend, Watcher
  27. from earwigbot.managers import CommandManager, TaskManager
  28. from earwigbot.wiki import SitesDB
  29. __all__ = ["Bot"]
  30. class Bot(object):
  31. """
  32. The Bot class is the core of EarwigBot, essentially responsible for
  33. starting the various bot components and making sure they are all happy.
  34. EarwigBot has three components that can run independently of each other: an
  35. IRC front-end, an IRC watcher, and a wiki scheduler.
  36. * The IRC front-end runs on a normal IRC server and expects users to
  37. interact with it/give it commands.
  38. * The IRC watcher runs on a wiki recent-changes server and listens for
  39. edits. Users cannot interact with this part of the bot.
  40. * The wiki scheduler runs wiki-editing bot tasks in separate threads at
  41. user-defined times through a cron-like interface.
  42. The Bot() object is accessable from within commands and tasks as self.bot.
  43. This is the primary way to access data from other components of the bot.
  44. For example, our BotConfig object is accessable from bot.config, tasks
  45. can be started with bot.tasks.start(), and sites can be loaded from the
  46. wiki toolset with bot.wiki.get_site().
  47. """
  48. def __init__(self, root_dir, level=logging.INFO):
  49. self.config = BotConfig(root_dir, level)
  50. self.logger = logging.getLogger("earwigbot")
  51. self.commands = CommandManager(self)
  52. self.tasks = TaskManager(self)
  53. self.wiki = SitesDB(self.config)
  54. self.frontend = None
  55. self.watcher = None
  56. self.component_lock = Lock()
  57. self._keep_looping = True
  58. self.config.load()
  59. self.commands.load()
  60. self.tasks.load()
  61. def _start_irc_components(self):
  62. """Start the IRC frontend/watcher in separate threads if enabled."""
  63. if self.config.components.get("irc_frontend"):
  64. self.logger.info("Starting IRC frontend")
  65. self.frontend = Frontend(self)
  66. Thread(name="irc_frontend", target=self.frontend.loop).start()
  67. if self.config.components.get("irc_watcher"):
  68. self.logger.info("Starting IRC watcher")
  69. self.watcher = Watcher(self)
  70. Thread(name="irc_watcher", target=self.watcher.loop).start()
  71. def _start_wiki_scheduler(self):
  72. """Start the wiki scheduler in a separate thread if enabled."""
  73. def wiki_scheduler():
  74. while self._keep_looping:
  75. time_start = time()
  76. self.tasks.schedule()
  77. time_end = time()
  78. time_diff = time_start - time_end
  79. if time_diff < 60: # Sleep until the next minute
  80. sleep(60 - time_diff)
  81. if self.config.components.get("wiki_scheduler"):
  82. self.logger.info("Starting wiki scheduler")
  83. thread = Thread(name="wiki_scheduler", target=wiki_scheduler)
  84. thread.daemon = True # Stop if other threads stop
  85. thread.start()
  86. def _stop_irc_components(self, msg):
  87. """Request the IRC frontend and watcher to stop if enabled."""
  88. if self.frontend:
  89. self.frontend.stop(msg)
  90. if self.watcher:
  91. self.watcher.stop(msg)
  92. def run(self):
  93. """Main entry point into running the bot.
  94. Starts all config-enabled components and then enters an idle loop,
  95. ensuring that all components remain online and restarting components
  96. that get disconnected from their servers.
  97. """
  98. self.logger.info("Starting bot")
  99. self._start_irc_components()
  100. self._start_wiki_scheduler()
  101. while self._keep_looping:
  102. with self.component_lock:
  103. if self.frontend and self.frontend.is_stopped():
  104. self.logger.warn("IRC frontend has stopped; restarting")
  105. self.frontend = Frontend(self)
  106. Thread(name=name, target=self.frontend.loop).start()
  107. if self.watcher and self.watcher.is_stopped():
  108. self.logger.warn("IRC watcher has stopped; restarting")
  109. self.watcher = Watcher(self)
  110. Thread(name=name, target=self.watcher.loop).start()
  111. sleep(2)
  112. def restart(self, msg=None):
  113. """Reload config, commands, tasks, and safely restart IRC components.
  114. This is thread-safe, and it will gracefully stop IRC components before
  115. reloading anything. Note that you can safely reload commands or tasks
  116. without restarting the bot with bot.commands.load() or
  117. bot.tasks.load(). These should not interfere with running components
  118. or tasks.
  119. If given, 'msg' will be used as our quit message.
  120. """
  121. self.logger.info("Restarting bot per request from owner")
  122. with self.component_lock:
  123. self._stop_irc_components(msg)
  124. self.config.load()
  125. self.commands.load()
  126. self.tasks.load()
  127. self._start_irc_components()
  128. def stop(self, msg=None):
  129. """Gracefully stop all bot components.
  130. If given, 'msg' will be used as our quit message.
  131. """
  132. self.logger.info("Stopping bot")
  133. with self.component_lock:
  134. self._stop_irc_components(msg)
  135. self._keep_looping = False