monitinsertPunk.py

Basato su Utente:YuBot/Script#monitinsert.py

# -*- coding: utf-8 -*-
 
"""
Questo script serve per inserire il template monitoraggio.
Si richiede di specificare un parametro di progetto.
 
***ATTENZIONE**** Sarebbe bene evitare l'inserimento automatico
del monitoraggio nel caso in cui la cosa comporti la creazione
massiccia di pagine di discussione senza info rilevanti (meglio
inserirne poche alla volta e compilare i template).
"""
 
import wikipedia, sys, re, catlib
 
def workon(lista, varp):
    template= '{{Monitoraggio\n|progetto= '+varp+'\n|progetto2= '+'\n|accuratezza= ' +  '\n|scrittura= ' + '\n|fonti= ' + '\n|immagini= ' + '\n|note= ' + '\n|utente= ' + '\n|data= ' + '\n}}\n'
 
    for voce in lista:
        oldtext=''
        print voce.namespace()
        if voce.namespace() == 0:
            nomediscu='Discussione:'+voce.title()
            discussione=wikipedia.Page('it', nomediscu)       
 
            wikipedia.output(u'\n>>> %s <<<' % discussione.title())
 
            if discussione.exists():
                try:
                    oldtext=discussione.get()
                except discussione.isRedirectPage() or discussione.isDisambig():
                    wikipedia.output(u'Redirect o disambigua.')
                    continue
 
                else:
                    wikipedia.output(u'La pagina %s esiste.' %discussione.title())
 
                    if re.search(r'\{\{[Mm]onitoraggio', oldtext):
                            wikipedia.output(u'Monitoraggio presente, proseguo.')
                    else:
                        newtext= template + oldtext
                        wikipedia.showDiff(oldtext, newtext)
                        wikipedia.output (u'Inserisco il template monitoraggio.')
                        wikipedia.setAction(u'Bot: monitoraggio %s' % varp)
                        discussione.put(newtext)
 
            else:
                wikipedia.output(u'Non esiste una pagina di discussione per la voce %s' % voce.title())
                newtext= template + oldtext
                wikipedia.showDiff(oldtext, newtext)
                wikipedia.output (u'Inserisco il template monitoraggio.')
                wikipedia.setAction(u'Bot: monitoraggio %s' % varp)
                discussione.put(newtext)        
def main():
 
    lista=[]
    varp=''
 
 
    varp='Punk' #wikipedia.input(u'Mi serve la variabile \'progetto\' da inserire:')
    if varp:
        choice = 'p'#wikipedia.inputChoice(u"Scegli il metodo di creazione dell'elenco di voci: 'categoria' o 'puntanoQui?",  ['categoria', 'puntanoQui'], ['c', 'p'], '')
        if choice in ['C', 'c']:
            nomecat=wikipedia.input(u"Specifica una categoria")
            cat=catlib.Category('it', 'Categoria:'+nomecat)
            lista=[page for page in cat.articlesList()]
 
        if choice in ['P', 'p']:
            portale=wikipedia.Page('it', 'Portale:Punk')
            if portale.exists():
                choice = 's' #wikipedia.inputChoice(u"Cerco i collegamenti a %s?"  % portale.title(),  ['Si', 'No per inserire un\'altra pagina'], ['s', 'N'], 'N')
                if choice in ['S', 's']:
                    lista=[page for page in portale.getReferences()]
                elif choice in ['N', 'n']:
                    nomepagina=wikipedia.input(u'Inserire il nome completo della pagina o del template: ')
                    pagina=wikipedia.Page('it', nomepagina)                    
                    lista=[page for page in pagina.getReferences()]
    if lista:
        workon(lista, varp)
    else:
        choice = wikipedia.inputChoice(u"Termino il programma?",  ['Si', 'No'], ['s', 'N'], 'S')
        if choice in ['N', 'n']:
            main()
 
 
if __name__ == "__main__":
 
    print __doc__
 
    try:
        main()
    finally:
        wikipedia.stopme()

monitoraggio.py

