A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

96 linhas
4.0 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. """A class to store data on 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. def __init__(self, msg):
  30. self.msg = msg
  31. def parse(self):
  32. """Parse a recent change event into some variables."""
  33. # Strip IRC color codes; we don't want or need 'em:
  34. self.msg = self.re_color.sub("", self.msg).strip()
  35. msg = self.msg
  36. self.is_edit = True
  37. # Flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user
  38. # creation log entry, etc:
  39. try:
  40. page, self.flags, url, user, comment = self.re_edit.findall(msg)[0]
  41. except IndexError:
  42. # We're probably missing the http:// part, because it's a log
  43. # entry, which lacks a URL:
  44. page, flags, user, comment = self.re_log.findall(msg)[0]
  45. url = "".join(("http://en.wikipedia.org/wiki/", page))
  46. self.is_edit = False # This is a log entry, not edit
  47. # Flags tends to have extra whitespace at the end when they're
  48. # log entries:
  49. self.flags = flags.strip()
  50. self.page, self.url, self.user, self.comment = page, url, user, comment
  51. def prettify(self):
  52. """Make a nice, colorful message to send back to the IRC front-end."""
  53. flags = self.flags
  54. # "New <event>:" if we don't know exactly what happened:
  55. event_type = flags
  56. if "N" in flags:
  57. event_type = "page" # "New page:"
  58. elif flags == "delete":
  59. event_type = "deletion" # "New deletion:"
  60. elif flags == "protect":
  61. event_type = "protection" # "New protection:"
  62. elif flags == "create":
  63. event_type = "user" # "New user:"
  64. if self.page == "Special:Log/move":
  65. event_type = "move" # New move:
  66. else:
  67. event_type = "edit" # "New edit:"
  68. if "B" in flags:
  69. # "New bot edit:"
  70. event_type = "bot {}".format(event_type)
  71. if "M" in flags:
  72. # "New minor edit:" OR "New minor bot edit:"
  73. event_type = "minor {}".format(event_type)
  74. # Example formatting:
  75. # New edit: [[Page title]] * User name * http://en... * edit summary
  76. if self.is_edit:
  77. return "".join(("\x02New ", event_type, "\x0F: \x0314[[\x0307",
  78. self.page, "\x0314]]\x0306 *\x0303 ", self.user,
  79. "\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ",
  80. self.comment))
  81. return "".join(("\x02New ", event_type, "\x0F: \x0303", self.user,
  82. "\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ",
  83. self.comment))