A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

57 rindas
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # A class to store data on an individual event received from our IRC watcher.
  3. import re
  4. class RC(object):
  5. def __init__(self, msg):
  6. """store data on an individual event received from our IRC watcher"""
  7. self.msg = msg
  8. def parse(self):
  9. """parse recent changes log into some variables"""
  10. msg = self.msg
  11. msg = re.sub("\x03([0-9]{1,2}(,[0-9]{1,2})?)?", "", msg) # strip IRC color codes; we don't want/need 'em
  12. msg = msg.strip()
  13. self.msg = msg
  14. self.is_edit = True
  15. # page name of the modified page
  16. # 'M' for minor edit, 'B' for bot edit, 'create' for a user creation log entry...
  17. try:
  18. page, flags, url, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0]
  19. except IndexError: # we're probably missing the http:// part, because it's a log entry, which lacks a url
  20. page, flags, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0]
  21. url = "http://en.wikipedia.org/wiki/{}".format(page)
  22. flags = flags.strip() # flag tends to have a extraneous whitespace character at the end when it's a log entry
  23. self.is_edit = False # this is a log entry, not edit
  24. self.page, self.flags, self.url, self.user, self.comment = page, flags, url, user, comment
  25. def get_pretty(self):
  26. """make a nice, colorful message from self.msg to send to the front-end"""
  27. flags = self.flags
  28. event_type = flags # "New <event>:" if we don't know exactly what happened
  29. if "N" in flags:
  30. event_type = "page" # "New page:"
  31. elif flags == "delete":
  32. event_type = "deletion" # "New deletion:"
  33. elif flags == "protect":
  34. event_type = "protection" # "New protection:"
  35. elif flags == "create":
  36. event_type = "user" # "New user:"
  37. else:
  38. event_type = "edit" # "New edit:"
  39. if "B" in flags:
  40. event_type = "bot {}".format(event_type) # "New bot edit:"
  41. if "M" in flags:
  42. event_type = "minor {}".format(event_type) # "New minor edit:" OR "New minor bot edit:"
  43. if self.is_edit:
  44. pretty = "\x02New {0}\x0F: \x0314[[\x0307{1}\x0314]]\x0306 *\x0303 {2}\x0306 *\x0302 {3}\x0306 *\x0310 {4}".format(event_type, self.page, self.user, self.url, self.comment)
  45. else:
  46. pretty = "\x02New {0}\x0F: \x0303{1}\x0306 *\x0302 {2}\x0306 *\x0310 {3}".format(event_type, self.user, self.url, self.comment)
  47. return pretty