跳转到内容

英文维基 | 中文维基 | 日文维基 | 草榴社区

User:P-bot/简繁重定向

维基百科,自由的百科全书

Xie Yanbo提供的繁简转换程序致以谢意。

# -*- coding: utf-8  -*-
# Filename: sc_redirect.py

import wikipedia, pagegenerators
from langconv import *
from movepages import *

def main():
    gen = None
    prefix = None
    oldTitle = None
    hansTitle = None
    hantTitle = None
    always = False
    summary = None
    tempPageTitle = None
    # This factory is responsible for processing command line arguments
    # that are also used by other scripts and that determine on which pages
    # to work on.
    genFactory = pagegenerators.GeneratorFactory()

    for arg in wikipedia.handleArgs():
        if arg == '-always':
            always = True
        if arg.startswith('-page:'):
            oldTitle = arg[len('-page:'):]
        elif arg.startswith('-prefix'):
            if len(arg) == len('-prefix'):
                prefix = wikipedia.input(u'Enter the prefix:')
            else:
                prefix = arg[8:]
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = wikipedia.input(u'Enter the summary:')
            else:
                summary = arg[9:]
        else:
            generator = genFactory.handleArg(arg)
            if generator:
                gen = generator

    zh_hans = Converter('zh-hans')
    zh_hant = Converter('zh-hant')
    msg = u"机器人:简繁重定向"
    if oldTitle:
        hansTitle = zh_hans.convert(oldTitle)
        hantTitle = zh_hant.convert(oldTitle)
        oldPage = wikipedia.Page(wikipedia.getSite(), oldTitle)
        if oldTitle != hansTitle:
            newPage = wikipedia.Page(wikipedia.getSite(), hansTitle)
            if oldPage.exists() and not newPage.exists():
            	text = u'#REDIRECT [[' + oldTitle + u']]'
                newPage.put(text, comment=msg, force=False)
                wikipedia.output(u'页面[[%s]]的简繁重定向已成功完成。' % oldTitle)
        elif oldTitle != hantTitle:
            newPage = wikipedia.Page(wikipedia.getSite(), hantTitle)
            if oldPage.exists() and not newPage.exists():
            	text = u'#REDIRECT [[' + oldTitle + u']]'
                newPage.put(text, comment=msg, force=False)
                wikipedia.output(u'页面[[%s]]的简繁重定向已成功完成。' % oldTitle)
        else:
            wikipedia.output(u'页面[[%s]]不需要简繁重定向。' % oldTitle)
    elif gen:
        preloadingGen = pagegenerators.PreloadingGenerator(gen)
        for page in preloadingGen:
            oldTitle = page.title()
            hansTitle = zh_hans.convert(oldTitle)
            hantTitle = zh_hant.convert(oldTitle)
            oldPage = wikipedia.Page(wikipedia.getSite(), oldTitle)
            if oldTitle != hansTitle:
                newPage = wikipedia.Page(wikipedia.getSite(), hansTitle)
                if oldPage.exists() and not newPage.exists():
                    text = u'#REDIRECT [[' + oldTitle + u']]'
                    newPage.put(text, comment=msg, force=False)
                    wikipedia.output(u'页面[[%s]]的简繁重定向已成功完成。' % oldTitle)
            elif oldTitle != hantTitle:
                newPage = wikipedia.Page(wikipedia.getSite(), hantTitle)
                if oldPage.exists() and not newPage.exists():
                    text = u'#REDIRECT [[' + oldTitle + u']]'
                    newPage.put(text, comment=msg, force=False)
                    wikipedia.output(u'页面[[%s]]的简繁重定向已成功完成。' % oldTitle)
            else:
                wikipedia.output(u'页面[[%s]]不需要简繁重定向。' % oldTitle)

if __name__ == '__main__':
    try:
        main()
    finally:
        wikipedia.stopme()