# -*- coding: utf-8 -*-
import pywikibot, subprocess
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'])
siteweb="www.metro-pole.net"
commentaire="Bot: Ajout du modèle lien brisé pour %s" % siteweb
site=pywikibot.getSite('fr')
i=0
for p in site.exturlusage(siteweb, namespaces=0):
texte= p.get()
texteoriginal=texte
lastpos=0
while True:
pos=texte.find('[http://'+siteweb, lastpos)
if pos<=0:
break
posfin=texte.find(']',pos)
textelien=texte[pos:posfin+1]
print textelien
posespace=textelien.find(' ')
url=textelien[1:posespace]
if posespace > 0:
titre=textelien[posespace+1:-1]
modele= "{{lien brisé|url=%s|titre=%s}}" % (url, titre)
else:
modele= "{{lien brisé|url=%s}}" % (url)
print textelien, modele
texte=texte.replace(textelien, modele)
lastpos=pos+1
if texte <> texteoriginal:
diff(texte, texteoriginal)
i+=1
print i, p.title()
p.put(texte, commentaire)
if i<10:
raw_input()