#!/usr/bin/env python
# -*- utf-8 -*-
"""
README: Todo
"""
#
# (C) Pietrodn, 2007
# (C) Sirabder87, 2008
# (C) Filnik, 2008
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'
#
 
import wikipedia, catlib, re
 
def main():
    always = False
    for arg in wikipedia.handleArgs():
        if arg.startswith("-always"):
            always = True
    site = wikipedia.getSite()
    category_name = 'Voci monitorate Progetto Musica/Punk'
    categoria = catlib.Category(site, category_name)
    discussioni = categoria.articles()
    for page in discussioni:
        if not page.isTalkPage():
            continue
        associatedPage = page.toggleTalkPage()
        try:
            pageText = associatedPage.get()
        except wikipedia.Error, e:
            print e # Print the error and skip
            continue
        match = re.search(r'(\[\[Immagine|\[\[Image|\|Fotografia\s*=[^\|\n]*|\|Marchio\s*=[^\|\n])', pageText, re.IGNORECASE)
        if match == None:
            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
            try:
                talkText = page.get()
            except wikipedia.Error, e:
                print e # Print the error and skip
                continue
            newTalkText = re.sub('\|\n? *?[Ii]mmagini ?= *?\n', '|immagini=d\n', talkText)
            if talkText != newTalkText:
                wikipedia.showDiff(talkText, newTalkText)
                if not always:
                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                else:
                    choice = 'y'
                if choice in ['A', 'a']:
                    always = True
                    choice = 'y'
                if choice in ['Y', 'y']:
                    try:
                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Non ci sono immagini: valutazione "d"')
                    except wikipedia.Error, e:
                        print e # Print the error and skip
                        continue
        if match is not None:
            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
            try:
                talkText = page.get()
            except wikipedia.Error, e:
                print e # Print the error and skip
                continue
            newTalkText = re.sub('\|\n? *?[Ii]mmagini ?= ?d\n', '|immagini= \n', talkText)
            if talkText != newTalkText:
                wikipedia.showDiff(talkText, newTalkText)
                if not always:
                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                else:
                    choice = 'y'
                if choice in ['A', 'a']:
                    always = True
                    choice = 'y'
                if choice in ['Y', 'y']:
                    try:
                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Trovate immagini: rimuovo valutazione "d"')
                    except wikipedia.Error, e:
                        print e # Print the error and skip
                        continue
        match = re.search('(<ref.*)', pageText, re.IGNORECASE)
        if match == None:
            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
            try:
                talkText = page.get()
            except wikipedia.Error, e:
                print e # Print the error and skip
                continue
            newTalkText = re.sub('\|\n? *?[Ff]onti ?= *?\n', '|fonti=d\n', talkText)
            if talkText != newTalkText:
                wikipedia.showDiff(talkText, newTalkText)
                if not always:
                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                else:
                    choice = 'y'
                if choice in ['A', 'a']:
                    always = True
                    choice = 'y'
                if choice in ['Y', 'y']:
                    try:
                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Non ci sono fonti: valutazione "d"')
                    except wikipedia.Error, e:
                        print e # Print the error and skip
                        continue
        if match is not None:
            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
            try:
                talkText = page.get()
            except wikipedia.Error, e:
                print e # Print the error and skip
                continue
            newTalkText = re.sub('\|\n? *?[Ff]onti ?= ?d\n', '|fonti= \n', talkText)
            if talkText != newTalkText:
                wikipedia.showDiff(talkText, newTalkText)
                if not always:
                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                else:
                    choice = 'y'
                if choice in ['A', 'a']:
                    always = True
                    choice = 'y'
                if choice in ['Y', 'y']:
                    try:
                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Trovate fonti: rimuovo valutazione "d"')
                    except wikipedia.Error, e:
                        print e # Print the error and skip
                        continue
        match = re.search('(\{\{[Vv]etrina)', pageText, re.IGNORECASE)
        if match is not None:
            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
            try:
                talkText = page.get()
            except wikipedia.Error, e:
                print e # Print the error and skip
                continue
            newTalkText = re.sub('\|\n? *?[Aa]ccuratezza ?= *?\n', '|accuratezza=a\n', talkText)
            if talkText != newTalkText:
                wikipedia.showDiff(talkText, newTalkText)
                if not always:
                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                else:
                    choice = 'y'
                if choice in ['A', 'a']:
                    always = True
                    choice = 'y'
                if choice in ['Y', 'y']:
                    try:
                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Voce in vetrina: completezza "a"')
                    except wikipedia.Error, e:
                        print e # Print the error and skip
                        continue
        if match == None:
		match = re.search('(\{\{[Ss]\|)', pageText, re.IGNORECASE)
	        if match is not None:
	            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
	            try:
	                talkText = page.get()
	            except wikipedia.Error, e:
	                print e # Print the error and skip
	                continue
	            newTalkText = re.sub('\|\n? *?[Aa]ccuratezza ?=.*?\n', '|accuratezza=c\n', talkText)
	            if talkText != newTalkText:
	                wikipedia.showDiff(talkText, newTalkText)
	                if not always:
	                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
	                else:
	                    choice = 'y'
	                if choice in ['A', 'a']:
	                    always = True
	                    choice = 'y'
	                if choice in ['Y', 'y']:
	                    try:
	                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Stub: completezza "c"')
	                    except wikipedia.Error, e:
	                        print e # Print the error and skip
	                        continue
	        if match == None:
		        match = re.search('(\{\{[Aa]\|)', pageText, re.IGNORECASE)
		        if match is not None:
		            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
		            try:
		                talkText = page.get()
		            except wikipedia.Error, e:
		                print e # Print the error and skip
		                continue
		            newTalkText = re.sub('\|\n? *?[Aa]ccuratezza ?= *?\n', '|accuratezza=d\n', talkText)
		            if talkText != newTalkText:
		                wikipedia.showDiff(talkText, newTalkText)
		                if not always:
		                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
		                else:
		                    choice = 'y'
		                if choice in ['A', 'a']:
		                    always = True
		                    choice = 'y'
		                if choice in ['Y', 'y']:
		                    try:
		                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Voce da aiutare: completezza "d"')
		                    except wikipedia.Error, e:
		                        print e # Print the error and skip
		                        continue
		        if match == None:
		            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
		            try:
		                talkText = page.get()
		            except wikipedia.Error, e:
		                print e # Print the error and skip
		                continue
		            newTalkText = re.sub('\|\n? *?[Aa]ccuratezza ?= ?[cd]\n', '|accuratezza= \n', talkText)
            		if talkText != newTalkText:
		                wikipedia.showDiff(talkText, newTalkText)
                		if not always:
                		    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                		else:
                		    choice = 'y'
                		if choice in ['A', 'a']:
                		    always = True
                		    choice = 'y'
                		if choice in ['Y', 'y']:
                		    try:
                		        page.put(newTalkText, u'[[WP:BOT|Bot]]: Voce non da aiutare o stub: rimuovo completezza')
		                    except wikipedia.Error, e:
                		        print e # Print the error and skip
                		        continue
        match = re.search('(\{\{[Dd]a correggere)', pageText, re.IGNORECASE)
        if match is not None:
            wikipedia.output(">>>>> %s <<<<<" % associatedPage.title())
            try:
                talkText = page.get()
            except wikipedia.Error, e:
                print e # Print the error and skip
                continue
            newTalkText = re.sub('\|\n? *?[Ss]crittura ?= *?\n', '|scrittura=d\n', talkText)
            if talkText != newTalkText:
                wikipedia.showDiff(talkText, newTalkText)
                if not always:
                    choice = wikipedia.inputChoice(u"Apply the changes?",  ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                else:
                    choice = 'y'
                if choice in ['A', 'a']:
                    always = True
                    choice = 'y'
                if choice in ['Y', 'y']:
                    try:
                        page.put(newTalkText, u'[[WP:BOT|Bot]]: Voce da correggere: scrittura "d"')
                    except wikipedia.Error, e:
                        print e # Print the error and skip
                        continue
 
if __name__ == "__main__": 
    try:
        main()
    finally:
        wikipedia.stopme()