Created
October 15, 2010 13:43
-
-
Save edsu/628199 to your computer and use it in GitHub Desktop.
little irc bot that prints out live edits in wikipedia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# run this script and it will connect to irc://irc.wikimedia.org/en.wikipedia and | |
# print out live edits that are happening on wikipedia. | |
# | |
# you'll need to install python-irclib | |
import re | |
import sys | |
import irclib | |
import getpass | |
# now we have two problems, right? | |
MESSAGE_PATTERN = re.compile('\[\[(.+?)\]\] (.+)? (http:.+?)? (?:\* (.+?) \*)? (?:\(([+-]\d+)\))? (.+)?') | |
class WikipediaClient(irclib.SimpleIRCClient): | |
def on_error(self, connection, event): | |
print event | |
def on_created(self, connection, event): | |
connection.join("#en.wikipedia") | |
def on_pubmsg(self, connection, event): | |
msg = strip_color(event.arguments()[0]) | |
match = MESSAGE_PATTERN.search(msg) | |
if match: | |
page, status, diff_url, user, bytes_changed, msg = match.groups() | |
print "page: %s" % page | |
print "status: %s" % status | |
print "diff: %s" % diff_url | |
print "user: %s" % user | |
print "bytes: %s" % bytes_changed | |
print "msg: %s" % msg | |
else: | |
print "NO MATCH: %s" % msg | |
connection.close() | |
sys.exit(-1) | |
def strip_color(msg): | |
# should remove irc color coding | |
return re.sub(r"(\x03|\x02)([0-9]{1,2}(,[0-9]{1,2})?)?", "", msg) | |
def main(username): | |
irc = WikipediaClient() | |
irc.connect("irc.wikimedia.org", 6667, username) | |
irc.start() | |
if __name__ == "__main__": | |
username = getpass.getuser() | |
main(username) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment