|
@@ -1,57 +1,73 @@ |
|
|
# -*- coding: utf-8 -*- |
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
# A class to store data on an individual event received from our IRC watcher. |
|
|
|
|
|
|
|
|
|
|
|
import re |
|
|
import re |
|
|
|
|
|
|
|
|
class RC(object): |
|
|
class RC(object): |
|
|
|
|
|
"""A class to store data on an event received from our IRC watcher.""" |
|
|
|
|
|
re_color = re.compile("\x03([0-9]{1,2}(,[0-9]{1,2})?)?") |
|
|
|
|
|
re_edit = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z") |
|
|
|
|
|
re_log = re.compile("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z") |
|
|
|
|
|
|
|
|
def __init__(self, msg): |
|
|
def __init__(self, msg): |
|
|
"""store data on an individual event received from our IRC watcher""" |
|
|
|
|
|
self.msg = msg |
|
|
self.msg = msg |
|
|
|
|
|
|
|
|
def parse(self): |
|
|
def parse(self): |
|
|
"""parse recent changes log into some variables""" |
|
|
|
|
|
|
|
|
"""Parse a recent change event into some variables.""" |
|
|
|
|
|
# Strip IRC color codes; we don't want or need 'em: |
|
|
|
|
|
self.msg = self.re_color.sub("", self.msg).strip() |
|
|
msg = self.msg |
|
|
msg = self.msg |
|
|
msg = re.sub("\x03([0-9]{1,2}(,[0-9]{1,2})?)?", "", msg) # strip IRC color codes; we don't want/need 'em |
|
|
|
|
|
msg = msg.strip() |
|
|
|
|
|
self.msg = msg |
|
|
|
|
|
self.is_edit = True |
|
|
self.is_edit = True |
|
|
|
|
|
|
|
|
# flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user creation log entry... |
|
|
|
|
|
|
|
|
# Flags: 'M' for minor edit, 'B' for bot edit, 'create' for a user |
|
|
|
|
|
# creation log entry, etc: |
|
|
try: |
|
|
try: |
|
|
page, flags, url, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s(http://.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0] |
|
|
|
|
|
except IndexError: # we're probably missing the http:// part, because it's a log entry, which lacks a url |
|
|
|
|
|
page, flags, user, comment = re.findall("\A\[\[(.*?)\]\]\s(.*?)\s\*\s(.*?)\s\*\s(.*?)\Z", msg)[0] |
|
|
|
|
|
url = "http://en.wikipedia.org/wiki/{}".format(page) |
|
|
|
|
|
flags = flags.strip() # flag tends to have a extraneous whitespace character at the end when it's a log entry |
|
|
|
|
|
self.is_edit = False # this is a log entry, not edit |
|
|
|
|
|
|
|
|
|
|
|
self.page, self.flags, self.url, self.user, self.comment = page, flags, url, user, comment |
|
|
|
|
|
|
|
|
|
|
|
def get_pretty(self): |
|
|
|
|
|
"""make a nice, colorful message from self.msg to send to the front-end""" |
|
|
|
|
|
|
|
|
page, self.flags, url, user, comment = self.re_edit.findall(msg)[0] |
|
|
|
|
|
except IndexError: |
|
|
|
|
|
# We're probably missing the http:// part, because it's a log |
|
|
|
|
|
# entry, which lacks a URL: |
|
|
|
|
|
page, flags, user, comment = self.re_log.findall(msg)[0] |
|
|
|
|
|
url = "".join(("http://en.wikipedia.org/wiki/", page)) |
|
|
|
|
|
|
|
|
|
|
|
self.is_edit = False # This is a log entry, not edit |
|
|
|
|
|
|
|
|
|
|
|
# Flags tends to have extra whitespace at the end when they're |
|
|
|
|
|
# log entries: |
|
|
|
|
|
self.flags = flags.strip() |
|
|
|
|
|
|
|
|
|
|
|
self.page, self.url, self.user, self.comment = page, url, user, comment |
|
|
|
|
|
|
|
|
|
|
|
def prettify(self): |
|
|
|
|
|
"""Make a nice, colorful message to send back to the IRC front-end.""" |
|
|
flags = self.flags |
|
|
flags = self.flags |
|
|
event_type = flags # "New <event>:" if we don't know exactly what happened |
|
|
|
|
|
|
|
|
# "New <event>:" if we don't know exactly what happened: |
|
|
|
|
|
event_type = flags |
|
|
if "N" in flags: |
|
|
if "N" in flags: |
|
|
event_type = "page" # "New page:" |
|
|
|
|
|
|
|
|
event_type = "page" # "New page:" |
|
|
elif flags == "delete": |
|
|
elif flags == "delete": |
|
|
event_type = "deletion" # "New deletion:" |
|
|
|
|
|
|
|
|
event_type = "deletion" # "New deletion:" |
|
|
elif flags == "protect": |
|
|
elif flags == "protect": |
|
|
event_type = "protection" # "New protection:" |
|
|
|
|
|
|
|
|
event_type = "protection" # "New protection:" |
|
|
elif flags == "create": |
|
|
elif flags == "create": |
|
|
event_type = "user" # "New user:" |
|
|
|
|
|
|
|
|
event_type = "user" # "New user:" |
|
|
if self.page == "Special:Log/move": |
|
|
if self.page == "Special:Log/move": |
|
|
event_type = "move" # New move: |
|
|
|
|
|
|
|
|
event_type = "move" # New move: |
|
|
else: |
|
|
else: |
|
|
event_type = "edit" # "New edit:" |
|
|
|
|
|
|
|
|
event_type = "edit" # "New edit:" |
|
|
if "B" in flags: |
|
|
if "B" in flags: |
|
|
event_type = "bot {}".format(event_type) # "New bot edit:" |
|
|
|
|
|
|
|
|
# "New bot edit:" |
|
|
|
|
|
event_type = "bot {}".format(event_type) |
|
|
if "M" in flags: |
|
|
if "M" in flags: |
|
|
event_type = "minor {}".format(event_type) # "New minor edit:" OR "New minor bot edit:" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# "New minor edit:" OR "New minor bot edit:" |
|
|
|
|
|
event_type = "minor {}".format(event_type) |
|
|
|
|
|
|
|
|
|
|
|
# Example formatting: |
|
|
|
|
|
# New edit: [[Page title]] * User name * http://en... * edit summary |
|
|
if self.is_edit: |
|
|
if self.is_edit: |
|
|
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) |
|
|
|
|
|
else: |
|
|
|
|
|
pretty = "\x02New {0}\x0F: \x0303{1}\x0306 *\x0302 {2}\x0306 *\x0310 {3}".format(event_type, self.user, self.url, self.comment) |
|
|
|
|
|
|
|
|
|
|
|
return pretty |
|
|
|
|
|
|
|
|
return "".join(("\x02New ", event_type, "\x0F: \x0314[[\x0307", |
|
|
|
|
|
self.page, "\x0314]]\x0306 *\x0303 ", self.user, |
|
|
|
|
|
"\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ", |
|
|
|
|
|
self.comment)) |
|
|
|
|
|
|
|
|
|
|
|
return "".join(("\x02New ", event_type, "\x0F: \x0303", self.user, |
|
|
|
|
|
"\x0306 *\x0302 ", self.url, "\x0306 *\x0310 ", |
|
|
|
|
|
self.comment)) |