A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

main.py 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. frontend.startup(f_conn)
  31. t_watcher = threading.Thread(target=irc_watcher, args=(f_conn,))
  32. t_watcher.daemon = True
  33. t_watcher.start()
  34. frontend.main()
  35. w_conn.close()
  36. f_conn.close()
  37. if __name__ == "__main__":
  38. run()