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.

93 wiersze
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  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(object):
  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(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z")
  28. re_log = re.compile("\A\[\[(.*?)\]\]\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. def __init__(self, chan, msg):
  32. self.chan = chan
  33. self.msg = msg
  34. def parse(self):
  35. """Parse a recent change event into some variables."""
  36. # Strip IRC color codes; we don't want or need 'em:
  37. self.msg = self.re_color.sub("", self.msg).strip()
  38. msg = self.msg
  39. self.is_edit = True
  40. # Flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user
  41. # creation log entry, etc:
  42. try:
  43. page, self.flags, url, user, comment = self.re_edit.findall(msg)[0]
  44. except IndexError:
  45. # We're probably missing the http:// part, because it's a log
  46. # entry, which lacks a URL:
  47. page, flags, user, comment = self.re_log.findall(msg)[0]
  48. url = "http://{0}.org/wiki/{1}".format(self.chan[1:], page)
  49. self.is_edit = False # This is a log entry, not edit
  50. # Flags tends to have extra whitespace at the end when they're
  51. # log entries:
  52. self.flags = flags.strip()
  53. self.page, self.url, self.user, self.comment = page, url, user, comment
  54. def prettify(self):
  55. """Make a nice, colorful message to send back to the IRC front-end."""
  56. flags = self.flags
  57. if "N" in flags:
  58. event_type = "page" # "New page:"
  59. elif flags == "delete":
  60. event_type = "deletion" # "New deletion:"
  61. elif flags == "protect":
  62. event_type = "protection" # "New protection:"
  63. elif flags == "create":
  64. event_type = "user" # "New user:"
  65. if self.page == "Special:Log/move":
  66. event_type = "move" # New move:
  67. else:
  68. event_type = "edit" # "New edit:"
  69. if "B" in flags:
  70. # "New bot edit:"
  71. event_type = "bot " + event_type
  72. if "M" in flags:
  73. # "New minor edit:" OR "New minor bot edit:"
  74. event_type = "minor " + event_type
  75. if self.is_edit:
  76. return self.pretty_edit.format(event_type, self.page, self.user,
  77. self.url, self.comment)
  78. else:
  79. return self.pretty_log.format(event_type, self.user, self.url,
  80. self.comment)