wikifirc

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

commit 7541e2c181e44c1ddfd15fc77e8c8c9ea889305b
parent 3e5d5c56aa3c4df854fdd7e6d45c59f3bfb94640
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Mon, 11 Jun 2012 19:34:52 +0200

comment, add back shortening

Diffstat:
wikifirc | 36+++++++++++++++++++++++++-----------
1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/wikifirc b/wikifirc @@ -1,10 +1,27 @@ #!/usr/bin/python3 -u +"""Filter Wikipedia recent changes to specific users and pages""" + import sys import time import urllib.parse import urllib.request +API="http://minu.me/api.php?url=%s" + +def shorten(url): + return (urllib.request.urlopen(API % + urllib.parse.quote(url)).read() + ).decode('utf-8') + +# user and user talk namespaces for the various languages of interest +# add your language here if it isn't there +user_namespaces = [ + 'User:', 'User talk:', # en + 'Utilisateur:', 'Discussion utilisateur:', # fr + 'Gebruiker:', 'Overleg gebruiker:', # nl + ] + colors = { 'green': 3, 'red': 4, @@ -13,10 +30,12 @@ colors = { } def colorize(text, color): + """colorize for IRC, cf. http://irssi.org/documentation/formats""" return "\x03%d%s\x030" % (colors[color], text) -class Line: +class Change: def __init__(self, project, data): + """parse a change from a channel and irc line""" self.project = project self.data = data self.time = time.time() @@ -30,33 +49,28 @@ class Line: username, sep, rest = ' '.join(fields).partition('*') self.username = username[3:-4] fields = rest.split(' ') - fields.pop(0) # rest of flags3 + fields.pop(0) # rest of flags2 self.diffc = fields.pop(0)[1:-1] self.message = ' '.join(fields)[3:-1] def __str__(self): + """format the change to an irc line""" return ("<%s> [[%s]] %s %s \"%s\" %s" % ( colorize(self.username, 'green'), colorize(self.page, 'olive'), self.diffc, colorize(self.flags+self.flags2, 'red'), colorize(self.message, 'teal'), - self.diff, + shorten(self.diff), )) def register(pages, page): + """add a page to a set of pages and output it if not already present""" if page in pages: return pages.add(page) print(page, file=sys.stderr) -# other languages should be added here -user_namespaces = [ - 'User:', 'User talk:', - 'Utilisateur:', 'Discussion utilisateur:', - 'Gebruiker:', 'Overleg gebruiker:', - ] - if __name__ == "__main__": pages = set() @@ -106,7 +120,7 @@ if __name__ == "__main__": continue fields.pop(0) # bot username data = ' '.join(fields) - line = Line(project, data) + line = Change(project, data) # a user is followed if its user page is followed if user_namespaces[0] + line.username in pages: register(pages, line.page)