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.

rc.py 2.6 KiB

13 lat temu
13 lat temu
13 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. # flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user creation log entry...
  16. try:
  17. page, flags, url, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0]
  18. except IndexError: # we're probably missing the http:// part, because it's a log entry, which lacks a url
  19. page, flags, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0]
  20. url = "http://en.wikipedia.org/wiki/{}".format(page)
  21. flags = flags.strip() # flag tends to have a extraneous whitespace character at the end when it's a log entry
  22. self.is_edit = False # this is a log entry, not edit
  23. self.page, self.flags, self.url, self.user, self.comment = page, flags, url, user, comment
  24. def get_pretty(self):
  25. """make a nice, colorful message from self.msg to send to the front-end"""
  26. flags = self.flags
  27. event_type = flags # "New <event>:" if we don't know exactly what happened
  28. if "N" in flags:
  29. event_type = "page" # "New page:"
  30. elif flags == "delete":
  31. event_type = "deletion" # "New deletion:"
  32. elif flags == "protect":
  33. event_type = "protection" # "New protection:"
  34. elif flags == "create":
  35. event_type = "user" # "New user:"
  36. if page = "Special:Log/move":
  37. event_type = "move" # New move:
  38. else:
  39. event_type = "edit" # "New edit:"
  40. if "B" in flags:
  41. event_type = "bot {}".format(event_type) # "New bot edit:"
  42. if "M" in flags:
  43. event_type = "minor {}".format(event_type) # "New minor edit:" OR "New minor bot edit:"
  44. if self.is_edit:
  45. 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)
  46. else:
  47. pretty = "\x02New {0}\x0F: \x0303{1}\x0306 *\x0302 {2}\x0306 *\x0310 {3}".format(event_type, self.user, self.url, self.comment)
  48. return pretty