Python:Rcbot.py

From Botwiki

Jump to: navigation, search

This is a simple IRC recent changes bot that works perfectly on all wikimedia wikis. You can change «print data» with another command to begin scanning the change.

As it is configured now, it will work with en.wikipedia, en.wiktionary and meta.wikimedia; of course you can change these values by changing the variable channels.

######## IRC CONFIGURATION ########
server = 'irc.wikimedia.org'
port = 6667
nickname = 'rc'
channels = ['en.wikipedia', 'en.wiktionary', 'meta.wikimedia']
###################################
 
import re
regexps = [re.compile(r':.*? PRIVMSG (?P<channel>.*) :\x0314\[\[\x0307(?P<title>.*?)\x0314\]\]\x034 (?P<flags>.*?)\x0310 \x0302http:\/\/.*?\/w\/index\.php\?title=.*?&diff=(?P<diff>[0-9]*)&oldid=(?P<oldid>[0-9]*)(&rcid=(?P<rcid>[0-9]*))?\x03 \x035\*\x03 \x0303(?P<user>.*?)\x03 \x035\*\x03 \(\x02?(?P<diffsize>[+-][0-9]*)\x02?\) \x0310(?P<comment>.*)\x03\r\n'), re.compile(r':.*? PRIVMSG (?P<channel>.*) :\x0314\[\[\x0307(?P<title>.*?)\x0314\]\]\x034 (?P<flags>.*?N)\x0310 \x0302http:\/\/.*?\/w\/index\.php\?title=.*?&rcid=(?P<rcid>[0-9]*)\x03 \x035\*\x03 \x0303(?P<user>.*?)\x03 \x035\*\x03 \(\x02?(?P<diffsize>[+-][0-9]*)\x02?\) \x0310(?P<comment>.*)\x03\r\n'), re.compile(r':.*? PRIVMSG (?P<channel>.*) :\x0314\[\[\x0307(?P<title>.*?)\x0314\]\]\x034 (?P<flags>.*?N)\x0310 \x0302http:\/\/.*?\/wiki/.*?\x03 \x035\*\x03 \x0303(?P<user>.*?)\x03 \x035\*\x03 \(\x02?(?P<diffsize>[+-][0-9]*)\x02?\) \x0310(?P<comment>.*)\x03\r\n'), re.compile(r':.*? PRIVMSG (?P<channel>.*) :\x0314\[\[\x0307(?P<title>.*?)\x0314\]\]\x034 (?P<flags>.*?)\x0310 \x0302\x03 \x035\*\x03 \x0303(?P<user>.*?)\x03 \x035\*\x03  \x0310(?P<comment>.*)\x03\r\n')]
 
import socket, random
def rcbot():
	rc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	rc.connect((str(server), int(port)))
	rc.recv(4096)
	def identify():
		rand = str(random.randint(120, 9999))
		nick = '%s%s' %(nickname[0:9-len(rand)], rand)
		rc.send('NICK %s\r\n' %nick)
		rc.send('USER %s %s %s :%s\r\n' %(nick, nick, nick, nick))
		for channel in channels:
			if not str(channel)[0] in '&#!+':
				channel = '#%s' %channel
			rc.send('JOIN %s\r\n' %channel)
		return nick
	nick = identify()
	while True:
		try:
			d = rc.recv(4096)
			if 'PING' in d:
				rc.send('PONG ' + d.split()[1] + '\r\n')
			elif d.endswith('433 * %s :Nickname is already in use.\r\n' %nick):
				nick = identify()
			elif d == '':
				try:
					rc.send('QUIT\r\n')
				except socket.error:
					pass
				rcbot()
				return
			d = d.decode('utf-8', 'replace')
			for r in regexps:
				m = r.match(d)
				if m:
					data = m.groupdict()
					print data
					break
		except KeyboardInterrupt:
			rc.send('QUIT\r\n')
			return
 
if __name__ == '__main__':
	rcbot()
Personal tools