Utilisateur:NaggoBot/chine.py

Une page de Wikipédia, l'encyclopédie libre.
# -*- coding: utf-8 -*-
import pywikibot, subprocess, time
def diff(texte, texteorig):
    f1=open('/tmp/f1','w')
    f1.write(texteorig)
    f2=open('/tmp/f2','w')
    f2.write(texte)
    f1.close()
    f2.close()
    subprocess.call(['/usr/bin/diff','-C','1','/tmp/f1','/tmp/f2'])
    subprocess.call(['rm','/tmp/f1','/tmp/f2'])
i=0
site = pywikibot.getSite('fr')
rename='Empire chinois'
cible='Chine'
article='Civilisation chinoise'
commentaire='''Bot : remplacement de [%s|%s] par [%s|%s]''' % (article, rename, cible, rename)
articlel=article[0:1].lower()+article[1:]
print commentaire
print articlel, article
page=pywikibot.Page(site,article)
for ref in page.backlinks(namespaces=0, followRedirects=False):
    if ref.isRedirectPage():
        continue
    texte=ref.get()
    if ('[[%s|%s]]' % (article, rename) in texte) or ('[[%s|%s]]' % (articlel, rename) in texte):
        print ref
	texte_cible=texte.replace('[[%s|%s]]' % (article, rename),'[[%s|%s]]'% (cible, rename))
	texte_cible=texte_cible.replace('[[%s|%s]]' % (articlel, rename),'[[%s|%s]]'% (cible, rename))
        diff(texte, texte_cible)
	ref.put(texte_cible, commentaire)
	continue
        i+=1
        if i < 5:
            time.sleep(30)
        if i < 10:
            time.sleep(30)
    else:
        continue