Utente:BimBot/Scripts: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Il mio <tt>benvenuto.py</tt>: adesso mostra diff e chiede conferma
nuovo redirectlinks.py
Riga 1:
Copiate gente, copiate! (<small>Che poi va a fuoco il computer...</small>) :-D
 
== <tt>sistemaredirectredirectlinks.py</tt> v. 3.1 ==
<source lang="python">
<pre>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
 
#################################################################################################
# #
# Questo bot permette di modificare i link ad un redirect #
# redirectlinks.py #
# perche' puntino direttamente alla pagina giusta. #
# #
# Usa il framework pywikipedia. #
# This script corrects links to a redirect. #
################################################################
# #
# Syntax: redirectlinks.py [options] #
# Options: -dest:Destpage #
# It gets all redirects which link to Destpage and corrects their references. #
# #
# -page:Redirpage #
# It corrects all the refereces of the specified redirect. #
# #
# You must specify the -page OR the -dest option. #
# #
#################################################################################################
 
import wikipedia, urllib, re, syswikipedia
 
def main():
args = wikipedia.handleArgs()
for argh in args:
while 1:
if argh.startswith("-dest:"):
redirectLinks = dict()
destPage = wikipedia.Page(wikipedia.getSite(), argh[6:])
all = 0;
redirs = destPage.getReferences(redirectsOnly = True)
ic = inputchoice()
if argh.startswith("-page:"):
if ic == u'.':
redirs = [wikipedia.Page(wikipedia.getSite(), argh[6:])]
safeexit()
if not redirs[0].isRedirectPage():
if ic == u'P':
raise wikipedia.IsNotRedirectPage(redirs[0])
name = wikipedia.input(u'Inserisci il nome della pagina da cui ricavare i redirect [punto per terminare]:')
destPage = redirs[0].linkedPages()[0]
if name == u'.':
print destPage.title()
safeexit()
for r in redirs:
page = wikipedia.Page(wikipedia.getSite(), name)
redirRefPages = r.getReferences()
redirectLinks = getlinkstoredirectstopage(page)
for ref in redirRefPages:
else:
print "Correcting links in page: " + ref.title()
name = wikipedia.input(u'Inserisci il nome del redirect [punto per terminare]:')
ifoldtext name == u'ref.':get()
newtext = oldtext
safeexit()
linkRegexp = "\[\[" + r.title() + "(\|[^]]+)?\]\]"
page = wikipedia.Page(wikipedia.getSite(), name)
ptrn = re.compile(linkRegexp, re.IGNORECASE)
redirectLinks[page.title()] = page.getReferences()
linkMatch = re.search(ptrn, newtext)
for i in redirectLinks:
page = wikipedia.Page(wikipedia.getSite(), i)
while linkMatch != None:
dest = wikipedia.Page(wikipedia.getSite(), page.getRedirectTarget())
oldLink = newtext[linkMatch.start():linkMatch.end()]
ref = redirectLinks[i]
afterLink = linkMatch.group(1)
for j in ref:
oldtextif afterLink == j.get()None:
afterLink = ""
lnks = sgamalink(i, oldtext)
newLink = re.sub(linkRegexp, "[[" + destPage.title() + afterLink + "]]", oldLink)
for k in lnks:
choice = wikipedia.inputChoice("Replacing link " + oldLink + " to " + newLink + ".\nDo you want to change the link?", ["Accept", "Change"], ['a', 'c'], 'a')
print "\n"
if choice in ['C', 'c']:
wikipedia.output("Nome del redirect: " + page.aslink())
linkText = wikipedia.outputinput("PaginaInsert alink cui punta(without ilsquare redirectbrackets): [[" + destdestPage.aslinktitle())
newLink = "[[" + destPage.title() + linkText + "]]"
wikipedia.output("Link in via di correzione: " + k)
newtext = newtext[:linkMatch.start()] + newLink + newtext[linkMatch.end():]
wikipedia.output("Pagina che contiene il link: " + j.aslink())
linkMatch = re.search(ptrn, newtext)
print "\n"
if u'|' in k:
wikipedia.showDiff(oldtext, newtext)
regex = u"\[\[%s\|(.*?)\]\]" % i
choice = wikipedia.inputChoice(u'Modifico?', ['Yes', 'No'], ['y', 'N'], 'N')
p = re.compile(regex, re.IGNORECASE)
if choice in ['Y', 'y']:
dopolink = re.search(p, oldtext)
ref.put(newtext, str("Correcting links to redirect: " + r.title()))
choice = "[[" + dest.title() + "|" + dopolink.group(1) + "]]"
if __name__ == '__main__': main()
else:
</source>
tmptext = sistema(oldtext, i, u"<YOUR TEXT HERE>")
wikipedia.showDiff(oldtext, tmptext)
choice = linkchoice(dest.title(), i)
if choice == u'.':
safeexit()
newtext = sistema(oldtext, i, choice)
wikipedia.showDiff(oldtext, newtext)
choice = wikipedia.inputChoice('Posso procedere?', ['Yes', 'No'], ['y', 'N'], 'N')
if choice in ['Y', 'y']:
wikipedia.setAction(u'Correggo i link che puntano al redirect "%s"' % i)
j.put(newtext)
wikipedia.stopme();
 
