User:LittleBot/peerreviewbot.py

#!/usr/bin/python
# -*- coding: utf-8  -*-

import wikipedia
import login
import pagegenerators
import sys
import re

def workon(page):
    replace_text = u""

    listTemplates = page.templates()
    if "Peerreview" in listTemplates:
       replace_text = u"Peerreview"
    if "peerreview" in listTemplates:
       replace_text = u"peerreview"
    if "Oldpeerreview" in listTemplates or "oldpeerreview" in listTemplates:
       wikipedia.output(u"<FAILED> [[%s]]: Oh, Somebody done it."%page.title())   
       return

    try:
        # Get the page content
        text = page.get()
    except wikipedia.IsRedirectPage:
        # Oh no, this is a redirect page :(
        wikipedia.output(u"<FAILED> [[%s]]: Oh no, this is a redirect page :("%page.title())
        return
    if replace_text=="":
        mySummary = u"Robot: 新增舊的同行評審提示"
        wikipedia.output(u"I cannot found Template Peerreview used. Oh, I will add Oldpeerreview!")
        text = u"{{Oldpeerreview}}\n" + text
    else:
        mySummary = u"Robot: 更改同行評審提示為舊的同行評審"
        wikipedia.output(u"I found Template used: %s\nOh, I will replace it with Oldpeerreview!"%replace_text)
        RtemplateOld = re.compile(u'{{(msg:)?'+ replace_text +'(\|(.+?))?}}', re.DOTALL)
        text = RtemplateOld.sub("{{Oldpeerreview}}",text)
        

    # Here go edit text in whatever way you want. If you find you do not
    # want to edit this page, just return
    
    if text != page.get():
        page.put(text,mySummary) # Adding a summary text would be good
        wikipedia.output(u"<  OK  > [[%s]]: OK!"%page.title())
        if test:
            wikipedia.input(u"Changed [[%s]]. Press enter to continue."%page.title())

try:
    start = []
    test = False
    for arg in wikipedia.handleArgs():
        if arg.startswith("-test"):
            test = True
        else:
            start.append(arg)
    if start:
        start = " ".join(start)
    else:
        start = "!"
    
    # Start
    wikipedia.output(u"這是笨笨的小B所寫的同行評審處理專門程式。由DoggyBot負責執行。")

    # Connect to Chinese Wikipedia, then login
    mysite = wikipedia.Site('zh')
    login.LoginManager('mh1810sd', False, mysite)
    
    # Here We Go!
    page=wikipedia.Page(mysite,u'User:DoggyBot/測試區1')
    workon(page)

finally:
    wikipedia.stopme()