A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

140 řádky
5.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. """
  23. EarwigBot's Main Module
  24. The core is essentially responsible for starting the various bot components
  25. (irc, scheduler, etc) and making sure they are all happy. An explanation of the
  26. different components follows:
  27. EarwigBot has three components that can run independently of each other: an IRC
  28. front-end, an IRC watcher, and a wiki scheduler.
  29. * The IRC front-end runs on a normal IRC server and expects users to interact
  30. with it/give it commands.
  31. * The IRC watcher runs on a wiki recent-changes server and listens for edits.
  32. Users cannot interact with this part of the bot.
  33. * The wiki scheduler runs wiki-editing bot tasks in separate threads at
  34. user-defined times through a cron-like interface.
  35. There is a "priority" system here:
  36. 1. If the IRC frontend is enabled, it will run on the main thread, and the IRC
  37. watcher and wiki scheduler (if enabled) will run on separate threads.
  38. 2. If the wiki scheduler is enabled, it will run on the main thread, and the
  39. IRC watcher (if enabled) will run on a separate thread.
  40. 3. If the IRC watcher is enabled, it will run on the main (and only) thread.
  41. Else, the bot will stop, as no components are enabled.
  42. """
  43. import logging
  44. import threading
  45. import time
  46. from earwigbot import tasks
  47. from earwigbot.irc import Frontend, Watcher
  48. from earwigbot.config import config
  49. logger = logging.getLogger("earwigbot")
  50. def irc_watcher(frontend=None):
  51. """Function to handle the IRC watcher as another thread (if frontend and/or
  52. scheduler is enabled), otherwise run as the main thread."""
  53. while 1: # Restart the watcher component if it breaks (and nothing else)
  54. watcher = Watcher(frontend)
  55. try:
  56. watcher.loop()
  57. except:
  58. logger.exception("Watcher had an error")
  59. time.sleep(5) # Sleep a bit before restarting watcher
  60. logger.warn("Watcher has stopped; restarting component")
  61. def wiki_scheduler():
  62. """Function to handle the wiki scheduler as another thread, or as the
  63. primary thread if the IRC frontend is not enabled."""
  64. while 1:
  65. time_start = time.time()
  66. now = time.gmtime(time_start)
  67. tasks.schedule(now)
  68. time_end = time.time()
  69. time_diff = time_start - time_end
  70. if time_diff < 60: # Sleep until the next minute
  71. time.sleep(60 - time_diff)
  72. def irc_frontend():
  73. """If the IRC frontend is enabled, make it run on our primary thread, and
  74. enable the wiki scheduler and IRC watcher on new threads if they are
  75. enabled."""
  76. logger.info("Starting IRC frontend")
  77. frontend = Frontend()
  78. if config.components.get("wiki_schedule"):
  79. logger.info("Starting wiki scheduler")
  80. tasks.load()
  81. t_scheduler = threading.Thread(target=wiki_scheduler)
  82. t_scheduler.name = "wiki-scheduler"
  83. t_scheduler.daemon = True
  84. t_scheduler.start()
  85. if config.components.get("irc_watcher"):
  86. logger.info("Starting IRC watcher")
  87. t_watcher = threading.Thread(target=irc_watcher, args=(frontend,))
  88. t_watcher.name = "irc-watcher"
  89. t_watcher.daemon = True
  90. t_watcher.start()
  91. frontend.loop()
  92. if "irc_watcher" in config.components:
  93. w_conn.close()
  94. f_conn.close()
  95. def main():
  96. if config.components.get("irc_frontend"):
  97. # Make the frontend run on our primary thread if enabled, and enable
  98. # additional components through that function:
  99. irc_frontend()
  100. elif config.components.get("wiki_schedule"):
  101. # Run the scheduler on the main thread, but also run the IRC watcher on
  102. # another thread iff it is enabled:
  103. logger.info("Starting wiki scheduler")
  104. tasks.load()
  105. if "irc_watcher" in enabled:
  106. logger.info("Starting IRC watcher")
  107. t_watcher = threading.Thread(target=irc_watcher)
  108. t_watcher.name = "irc-watcher"
  109. t_watcher.daemon = True
  110. t_watcher.start()
  111. wiki_scheduler()
  112. elif config.components.get("irc_watcher"):
  113. # The IRC watcher is our only enabled component, so run its function
  114. # only and don't worry about anything else:
  115. logger.info("Starting IRC watcher")
  116. irc_watcher()
  117. else: # Nothing is enabled!
  118. logger.critical("No bot parts are enabled; stopping")
  119. exit(1)