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.

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