A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # Manage wiki tasks from IRC, and check on thread status.
  3. import threading
  4. import re
  5. from irc.classes import BaseCommand, Data, KwargParseException
  6. from wiki import task_manager
  7. from core import config
  8. class Tasks(BaseCommand):
  9. def get_hooks(self):
  10. return ["msg"]
  11. def get_help(self, command):
  12. return "Manage wiki tasks from IRC, and check on thread status."
  13. def check(self, data):
  14. if data.is_command and data.command in ["tasks", "threads", "tasklist"]:
  15. return True
  16. return False
  17. def process(self, data):
  18. self.data = data
  19. if data.host not in config.irc["permissions"]["owners"]:
  20. self.connection.reply(data, "at this time, you must be a bot owner to use this command.")
  21. return
  22. if not data.args:
  23. if data.command == "tasklist":
  24. self.do_list()
  25. else:
  26. self.connection.reply(data, "no arguments provided. Maybe you wanted '!{cmnd} list', '!{cmnd} start', or '!{cmnd} listall'?".format(cmnd=data.command))
  27. return
  28. if data.args[0] == "list":
  29. self.do_list()
  30. elif data.args[0] == "start":
  31. self.do_start()
  32. elif data.args[0] in ["listall", "all"]:
  33. self.do_listall()
  34. else: # they asked us to do something we don't know
  35. self.connection.reply(data, "unknown argument: \x0303{}\x0301.".format(data.args[0]))
  36. def do_list(self):
  37. threads = threading.enumerate()
  38. normal_threads = []
  39. task_threads = []
  40. for thread in threads:
  41. tname = thread.name
  42. if tname == "MainThread":
  43. tname = self.get_main_thread_name()
  44. normal_threads.append("\x0302{}\x0301 (as main thread, id {})".format(tname, thread.ident))
  45. elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]:
  46. normal_threads.append("\x0302{}\x0301 (id {})".format(tname, thread.ident))
  47. else:
  48. tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0]
  49. task_threads.append("\x0302{}\x0301 (id {}, since {})".format(tname, thread.ident, start_time))
  50. if task_threads:
  51. msg = "\x02{}\x0F threads active: {}, and \x02{}\x0F task threads: {}.".format(len(threads), ', '.join(normal_threads), len(task_threads), ', '.join(task_threads))
  52. else:
  53. msg = "\x02{}\x0F threads active: {}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads))
  54. self.connection.reply(self.data, msg)
  55. def do_listall(self):
  56. tasks = task_manager.task_list.keys()
  57. threads = threading.enumerate()
  58. tasklist = []
  59. tasks.sort()
  60. for task in tasks:
  61. threads_running_task = [t for t in threads if t.name.startswith(task)]
  62. ids = map(lambda t: str(t.ident), threads_running_task)
  63. if not ids:
  64. tasklist.append("\x0302{}\x0301 (idle)".format(task))
  65. elif len(ids) == 1:
  66. tasklist.append("\x0302{}\x0301 (\x02active\x0F as id {})".format(task, ids[0]))
  67. else:
  68. tasklist.append("\x0302{}\x0301 (\x02active\x0F as ids {})".format(task, ', '.join(ids)))
  69. tasklist = ", ".join(tasklist)
  70. msg = "{} tasks loaded: {}.".format(len(tasks), tasklist)
  71. self.connection.reply(self.data, msg)
  72. def do_start(self):
  73. data = self.data
  74. try:
  75. task_name = data.args[1]
  76. except IndexError: # no task name given
  77. self.connection.reply(data, "what task do you want me to start?")
  78. return
  79. try:
  80. data.parse_kwargs()
  81. except KwargParseException, arg:
  82. self.connection.reply(data, "error parsing argument: \x0303{}\x0301.".format(arg))
  83. return
  84. if task_name not in task_manager.task_list.keys(): # this task does not exist or hasn't been loaded
  85. self.connection.reply(data, "task could not be found; either wiki/tasks/{}.py doesn't exist, or it wasn't loaded correctly.".format(task_name))
  86. return
  87. task_manager.start_task(task_name, **data.kwargs)
  88. self.connection.reply(data, "task \x0302{}\x0301 started.".format(task_name))
  89. def get_main_thread_name(self):
  90. """Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher"."""
  91. if "irc_frontend" in config.components:
  92. return "irc-frontend"
  93. elif "wiki_schedule" in config.components:
  94. return "wiki-scheduler"
  95. else:
  96. return "irc-watcher"