Copiate gente, copiate! (Che poi va a fuoco il computer...) :-D

sistemaredirect.py v. 3.0

# -*- coding: utf-8  -*-

################################################################
# Questo bot permette di modificare i link ad un redirect      #
# perche' puntino direttamente alla pagina giusta.             #
# Usa il framework pywikipedia.                                #
################################################################

import urllib, re
import wikipedia

def main():
	args = wikipedia.handleArgs()
	while 1:
		redirectLinks = dict()
		all = 0;
		ic = inputchoice()
		if ic == u'.':
			safeexit()
		if ic == u'P':
			name = wikipedia.input(u'Inserisci il nome della pagina da cui ricavare i redirect [punto per terminare]:')
			if name == u'.':
				safeexit()
			page = wikipedia.Page(wikipedia.getSite(), name)
			redirectLinks = getlinkstoredirectstopage(page)
		else:
			name = wikipedia.input(u'Inserisci il nome del redirect [punto per terminare]:')
			if name == u'.':
				safeexit()
			page = wikipedia.Page(wikipedia.getSite(), name)
			redirectLinks[page.title()] = page.getReferences()
		for i in redirectLinks:
			page = wikipedia.Page(wikipedia.getSite(), i)
			dest = wikipedia.Page(wikipedia.getSite(), page.getRedirectTarget())
			ref = redirectLinks[i]
			for j in ref:
				oldtext = j.get()
				lnks = sgamalink(i, oldtext)
				for k in lnks:
					print "\n"
					print "Nome del redirect: " + page.aslink()
					print "Pagina a cui punta il redirect: " + dest.aslink()
					print "Link in via di correzione: " + k
					print "Pagina che contiene il link: " + j.aslink()
					print "\n"
					if u'|' in k:
						regex = u"\[\[%s\|(.*?)\]\]" % i
						p = re.compile(regex, re.IGNORECASE)
						dopolink = re.search(p, oldtext)
						choice = "[[" + dest.title() + "|" + dopolink.group(1) + "]]"
					else:
						choice = linkchoice(dest.title(), i)
						if choice == u'.':
							safeexit()
					newtext = sistema(oldtext, page.title(), 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"' % page.title())
						j.put(newtext)
	wikipedia.stopme();

def safeexit():
 wikipedia.stopme()
 exit()

def getlinkstoredirectstopage(page):
	try:
		redirectLinks = dict()
		ref = page.getReferences()
		redirectList=list()
		for i in ref:
			if i.isRedirectPage():
				redirectLinks[i.title()] = i.getReferences()
		return redirectLinks;
	except wikipedia.IsNotRedirectPage:
		print page.isRedirectPage()
		print u"Non e' un redirect!"
		return 0
	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 = scelte
 	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()

lonelypages.py

Prende le pagine da Speciale:Lonelypages, verifica che siano veramente orfane e inserisce l'avviso in quelle che non lo hanno già.

# -*- coding: utf-8 -*-

import wikipedia
import re
from pagegenerators import AllpagesPageGenerator

args = wikipedia.handleArgs()
wikiSite = wikipedia.getSite()
allpages = wikiSite.lonelypages()
for i in allpages:
 if i.isRedirectPage():
   continue
 refs = i.getReferences()
 refsList = list()
 for j in refs:
  refsList = refsList + [j]
 if len(refsList) == 0:
  regxp = ur'\{\{[Oo]rfan[oa]'
  oldtxt = i.get()
  if re.match(regxp, oldtxt) == None:
   newtxt = u'{{Orfana}}\n' + oldtxt
   print i
   wikipedia.showDiff(oldtxt, newtxt)
   choice = wikipedia.inputChoice(u'Pagina orfana non segnalata! Posso procedere?',  [u'Yes', u'No'], [u'y', u'N'], u'N')
   if choice in [u'Y', u'y']:
    wikipedia.setAction(u'Voce orfana')
    i.put(newtxt)
wikipedia.stopme()