def safeexit():
wikipedia.stopme()
sys.exit()
 
def getlinkstoredirectstopage(page):
try:
redirectLinks = dict()
ref = page.getReferences(redirectsOnly = True)
redirectList=list()
for i in ref:
redirectLinks[i.title()] = i.getReferences()
return redirectLinks
except wikipedia.NoPage:
print u'La pagina non esiste!'
return 0
 
def inputchoice():
choice = wikipedia.inputChoice(u"Vuoi inserire il nome di un redirect o di una pagina a cui linkano dei redirect? [punto per terminare]", [u'Redirect', u'Page', u'.'], [u'R', u'p', u'.'], u'R')
if choice == u'.':
return u'.'
elif choice in [u'P', u'p']:
return u'P'
else:
return u'R'
 
def linkchoice(newpage, oldpage):
scelte = [u"[[" + newpage + u"]]", u"[[" + newpage + u"|" + newpage[0].lower() + newpage[1:] + u"]]", u"[[" + newpage + u"|" + oldpage + u"]]", u"[[" + newpage + u"|" + oldpage[0].lower() + oldpage[1:] + u"]]", u'.']
sceltecopy = []
for scel in scelte:
sceltecopy.append(u"\n"+scel)
choice = wikipedia.inputChoice(u"Scegli una delle seguenti alternative per il link: [punto per terminare]", sceltecopy, [u'1', u'2', u'3', u'4', u'.'], u'1')
if choice == u'.':
return u'.'
return scelte[int(choice)-1] #[:len(scelte[int(choice)-1])-4]
 
def sgamalink(oldtitle, oldtext):
regex = u"\[\[%s.*?\]\]" % oldtitle
p = re.compile(regex, re.IGNORECASE)
lst = re.findall(p, oldtext)
return lst
 
def sistema(text, oldtitle, choice):
regex = u"\[\[%s.*?\]\]" % oldtitle
p = re.compile(regex, re.IGNORECASE)
text = re.sub(p, choice, text)
return text
 
if __name__==u"__main__":
main()
</pre>
 
== <tt>lonelypages.py</tt> ==
Line 122 ⟶ 71:
Prende le pagine da [[Speciale:Lonelypages]], verifica che siano veramente orfane e inserisce l'avviso in quelle che non lo hanno già.
 
<source lang="python">
<pre>
# -*- coding: utf-8 -*-
 
Line 151 ⟶ 100:
i.put(newtxt)
wikipedia.stopme()
</presource>
 
== Il mio <tt>benvenuto.py</tt> ==
Da [[Utente:Alfiobot/benvenuto.py]]
<source lang="python">
<pre>
######################################################
#
Line 234 ⟶ 183:
print u"User OK!"
wikipedia.stopme()
</presource>
 
== <tt>proxyaperti.py</tt> ==
Line 242 ⟶ 191:
Utilizzabile solo da personale addestrato ;-)
 
<source lang="python">
<pre>
#! -*- coding: utf-8 -*-
 
Line 257 ⟶ 206:
pagina.put(newtxt)
wikipedia.stopme()
</presource>