richiesta: Wikipedia:Bot/Richieste#Categorizzazione degli anime


# funzione ricorsiva per navigare nelle supercategorie alla ricerca della categoria cat
# si ferma se non ci sono piu supercategorie o comunque dopo level livelli risaliti
def searchCat(cat, list, level):
  if len(list) == 0 or level == 0:
    return 0
  if cat in list:
    return level
  else:
    level -= 1
    print list
    for item in list:
      res = searchCat(cat, item.categories(), level)
      if res > 0:
        break
    return res
    

titolo = 'Lista dei diff sugli edit che il bot farebbe per la richiesta: [[Wikipedia:Bot/Richieste#Categorizzazione degli anime]]\n\n'
output = codecs.open('anime_tmp_fumetto.txt', encoding='utf-8', mode='w+')
output.write(titolo + '\n')

site = wikipedia.getSite()
sandbox = wikipedia.Page(site, "Utente:PavelBOT/fea/log")
tempPage = wikipedia.Page(site, "Template:Fumetto e animazione")

sandboxTxt = titolo

catAnime = catlib.Category(site, "Anime")
catSerie = catlib.Category(site, "Serie televisive d'animazione")
newCatsDict = {
  'serie TV': "Serie televisive anime",
  'OAV': "Original anime video",
  'ONA': "Original net anime",
  'film': "Film d'animazione giapponesi"
}


refPages = tempPage.getReferences()
count = 0
maxRisalita = 3
for page in refPages:
  if page.namespace() != 0:
    continue
  wikipedia.output('Processing '+page.title())
  content = page.get()
  cats = page.categories()
  edited = 0
  risalita = maxRisalita
  sortKey = None
  for subtype in newCatsDict:
    pattern = re.compile(r'\{\{fumetto e animazione(.*?)\|\s*tipo\s*=\s*anime(.*?)\|\s*sottotipo\s*=\s*'+subtype+'(.*?)\}\}', re.DOTALL | re.IGNORECASE)
    res = re.search(pattern, content)
    if res != None:
      if catAnime in cats:
        index = cats.index(catAnime)
        tmp = cats.pop(index).sortKey
        if tmp != '':
          sortKey = tmp
        edited = 1
      if catSerie in cats:
        if sortKey == None:
          index = cats.index(catSerie)
          tmp = cats.pop(index).sortKey
          if tmp != '':
            sortKey = tmp
        else:
          cats.remove(catSerie)
        edited = 1
      newCat = catlib.Category(site, newCatsDict[subtype], None, sortKey)
      # if newCat not in cats:
      risalita = searchCat(newCat, cats, maxRisalita)
      if risalita == 0:
        wikipedia.output('aggiungo ' + newCat.title())
        cats.append(newCat)
        edited = 1
  
  if risalita > 0 and risalita != maxRisalita:
    s = '* [[' + page.title() + ']] ha la categoria richiesta nelle sopracategorie, livello ' + str(maxRisalita - risalita) + '\n\n'
    output.write(s)
    sandboxTxt += s
    
  if edited == 1:
    count += 1
    text = wikipedia.replaceCategoryLinks(content, cats, site)
    output.write(str(count) + '. ' + page.title() + '\n' + showDiff(content, text) + '\n')
    output.flush()
    sandboxTxt += '* ' + str(count) + '. [[' + page.title() + ']]\n' + '<pre>\n' + showDiff(content, text) + '</pre>\n'

wikipedia.setAction(titolo)
sandbox.put(sandboxTxt)

wikipedia.stopme()
output.close()

la funzione showDiff è la stessa della libreria wikipedia, modificata togliendo la colorazione e mettendo il ritorno dopo l'output per stanpare il diff su file e nel log