A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

rc.py 2.9 KiB

13年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. import re
  3. class RC(object):
  4. """A class to store data on an event received from our IRC watcher."""
  5. re_color = re.compile("\x03([0-9]{1,2}(,[0-9]{1,2})?)?")
  6. re_edit = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z")
  7. re_log = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z")
  8. def __init__(self, msg):
  9. self.msg = msg
  10. def parse(self):
  11. """Parse a recent change event into some variables."""
  12. # Strip IRC color codes; we don't want or need 'em:
  13. self.msg = self.re_color.sub("", self.msg).strip()
  14. msg = self.msg
  15. self.is_edit = True
  16. # Flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user
  17. # creation log entry, etc:
  18. try:
  19. page, self.flags, url, user, comment = self.re_edit.findall(msg)[0]
  20. except IndexError:
  21. # We're probably missing the http:// part, because it's a log
  22. # entry, which lacks a URL:
  23. page, flags, user, comment = self.re_log.findall(msg)[0]
  24. url = "".join(("http://en.wikipedia.org/wiki/", page))
  25. self.is_edit = False # This is a log entry, not edit
  26. # Flags tends to have extra whitespace at the end when they're
  27. # log entries:
  28. self.flags = flags.strip()
  29. self.page, self.url, self.user, self.comment = page, url, user, comment
  30. def prettify(self):
  31. """Make a nice, colorful message to send back to the IRC front-end."""
  32. flags = self.flags
  33. # "New <event>:" if we don't know exactly what happened:
  34. event_type = flags
  35. if "N" in flags:
  36. event_type = "page" # "New page:"
  37. elif flags == "delete":
  38. event_type = "deletion" # "New deletion:"
  39. elif flags == "protect":
  40. event_type = "protection" # "New protection:"
  41. elif flags == "create":
  42. event_type = "user" # "New user:"
  43. if self.page == "Special:Log/move":
  44. event_type = "move" # New move:
  45. else:
  46. event_type = "edit" # "New edit:"
  47. if "B" in flags:
  48. # "New bot edit:"
  49. event_type = "bot {}".format(event_type)
  50. if "M" in flags:
  51. # "New minor edit:" OR "New minor bot edit:"
  52. event_type = "minor {}".format(event_type)
  53. # Example formatting:
  54. # New edit: [[Page title]] * User name * http://en... * edit summary
  55. if self.is_edit:
  56. return "".join(("\x02New ", event_type, "\x0F: \x0314[[\x0307",
  57. self.page, "\x0314]]\x0306 *\x0303 ", self.user,
  58. "\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ",
  59. self.comment))
  60. return "".join(("\x02New ", event_type, "\x0F: \x0303", self.user,
  61. "\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ",
  62. self.comment))