-
Notifications
You must be signed in to change notification settings - Fork 4
/
monitor.py
49 lines (37 loc) · 1.51 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# some code from Twisted Matrix's irc_test.py
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
import time, sys, re, datetime
CHANNEL = 'en.wikipedia' # use language.project
PARSE_EDIT_RE = re.compile(r'(\[\[(?P<page_title>.*?)\]\])'
r' +((?P<flags>[A-Z\!]+) )?'
r'(?P<url>\S*)'
r' +\* (?P<user>.*?)'
r' \* (\((?P<change_size>[\+\-][0-9]+)\))?'
r' ?(?P<summary>.+)?')
COLOR_RE = re.compile("\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE) # remove IRC color codes
def process_message(message):
no_color = COLOR_RE.sub('', message)
ret = PARSE_EDIT_RE.match(no_color)
if ret:
return ret.groupdict()
return {}
class Monitor(irc.IRCClient):
def connectionMade(self):
irc.IRCClient.connectionMade(self)
def signedOn(self):
self.join(self.factory.channel)
def privmsg(self, user, channel, msg):
print process_message(msg)
class MonitorFactory(protocol.ClientFactory):
def __init__(self, channel):
self.channel = channel
def buildProtocol(self, addr):
p = Monitor()
p.factory = self
return p
if __name__ == '__main__':
f = MonitorFactory(CHANNEL)
reactor.connectTCP("irc.wikimedia.org", 6667, f)
reactor.run()
#(\[\[(?P<page_title>.*?)\]\]) +((?P<flags>[A-Z\!]+) )?(?P<url>\S*) +\* (?P<user>.*?) \* (\((?P<change_size>[\+\-][0-9]+)\))? ?(?P<summary>.+)?