Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:Comuni.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
"""
Italian bot for adding templates in some talk in NS0.
 
Bot per inserire {{Richiesta foto}} in una serie di comuni.
 
Parametro:
 
-cat:       Definisce la categoria da controllare (default: Comuni italiani)
 
"""
#
# Comuni.py 1.5
#
# (C) Filnik it.wikipedia
#
#
 
import wikipedia, catlib, re
 
class main:
    def __init__(self, lista, site = wikipedia.getSite()):
        self.lista = lista
        self.site = site
    def whattodo(self):
        pos2 = 0
        for pag in self.lista:
            page = pag.title()
            wikipedia.output(u'Processing %s...' % page)
            pagina = 'Discussione:%s' % page
            p = wikipedia.Page(self.site, page)
            p2 = wikipedia.Page(self.site, pagina)
            try:
                text = p.get()
                rim = rimanente(text, p2, page)
                rim.resto()
            except wikipedia.NoPage:
                text = ''
                rimanente(text, p2, page)
                rim = rimanente(text, p2, page)
                rim.resto()
 
class rimanente:
    def __init__(self, text, pagina, page):
        self.text = text
        self.pagina = pagina
        self.page = page
    def resto(self):
        pos = 0
        pos1 = 0
        pos3 = 0
        reg = r'(?:\[\[[Ii]mmagine:|\[\[[Ii]mage:|<gallery)'
        regexp = r"(?:provincia (di |della |dello |dei |del |dell')[^\[](.*?))\]\]"        
        while 1:
            r = re.compile(regexp, re.UNICODE)
            r7 = re.compile(reg, re.UNICODE)
            m7 = r7.search(self.text, pos3)
            if m7 == None:
                wikipedia.output(u"Ok, non c'e' un'immagine nella pagina del comune...")
            else:
                pos3 = m7.end()
                wikipedia.output(u"C'e' gia' l'immagine nel comune")
                return # Esco
            m = r.search(self.text, pos)
            if m == None:
                wikipedia.output(u'In %s non ho trovato niente...' % self.page)
                wikipedia.output(u"ATTENZIONE! Forse la regex e' sbagliata! Contattate Filnik!")
                break                
            pos = m.end()
            provincia = m.group(1)
            wikipedia.output(u"Ok, ho trovato che %s e' in provincia di %s" (self.page, provincia))
            if self.pagina.exists():      
                testo = self.pagina.get()
            else:
                continue
            regexp1 = '\{\{Richiesta foto\|(.*?)\}\}'
            r1 = re.compile(regexp1, re.UNICODE)
            m1 = r1.search(testo, pos1)
            if m1 == None:
                wikipedia.output(u"Perfetto, aggiungo {{Richiesta foto|%s}} alla pagina discussione" % provincia)
                newtext = u'{{Richiesta foto|%s}}\n' % provincia
                self.pagina.put(newtext + testo, "Bot: Aggiungo Template {{Richiesta foto}}")
                wikipedia.output(u"Messo il template!")
                botrun = main(0)
                botrun.whattodo()
            else:
                pos = m1.end()
                wikipedia.output(u"C'e' gia' {{Richiesta foto|%s}} nella pagina discussione, passo oltre..." % provincia)
            return # Esco
def work():
    site = wikipedia.getSite()
    CatTitle = "Comuni italiani"
    for arg in wikipedia.handleArgs():
        if arg.startswith('-cat'):
            if len(arg) == 4:
                CatTitle = wikipedia.input(u'Che categoria di comuni vuoi caricare?')
            else:
                CatTitle = arg[5:]
 
    cat = catlib.Category(site, "Category:" + CatTitle)
    botrun = main(cat.articles(), site)
    botrun.whattodo()
 
if __name__ == "__main__":
    try:
        work()
    finally:
        wikipedia.stopme()
Personal tools
Share