commit e26c8ab3060fada34614850ec0ac686e1b4aab11
parent 991c20fe310edcac62574ad84ce05ebf0bdc5383
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Sat, 7 Apr 2012 18:11:05 +0200
fix bugs, simplify code
Diffstat:
README | | | 11 | ++++++----- |
wikifirc | | | 79 | ++++++++++++++++++++++++++++++++++--------------------------------------------- |
2 files changed, 40 insertions(+), 50 deletions(-)
diff --git a/README b/README
@@ -1,11 +1,12 @@
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.
+Run as wikifirc ADMIN [DUMP]. DUMP should be a file to dump the currently
+followed pages, it will be read at startup. 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 and added pages on
+standard error.
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
diff --git a/wikifirc b/wikifirc
@@ -1,4 +1,4 @@
-#!/usr/bin/python3
+#!/usr/bin/python3 -u
import sys
import time
@@ -25,6 +25,10 @@ class Line:
def __str__(self):
return self.data
+def register(pages, page):
+ pages.add(page)
+ print(page, file=sys.stderr)
+
# other languages should be added here
user_namespaces = [
'User:', 'User talk:',
@@ -34,60 +38,45 @@ user_namespaces = [
if __name__ == "__main__":
pages = set()
- exception = None
try:
- save = sys.argv[1]
- admin = sys.argv[2]
+ admin = sys.argv[1]
except IndexError:
- print ("Usage: %s DUMP ADMIN" % sys.argv[0])
+ print ("Usage: %s ADMIN [DUMP]" % sys.argv[0])
sys.exit(1)
try:
- f = open(save, 'r')
+ dump = sys.argv[2]
+ f = open(dump, 'r')
while True:
- l = f.readline()
- if not l:
+ line = f.readline()
+ if not line:
break
- pages.add(l.rstrip())
+ pages.add(line.rstrip())
f.close()
- except IOError:
+ except IndexError:
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)
+ 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
- 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)
+ new_user = ' '.join(fields)
+ for namespace in user_namespaces:
+ register(pages, 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:
+ register(pages, line.page)
+ if line.page in pages:
+ print(line)