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

sistemaredirect.py v. 3.0 modifica

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

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

import wikipedia, urllib, re, sys

def main():
	args = wikipedia.handleArgs()
	while True:
		redirectLinks = {}
		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"
					wikipedia.output("Nome del redirect: " + page.aslink())
					wikipedia.output("Pagina a cui punta il redirect: " + dest.aslink())
					wikipedia.output("Link in via di correzione: " + k)
					wikipedia.output("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, 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 = {}
		ref = page.getReferences(redirectsOnly = True)
		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"\n[[" + newpage + u"]]", u"\n[[" + newpage + u"|" + newpage[0].lower() + newpage[1:] + u"]]", u"\n[[" + newpage + u"|" + oldpage + u"]]", u"\n[[" + newpage + u"|" + oldpage[0].lower() + oldpage[1:] + u"]]", u'\n.']
	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][:-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 modifica

Attenzione!: questo script ha dei problemi con le disambigue. 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(number = 10000, repeat = True)
for i in allpages:
 if i.isRedirectPage() or i.isDisambig():
   continue
 refs = i.getReferences()
 refsList = list(refs)
 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, +{{Orfana}}')
    i.put(newtxt)
wikipedia.stopme()

Il mio benvenuto.py modifica

Da Utente:Alfiobot/benvenuto.py

######################################################
#
# benvenuto.py
#
# Bot per dare il benvenuto agli utenti (sono diventati troppi per farlo a mano...)
#
# Scarica il log dei nuovi utenti (fino ad un limite predefinito) e, per ogni utente
# che ancora non ha pagina di discussione, crea il seguente testo:
#
# {{benvenuto|nome=<nomeutente>}}
#
# Basato sulle librerie pywikipediabot.


import urllib, re
import wikipedia


wikipedia.handleArgs()

# No. of users to check
limit = 250

# URL of the newuser log
url = "http://it.wikipedia.org/w/index.php?title=Speciale:Log&type=newusers&user=&page=&limit=%d" % limit

# Search regular expression to find links like this (and the class attribute is optional too)
#<a href="/w/index.php?title=Discussioni_utente:Urizon9&action=edit" class="new" title="Discussioni utente:Urizon9">Discussione</a>

regexp = '</a> \(<a href=\"/w/index.php\?title=Discussioni_utente:(.*?)&amp;action=edit'

# Modify user-agent string
class AppURLopener(urllib.FancyURLopener):
    version = "Alfiobot/1.0"
urllib._urlopener = AppURLopener()

# Modify summary text
wikipedia.setAction("benvenuto")

# Read newuser log
print "Getting newuser log (last %d new users)..." % limit
f = urllib.urlopen(url)
text = f.read()
f.close()

r = re.compile(regexp, re.UNICODE)

# Loop over the newuser log and put welcome messages on empty discussion pages
pos = 0
while True:
        m = r.search(text, pos)
        if m == None:
                break
        pos = m.end()
        
        username = m.group(1)
        
        print "User %s needs welcome" % username
        page = u'Discussioni utente:%s' % username
        
        p = wikipedia.Page(wikipedia.getSite(), page)
                
        # Additional check: make a get to prevent the rare case where a discussion page
        # is created between the newuser log download and now.
        try:
         p.get()
        except wikipedia.NoPage:
         newtext = u'{{benve|nome={{subst:PAGENAME}}|1=--~~~~}}'
         p.put(newtext)
         print u"User OK!"
wikipedia.stopme()