使用者:CrowleyBot/task/10

源代碼

編輯
import mwclient as mwc
import os, sys, time, re
import itertools as its
import zhconv as zhc
from mainaccount import *
from pagename import *

wd = mwc.Site('www.wikidata.org', clients_useragent = UA)
zh = mwc.Site('zh.wiktionary.org', clients_useragent = UA)
en = mwc.Site('en.wiktionary.org', clients_useragent = UA)
wd.login(UN, PWD)
zh.login(UN, PWD)
en.login(UN, PWD)
SLP = 0
n = len(pn)
summary = 'link some pages between enwikt and zhwikt'

# 创建新项的情况
def process():
    p = pn[i]
    #zp = p.replace('Module:d', 'Module:D')
    zp = p
    data = '{"labels":{"zh":{"language":"zh","value":"%s"},"en":{"language":"en","value":"%s"}},"sitelinks":{"zhwiktionary":{"site":"zhwiktionary","title":"%s"},"enwiktionary":{"site":"enwiktionary","title":"%s"}}}' % (zp, p, zp, p)
    wd.post('wbeditentity', format='json', new='item', errorformat='plaintext', uselang='zh-hans', assertuser=UN, bot=1, token=wd.get_token('edit', True), data=data, summary=summary)
    time.sleep(SLP)

# 链接至已有项的情况
def process2():
    wp = 'Wikipedia:大量帳號建立者'
    tp = 'Wiktionary:账户创建者'
    wd.post('wbsetsitelink', site='zhwiki', title=wp, linksite='zhwiktionary', linktitle=tp, token=wd.get_token('edit', True), bot=1, summary='link somepages between zhwiki and zhwiktionary')
    time.sleep(SLP)

# 链接至zhwiki的情况
def process3():
    wp = 'Template:倾向中立'
    tp = 'Template:倾向中立'
    data = '{"labels":{"zh":{"language":"zh","value":"%s"}},"sitelinks":{"zhwiktionary":{"site":"zhwiktionary","title":"%s"},"zhwiki":{"site":"zhwiki","title":"%s"}}}' % (wp, tp, wp)
    wd.post('wbeditentity', format='json', new='item', errorformat='plaintext', uselang='zh-hans', assertuser=UN, bot=1, token=wd.get_token('edit', True), data=data, summary='link somepages between zhwiki and zhwiktionary')
    time.sleep(SLP)

# 智能链接
def process4():
    p = pn[i]
    zp = p
    try:
        _ = wd.post('wbgetentities', sites='enwiktionary', titles=p, props='info')['entities']['-1']
        data = '{"labels":{"zh":{"language":"zh","value":"%s"},"en":{"language":"en","value":"%s"}},"sitelinks":{"zhwiktionary":{"site":"zhwiktionary","title":"%s"},"enwiktionary":{"site":"enwiktionary","title":"%s"}}}' % (zp, p, zp, p)
        wd.post('wbeditentity', format='json', new='item', errorformat='plaintext', uselang='zh-hans', assertuser=UN, bot=1, token=wd.get_token('edit', True), data=data, summary=summary)
    except KeyError:
        wd.post('wbsetsitelink', site='enwiktionary', title=p, linksite='zhwiktionary', linktitle=zp, token=wd.get_token('edit', True), bot=1, summary=summary)
    except:
        print((i, pn[i]))
    time.sleep(SLP)

# 智能链接,加入说明,方言对应词地图
def process5():
    p = pn[i]
    zp = p
    zsp = zhc.convert(zp, 'zh-hans')
    base = p[24:]
    ztpd = "漢語方言對應詞地圖模塊:" + base
    zspd = zhc.convert(ztpd, 'zh-hans')
    pd = "Chinese dialectal equivalents module: " + base
    try:
        _ = wd.post('wbgetentities', sites='enwiktionary', titles=p, props='info')['entities']['-1']
        data = '{"labels":{"zh":{"language":"zh","value":"%s"},"zh-hans":{"language":"zh-hans","value":"%s"},"en":{"language":"en","value":"%s"}},"descriptions":{"zh-hans":{"language":"zh-hans","value":"%s"},"zh":{"language":"zh","value":"%s"},"en":{"language":"en","value":"%s"}},"sitelinks":{"zhwiktionary":{"site":"zhwiktionary","title":"%s"},"enwiktionary":{"site":"enwiktionary","title":"%s"}}}' % (zp, zsp, p, zspd, ztpd, pd, zp, p)
        wd.post('wbeditentity', format='json', new='item', errorformat='plaintext', uselang='zh-hans', assertuser=UN, bot=1, token=wd.get_token('edit', True), data=data, summary=summary)
    except KeyError:
        wd.post('wbsetsitelink', site='enwiktionary', title=p, linksite='zhwiktionary', linktitle=zp, token=wd.get_token('edit', True), bot=1, summary=summary)
    except:
        print((i, pn[i]))
    time.sleep(SLP)

# 智能链接,发音地图
def process6():
    p = pn[i]
    zp = p
    zsp = zhc.convert(zp, 'zh-hans')
    base = p[25:]
    ztpd = "漢語方言發音地圖模塊:" + base
    zspd = zhc.convert(ztpd, 'zh-hans')
    pd = "Chinese dialectal pronunciation data module: " + base
    try:
        _ = wd.post('wbgetentities', sites='enwiktionary', titles=p, props='info')['entities']['-1']
        data = '{"labels":{"zh":{"language":"zh","value":"%s"},"zh-hans":{"language":"zh-hans","value":"%s"},"en":{"language":"en","value":"%s"}},"descriptions":{"zh-hans":{"language":"zh-hans","value":"%s"},"zh":{"language":"zh","value":"%s"},"en":{"language":"en","value":"%s"}},"sitelinks":{"zhwiktionary":{"site":"zhwiktionary","title":"%s"},"enwiktionary":{"site":"enwiktionary","title":"%s"}}}' % (zp, zsp, p, zspd, ztpd, pd, zp, p)
        wd.post('wbeditentity', format='json', new='item', errorformat='plaintext', uselang='zh-hans', assertuser=UN, bot=1, token=wd.get_token('edit', True), data=data, summary=summary)
    except KeyError:
        wd.post('wbsetsitelink', site='enwiktionary', title=p, linksite='zhwiktionary', linktitle=zp, token=wd.get_token('edit', True), bot=1, summary=summary)
    except:
        print((i, pn[i]))
    time.sleep(SLP)