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.

100 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2021 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import re
  23. __all__ = ["RC"]
  24. class RC:
  25. """Store data from an event received from our IRC watcher."""
  26. re_color = re.compile("\x03([0-9]{1,2}(,[0-9]{1,2})?)?")
  27. re_edit = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s(https?://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z")
  28. re_log = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s\s\*\s(.*?)\s\*\s(.*?)\Z")
  29. pretty_edit = "\x02New {0}\x0F: \x0314[[\x0307{1}\x0314]]\x0306 * \x0303{2}\x0306 * \x0302{3}\x0306 * \x0310{4}"
  30. pretty_log = "\x02New {0}\x0F: \x0303{1}\x0306 * \x0302{2}\x0306 * \x0310{3}"
  31. plain_edit = "New {0}: [[{1}]] * {2} * {3} * {4}"
  32. plain_log = "New {0}: {1} * {2} * {3}"
  33. def __init__(self, chan, msg):
  34. self.chan = chan
  35. self.msg = msg
  36. def __repr__(self):
  37. """Return the canonical string representation of the RC."""
  38. return "RC(chan={0!r}, msg={1!r})".format(self.chan, self.msg)
  39. def __str__(self):
  40. """Return a nice string representation of the RC."""
  41. return "<RC of {0!r} on {1}>".format(self.msg, self.chan)
  42. def parse(self):
  43. """Parse a recent change event into some variables."""
  44. # Strip IRC color codes; we don't want or need 'em:
  45. self.msg = self.re_color.sub("", self.msg).strip()
  46. msg = self.msg
  47. self.is_edit = True
  48. # Flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user
  49. # creation log entry, etc:
  50. try:
  51. page, self.flags, url, user, comment = self.re_edit.findall(msg)[0]
  52. except IndexError:
  53. # We're probably missing the https:// part, because it's a log
  54. # entry, which lacks a URL:
  55. page, flags, user, comment = self.re_log.findall(msg)[0]
  56. url = "https://{0}.org/wiki/{1}".format(self.chan[1:], page)
  57. self.is_edit = False # This is a log entry, not edit
  58. # Flags tends to have extra whitespace at the end when they're
  59. # log entries:
  60. self.flags = flags.strip()
  61. self.page, self.url, self.user, self.comment = page, url, user, comment
  62. def prettify(self, color=True):
  63. """Make a nice, colorful message to send back to the IRC front-end."""
  64. flags = self.flags
  65. if self.is_edit:
  66. if "N" in flags:
  67. event = "page" # "New page:"
  68. else:
  69. event = "edit" # "New edit:"
  70. if "B" in flags:
  71. event = "bot edit" # "New bot edit:"
  72. if "M" in flags:
  73. event = "minor " + event # "New minor (bot)? edit:"
  74. tmpl = self.pretty_edit if color else self.plain_edit
  75. return tmpl.format(event, self.page, self.user, self.url, self.comment)
  76. if flags == "delete":
  77. event = "deletion" # "New deletion:"
  78. elif flags == "protect":
  79. event = "protection" # "New protection:"
  80. elif flags == "create":
  81. event = "user" # "New user:"
  82. else:
  83. event = flags # Works for "move", "block", etc
  84. tmpl = self.pretty_log if color else self.plain_log
  85. return tmpl.format(event, self.user, self.url, self.comment)