User:LittleBot/stubbot.py

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

import wikipedia
import login,catlib
import pagegenerators
import sys
import re

def workon(page,stub_template = u"小作品", stub_redirect_template=[u"stub"], stub_category_name=u"Category:小作品"):
    replace_text = u""
    template_status = 0
    
    listTemplates = page.templates()
   
    # Avoid mistake
    stub_template = stub_template[0].upper() + stub_template[1:]

    for template_name in listTemplates:
       if template_name == stub_template:
             template_status = 2

       stub_template_lower = stub_template[0].lower() + stub_template[1:]
       if template_name == stub_template_lower:
             # Pass, for convince
	     template_status = 2

       for stub_redirect_name in stub_redirect_template:
             reTemplate = re.compile(u'[' + stub_redirect_name[0].upper() + stub_redirect_name[0].lower() + ']' + stub_redirect_name[1:] + '$')
             if reTemplate.search(template_name):
                 # Pass, for convince
                 template_status = 2

    try:
        # Get the page content
        text = page.get()
    except wikipedia.IsRedirectPage:
        # Oh no, this is a redirect page :(
        wikipedia.output(u"< SKIP > [[%s]]: Oh no, this is a redirect page :("%page.title())
        return

    # Remove interwiki links for a while
    if not mysite.lang in mysite.family.categories_last:
       interwikiLinks = wikipedia.getLanguageLinks(text, insite = mysite)
       text = wikipedia.removeLanguageLinks(text, site = mysite)

    # Get and remove category links
    categoryList = page.categories()
    text = wikipedia.removeCategoryLinks(text, site = mysite)

    # If there's no template:
    if template_status==0:
        mySummary = u"Robot: 新增小作品模板、整理"
        wikipedia.output(u"<CASE:0> [[%s]]: Will ADD stub template and arrange"%page.title())

        # Add Template
	text = text + u"\n{{" + stub_template +"}}\n"

    # If template exist, but not appropiate:
    elif template_status==1:
        mySummary = u"Robot: 更改小作品模板、整理"
        RtemplateOld = re.compile(u'{{(msg:)?'+ replace_text +'(\|(.+?))?}}', re.DOTALL)
        text = RtemplateOld.sub("{{" + stub_template +"}}",text)
        wikipedia.output(u"<CASE:1> [[%s]]: Will CHANGE stub template and arrange"%page.title())
    else:
        mySummary = u"Robot: 整理小作品"
        wikipedia.output(u"<CASE:2> [[%s]]: Will ARRANGE only"%page.title())

    # Arrange Category
    for categoryItem in categoryList:
        if categoryItem.title() == stub_category_name:
            categoryList.remove(categoryItem)
        
    text = wikipedia.replaceCategoryLinks(text ,categoryList, site = mysite)
        
    # Re-add interwiki links
    text = wikipedia.replaceLanguageLinks(text, interwikiLinks, site = mysite)

    # 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/測試區2')
    workon(page,u"Bio-stub",[u"人物小作品",u"People-stub"],u"Category:人物小作品")


finally:
    wikipedia.stopme()