A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. ## EarwigBot's Core
  3. ## EarwigBot has three components that can run independently of each other: an
  4. ## IRC front-end, an IRC watcher, and a wiki scheduler.
  5. ## * The IRC front-end runs on a normal IRC server and expects users to
  6. ## interact with it/give it commands.
  7. ## * The IRC watcher runs on a wiki recent-changes server and listens for
  8. ## edits. Users cannot interact with this part of the bot.
  9. ## * The wiki scheduler runs wiki-editing bot tasks in separate threads at
  10. ## user-defined times through a cron-like interface.
  11. ## There is a "priority" system here:
  12. ## 1. If the IRC frontend is enabled, it will run on the main thread, and the
  13. ## IRC watcher and wiki scheduler (if enabled) will run on separate threads.
  14. ## 2. If the wiki scheduler is enabled, it will run on the main thread, and the
  15. ## IRC watcher (if enabled) will run on a separate thread.
  16. ## 3. If the IRC watcher is enabled, it will run on the main (and only) thread.
  17. ## Else, the bot will stop, as no components are enabled.
  18. import threading
  19. import time
  20. import traceback
  21. import sys
  22. import os
  23. parent_dir = os.path.split(sys.path[0])[0]
  24. sys.path.append(parent_dir) # make sure we look in the parent directory for modules
  25. from core import config
  26. from irc import frontend, watcher
  27. from wiki import task_manager
  28. f_conn = None
  29. w_conn = None
  30. def irc_watcher(f_conn):
  31. """Function to handle the IRC watcher as another thread (if frontend and/or
  32. scheduler is enabled), otherwise run as the main thread."""
  33. global w_conn
  34. print "\nStarting IRC watcher..."
  35. while 1: # restart the watcher component if (just) it breaks
  36. w_conn = watcher.get_connection()
  37. w_conn.connect()
  38. print # print a blank line here to signify that the bot has finished starting up
  39. try:
  40. watcher.main(w_conn, f_conn)
  41. except:
  42. traceback.print_exc()
  43. time.sleep(5) # sleep a bit before restarting watcher
  44. print "\nWatcher has stopped; restarting component..."
  45. def wiki_scheduler():
  46. """Function to handle the wiki scheduler as another thread, or as the
  47. primary thread if the IRC frontend is not enabled."""
  48. while 1:
  49. time_start = time.time()
  50. now = time.gmtime(time_start)
  51. task_manager.start_tasks(now)
  52. time_end = time.time()
  53. time_diff = time_start - time_end
  54. if time_diff < 60: # sleep until the next minute
  55. time.sleep(60 - time_diff)
  56. def irc_frontend():
  57. """If the IRC frontend is enabled, make it run on our primary thread, and
  58. enable the wiki scheduler and IRC watcher on new threads if they are
  59. enabled."""
  60. global f_conn
  61. print "\nStarting IRC frontend..."
  62. f_conn = frontend.get_connection()
  63. frontend.startup(f_conn)
  64. if enable_wiki_schedule:
  65. print "\nStarting wiki scheduler..."
  66. task_manager.load_tasks()
  67. t_scheduler = threading.Thread(target=wiki_scheduler)
  68. t_scheduler.name = "wiki-scheduler"
  69. t_scheduler.daemon = True
  70. t_scheduler.start()
  71. if enable_irc_watcher:
  72. t_watcher = threading.Thread(target=irc_watcher, args=(f_conn,))
  73. t_watcher.name = "irc-watcher"
  74. t_watcher.daemon = True
  75. t_watcher.start()
  76. frontend.main()
  77. if enable_irc_watcher:
  78. w_conn.close()
  79. f_conn.close()
  80. def run():
  81. config.load()
  82. components = config.config["main"]
  83. if components["enable_irc_frontend"]: # make the frontend run on our primary thread if enabled, and enable additional components through that function
  84. irc_frontend()
  85. elif components["enable_wiki_schedule"]: # the scheduler is enabled - run it on the main thread, but also run the IRC watcher on another thread if it is enabled
  86. print "\nStarting wiki scheduler..."
  87. task_manager.load_tasks()
  88. if enable_irc_watcher:
  89. t_watcher = threading.Thread(target=irc_watcher, args=(f_conn,))
  90. t_watcher.name = "irc-watcher"
  91. t_watcher.daemon = True
  92. t_watcher.start()
  93. wiki_scheduler()
  94. elif components["enable_irc_watcher"]: # the IRC watcher is our only enabled component, so run its function only and don't worry about anything else
  95. irc_watcher()
  96. else: # nothing is enabled!
  97. exit("\nNo bot parts are enabled; stopping...")
  98. if __name__ == "__main__":
  99. try:
  100. run()
  101. except KeyboardInterrupt:
  102. exit("\nKeyboardInterrupt: stopping main bot loop.")