Utente:Wisbot/asteroidi.py: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
Nuova pagina: <source lang=python> """ Modifiche asteroidi -first:X Primo asteroide su cui agisce, poi segue l'ordine della lista ... |
Nessun oggetto della modifica |
||
Riga 1:
<source lang=python>
# -*- coding: utf-8 -*-
"""
Modifiche asteroidi
Riga 13:
argomento non nullo
-valore:V Agisce solo sugli
argomento con un certo valore (usare con
l'opzione -argomento)
Riga 19:
"""
#
# [[Utente:Wiso]]
#
# Distributed under the terms of the GPL licence
Riga 109:
for row in rows:
self.writerow(row)
def text2regex(text):
# maiuscole / minuscole come iniziale
text = '[' + text.upper()[0] + text.lower()[0] + ']' + text[1:]
return text
def name2regex(name):
name = re.escape(name)
# maiuscole / minuscole come iniziale
name = '[' + name.upper()[0] + name.lower()[0] + ']' + name[1:]
# spazio -> [ _]
name = name.replace('\ ','[ _]').replace('\_','[ _]')
return name
class botAsteroidi:
def __init__(self, gen, acceptall = False):
self.gen = gen
self.site = wikipedia.getSite()
self.acceptall = acceptall
def campo_pieno(self,campo,text):
Riga 171 ⟶ 178:
def aggiungi_cat(self,text,cat):
regex = re.compile('\[\[[Cc]ategoria ?: ?Asteroidi del sistema solare(.*?)\]\]')▼
Aggiunge una categoria. Se la categoria è già presente non fa nulla. La
text = regex.sub('\\0\n[[Categoria:%s|\\1]] %cat',text)▼
categoria viene aggiunta dopo una categoria già presente che inizia
con Asteroid. Viene restituito il testo modificato.
"""
# controllo che la categoria non sia già presente
regex = re.compile(u'[Cc]ategoria ?: ?%s' %name2regex(cat))
if regex.search(text):
wikipedia.output(u'\03{lightyellow}La categoria %s era già presente\03{default}' %cat)
return text
# faccio l'aggiunta dopo una categoria già presente
▲ regex = re.compile('(\[\[[Cc]ategoria ?: ?
# controllo che sia stata aggiunta
if n==0:
wikipedia.output(u'\03{lightred}ERR: NON RIESCO AD INSERIRE LA CATEGORIA: %s' %cat)
return text
def aggiungi_testo(self,text,new):
"""
Inserisce la stringa new nel testo text dopo la prima frase. La prima
frase è quella dopo il primo template, che contiene il carattere 'è'
e che finisce con un '.'. Se la stringa new era già presente nel testo
non fa nulla. Resistuisce il testo modificato.
"""
# controlla che la stringa non sia già presente
regex = re.compile(text2regex(new))
if regex.search(text):
wikipedia.output(u'\03{lightyellow}La stringa %s era già presente nel testo' %new)
return text
# aggiunta
regex = re.compile(u'(}}.+?è.+?)\. ?',re.DOTALL)
(text,n) = regex.subn(u'\\1%s. ' %new,text,1)
# controlla che la sostituzione sia abbenuta
if n==0:
wikipedia.output(u'\03{lightred}ERR: NON RIESCO AD INSERIRE LA STRINGA %s ALLA FINE DELLA PRIMA FRASE\03{default}' %new)
return text
Riga 333 ⟶ 367:
wikipedia.output('-----------------------------------------')
wikipedia.setAction(message[:-2])
if not self.acceptall:
choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
if choice in ['a', 'A']:
self.acceptall = True
if choice in ['y', 'Y']:
page.put_async(text)
if self.acceptall:
try:
page.put(text)
except wikipedia.EditConflict:
wikipedia.output(u'Skipping %s because of edit conflict' % (page.title(),))
except wikipedia.SpamfilterError, e:
wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
except wikipedia.PageNotSaved, error:
wikipedia.output(u'Error putting page: %s' % (error.args,))
except wikipedia.LockedPage:
wikipedia.output(u'Skipping %s (locked page)' % (page.title(),))
def filterArg(gen,argomento,valore):
Riga 387 ⟶ 438:
def main():
s = wikipedia.Site('it')
filename = "lista_asteroidi.txt"▼
lista = open(filename, 'r')▼
csv.register_dialect('dialettoAsteroidi',delimiter='|')▼
gen = UnicodeReader(lista, 'dialettoAsteroidi')▼
▲ filename = "lista_asteroidi.txt"
first = None
number = None
argomento = None
valore = None
for arg in wikipedia.handleArgs():
Riga 406 ⟶ 456:
elif arg.startswith('-number'):
if len(arg) == 7:
number = int(wikipedia.
else:
number = int(arg[8:])
elif arg.startswith('-list'):
if len(arg) == 5:
filename = wikipedia.input('Lista dati:')
else:
filename = arg[6:]
else:
array = arg.split(':')
Riga 415 ⟶ 470:
valore = array[1]
argomento = arg2int(argomento[1:])
▲ lista = open(filename, 'r')
▲ csv.register_dialect('dialettoAsteroidi',delimiter='|')
▲ gen = UnicodeReader(lista, 'dialettoAsteroidi')
Riga 431 ⟶ 490:
bot = botAsteroidi(gen)
bot.run()
wikipedia.output(u'Lista finita')
|