跳至內容

使用者:P-bot/blp.py

維基百科,自由的百科全書
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wikipedia, pagegenerators
import re, time, codecs
summary = u'机器人:添加{{Blp}}模板'
gfwlogtitle = 'e:\\pywiki\\fuckgfw\\log.txt'
genFactory = pagegenerators.GeneratorFactory()

def writegfwlog(title, other = ''):
    gfwlogfile = codecs.open(gfwlogtitle, 'a', 'utf-8')
    gfwlogfile.write(u'\n[[%s]]%s' % (title, other))
    gfwlogfile.close()

for arg in wikipedia.handleArgs():
    generator = genFactory.handleArg(arg)
    if generator:
        gen = generator
preloadingGen = pagegenerators.PreloadingGenerator(gen)
for page in preloadingGen:
    if page.namespace() == 0: # fuck GFW
    	if not re.search(u'(法[会會]|法[輪轮]|多[維维]|屠[殺杀]|[經经]文|邪[惡恶]|[鄧邓]力群|[趙赵]紫[陽阳]|六四|色情|[鮑鲍]彤|柴玲|封[从從]德|胡海清|王丹|[劉刘][曉晓]波|王炳章|[韓韩][東东]方|中[國国][勞劳]工[觀观]察[組组][織织]|[張张]戎|[刘劉]炳章|王[軍军][濤涛]|中[國国]共[产産][黨党]|[華华][国國][鋒锋]|民主?[进進]步?[黨党])', page.title()) == None:
    	    wikipedia.output(u'[[%s]]中存在GFW不能接受的关键词,自动跳过。' % page.title())
    	    writegfwlog(page.title(), u'(自动排除)')
    	    continue
        talktitle = 'talk:' + page.title()
        talkpage = wikipedia.Page(wikipedia.getSite(), talktitle)
        try:
            text = talkpage.get()
            if re.search('\{\{\s*([Bb]lp|BLP|生者[传傳][记記])\s*\}\}', text) == None:
                text = '{{Blp}}\n' + text
                talkpage.put(text, summary)
            else:
                wikipedia.output(u'条目[[%s]]的{{Blp}}模板已存在。' % page.title())
                continue
        except (wikipedia.NoPage, wikipedia.IsRedirectPage):
            try:
                text = '{{Blp}}'
                talkpage.put(text, summary)
            except KeyboardInterrupt:
                raise
            except: # fuck GFW
                wikipedia.output(u'[[%s]]中可能存在GFW不能接受的关键词,两分钟后将自动跳到下一个条目。' % page.title())
                writegfwlog(page.title())
                time.sleep(120)
                continue
        except KeyboardInterrupt:
            raise
        except: # fuck GFW
            wikipedia.output(u'[[%s]]中可能存在GFW不能接受的关键词,两分钟后将自动跳到下一个条目。' % page.title())
            writegfwlog(page.title())
            time.sleep(120)
            continue
    else:
        wikipedia.output(u'[[%s]]不是一个条目。' % page.title())
        continue