wikifirc

filter irc.wikimedia.org on specific pages and users
git clone https://a3nm.net/git/wikifirc/
Log | Files | Refs | README

commit 991c20fe310edcac62574ad84ce05ebf0bdc5383
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sat,  7 Apr 2012 17:08:10 +0200

initial commit

Diffstat:
README | 15+++++++++++++++
wikifirc | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/README b/README @@ -0,0 +1,15 @@ +wikifirc -- filter irc.wikimedia.org on specific pages and users +Copyright (C) 2012 by Antoine Amarilli + +Run as wikifirc DUMP ADMIN. DUMP should be a file to dump the currently followed +pages, it will be read at startup if it exists and will be written on exit. +ADMIN should be the name of an administrator who will send commands to the +program. Events from irc.wikimedia.org should be provided in the irctk format on +standard input, the data of matching lines will be sent on standard output. + +The list of pages and users to follow is initially empty. ADMIN can send the +name of new users to follow as the body of a IRC message to the program. The +user and user talk pages of an user are followed. Whenever a followed user +modifies a page, the page is followed. The program will display modifications +that are performed on a followed page or performed by a followed user. + diff --git a/wikifirc b/wikifirc @@ -0,0 +1,93 @@ +#!/usr/bin/python3 + +import sys +import time + +class Line: + def __init__(self, project, data): + self.project = project + self.data = data + self.time = time.time() + page, sep, rest = data.partition(']]') + self.page = page[8:-3] + fields = rest.split(' ') + fields.pop(0) # trailing characters of title + self.flags = fields.pop(0)[:-2] + self.diff = fields.pop(0)[3:-1] + self.flags2 = fields.pop(0)[2:-1] + username, sep, rest = ' '.join(fields).partition('*') + self.username = username[3:-4] + fields = rest.split(' ') + fields.pop(0) # rest of flags3 + self.diff = fields.pop(0)[1:-1] + self.message = ' '.join(fields)[3:-1] + + def __str__(self): + return self.data + +# other languages should be added here +user_namespaces = [ + 'User:', 'User talk:', + 'Utilisateur:', 'Discussion utilisateur:', + ] + +if __name__ == "__main__": + + pages = set() + exception = None + + try: + save = sys.argv[1] + admin = sys.argv[2] + except IndexError: + print ("Usage: %s DUMP ADMIN" % sys.argv[0]) + sys.exit(1) + + try: + f = open(save, 'r') + while True: + l = f.readline() + if not l: + break + pages.add(l.rstrip()) + f.close() + except IOError: + pass + + try: + while True: + data = sys.stdin.readline() + if not data: + break + fields = data.strip().split() + project = fields.pop(0)[1:-1] + if not project.startswith('#'): + user = fields.pop(0)[1:-1] + if user != admin: + continue + new_user = fields.pop(0) + for namespace in user_namespaces: + pages.add(namespace + new_user) + continue + fields.pop(0) # bot username + data = ' '.join(fields) + line = Line(project, data) + # a user is followed if its user page is followed + if user_namespaces[0] + line.username in pages: + pages.add(line.page) + if line.page in pages: + print(line) + except Exception as e: + exception = e + raise e + + f = open(save, 'w') + for x in pages: + print(x, file=f) + f.close() + + if exception: + raise exception + + sys.exit(0) +