A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

main.py 1.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. ## EarwigBot's Core
  3. ## Basically, this creates threads for our IRC watcher component and Wikipedia component, and then runs the main IRC bot on the main thread.
  4. ## The IRC bot component of EarwigBot has two parts: a front-end and a watcher.
  5. ## The front-end runs on a normal IRC server and expects users to interact with it/give it commands.
  6. ## The watcher runs on a wiki recent-changes server and listens for edits. Users cannot interact with this part of the bot.
  7. import threading
  8. import time
  9. import traceback
  10. import sys
  11. import os
  12. parent_dir = os.path.split(sys.path[0])[0]
  13. sys.path.append(parent_dir) # make sure we look in the parent directory for modules
  14. from irc import frontend, watcher
  15. f_conn = None
  16. w_conn = None
  17. def irc_watcher(f_conn):
  18. global w_conn
  19. while 1: # restart the watcher component if (just) it breaks
  20. w_conn = watcher.get_connection()
  21. try:
  22. watcher.main(w_conn, f_conn)
  23. except:
  24. traceback.print_exc()
  25. time.sleep(5) # sleep a bit before restarting watcher
  26. print "watcher has stopped; restarting component..."
  27. def run():
  28. global f_conn
  29. f_conn = frontend.get_connection()
  30. t_watcher = threading.Thread(target=irc_watcher, args=(f_conn,))
  31. t_watcher.daemon = True
  32. t_watcher.start()
  33. frontend.main(f_conn)
  34. w_conn.close()
  35. f_conn.close()
  36. if __name__ == "__main__":
  37. run()