User:Misza13/ace voter.py

From Wikipedia, the free encyclopedia

This script was developed for use in Arbitration Committee Elections December 2008.

To use it, you must customize your call to main(...) at the bottom, specifying:

votePrefix - common prefix for ACE voting pages
voteFile - name of file that contains your votes, in Username|+/- format, eg.:
Example|-
Jimbo Wales|+
Willy on Wheels|-
voteTexts - an array of two strings, to put as your vote (the # list numerator will be added automatically) - please customize especially these so you don't refer to my criteria

Code is released under the terms of the MIT license (make sure you understand the "WITHOUT WARRANTY OF ANY KIND" part).

#!/usr/bin/env python

import os, sys, re
sys.path.append(os.environ['HOME']+'/pywikipedia')
import wikipedia

def main(votePrefix, voteFile, voteTexts):
    L = open(voteFile,'r').readlines()
    L = [l.strip().split('|') for l in L]

    site = wikipedia.getSite()
    for vote in L:
        if vote[1] == '+':
            v = True
        elif vote[1] == '-':
            v = False
        else:
            continue
        votePage = wikipedia.Page(site, votePrefix + '/' + vote[0])
        txt = votePage.get()
        rx = re.search('^(.*)(\n== *Support *==\n.*[^\n])(\n+== *Oppose *==.*[^\n])(\n*)', txt, re.DOTALL)
        if rx:
            if v:
                newtxt = rx.group(1) + rx.group(2) + '\n# ' + voteTexts[v] + rx.group(3) + rx.group(4)
            else:
                newtxt = rx.group(1) + rx.group(2) + rx.group(3) + '\n# ' + voteTexts[v] + rx.group(4)
            votePage.put(
                newtext=newtxt,
                 comment='Voting '+['oppose','support'][v]+' using [[User:Misza13/Scripts/ace_voter.py|ace_voter.py]].',
                 watchArticle=v,
                 minorEdit=False,
                 force=True)

if __name__ == '__main__':
    try:
        main(
            'Wikipedia:Arbitration Committee Elections December 2008/Vote',
            'votes.txt',
            ["'''[[User:Misza13/ACE 2008|Oppose]]'''. ~~~~","'''[[User:Misza13/ACE 2008|Support]]'''. ~~~~"])
    finally:
        wikipedia.stopme()