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.

User:Alex brollo/Scripts

From Botwiki
Jump to: navigation, search

Here the scripts of Alebot. They are too listed into Scripts subpage of the bot into it.source.

Please laught, but not so loud - Alebot is really sensitive.... ;-)

# -*- coding: utf-8 -*-
import wikipedia
import pickle
 
 
 
f=open("aut_xml.txt")
autori=f.read()
f.close()
 
# elabora databox + xml
def databox_xml(iniziali="ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
    leggi_elenco_autori(iniziali)
    leggi_pagine()
    return "Fatta revisione databox e xml degli autori con iniziali "+iniziali
 
 
##procedure list
def cerca(autore,dato=""):
    dati_autore=find_stringa(autori,"<autore>"+autore,"</autore>",1)
    if not dato == "":
        tag1,tag2="<"+dato+">","</"+dato+">"
        dato_autore=find_stringa(dati_autore,tag1,tag2)
    else:
        dato_autore=dati_autore
 
    return dato_autore
 
##unicode translations:
#utf-8->unicode
def t2u(stringa):
    return unicode(stringa,"utf-8")
 
# unicode->utf-8
def u2t(stringa):
    return stringa.encode("utf-8")
 
##wiki reading/writing
 
 
# Reads from it.wikisource a page (full name)
def leggi_pagina(nome_pagina="Utente:Alex brollo"):
    site = wikipedia.getSite()
    page = wikipedia.Page(site, nome_pagina)
    text = u2t(page.get())
    return text
 
# Writes into it.wikisource a unicode string into a named page (full name), adding to previous content (aggiungi=True)
# or replacing it (aggiungi=False)
def scrivi_pagina(testo="Prova\n\n",aggiungi=True ,nome_pagina="Utente:Alebot/Sandbox"):
    site = wikipedia.getSite()
    page = wikipedia.Page(site, nome_pagina)
    if aggiungi:
        contenuto = page.get()
        page.put(unicode(contenuto+"\n\n"+testo),"utf-8")
    else:
        #print testo
        page.put(unicode(testo,"utf-8"))
    wikipedia.stopme()
    return "Fatto"
 
# reads from it.wikisource the pages with alphabetical lists of authors and 
# gets the authors list; saves the list into a txt file (one name for row)
def leggi_elenco_autori(iniziali="ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
    x=iniziali
    text=[]
    for i in range(len(x)):
        titolo="Wikisource:Elenco alfabetico degli autori "+x[i:i+1]
        t=leggi_pagina(titolo)
        text.append([titolo,t])
        print "letto "+titolo
  #  stopme()
    lista_autori=[]
    for i in range(len(text)):
        #print "parte produci lista"
        a1=produci_lista(text[i][1],"{{Template:","}}")
        lista_autori=lista_autori+a1
    lista_autori_txt=""
    for i in range(len(lista_autori)):
        lista_autori_txt=lista_autori_txt+lista_autori[i]+"\n"
    f=open("lista_autori.txt","w")
    f.write(lista_autori_txt)
    f.close()
    return "fatto"
 
# reads from text file the list of authors' names, splits it into a python list 
# and reads the authors' pages, getting the data template; saves all templates content
# as text into a txt file
 
def leggi_pagine(lista="lista_autori.txt"):
    f=open(lista)
    lista_autori=f.read().split("\n")
    f.close()
    fo=open("aut_xml.txt","w")
    for i in range(len(lista_autori)):
        if len(lista_autori[i])>0:
            autore=leggi_pagina(t2u("Autore:"+lista_autori[i]))
            autore=find_stringa(autore,"{{Autore","}}", 1)
            template=autore
            autore_xml=parse_autore(autore)
            databox(lista_autori[i],template,"\n\n<!-- DATI XML AUTORE\n"+autore_xml+"\n -->")
            print "Letto "+lista_autori[i]; 
            fo.write(autore_xml)
            print ".. e scritto"
    fo.close()
    return "Fatto!"
 
# Writes a databox into Template:Cognome Nome/Dati
def databox(autore,template_autore,xml):
    template=template_autore[:]
    template=template.replace("{{Autore","{{subst:Autore1")+xml
    nome_pagina=t2u("Template:"+autore+"/Dati")
    #print template
    scrivi_pagina(template,False,nome_pagina)
    return 
 
 
 
 
##Pickle data loading/dumping
 
# dumps a variable
 
def salva(variabile,nome_file="dato"):
    f=open(nome_file+".pcl","w")
    pickle.dump(variabile, f)
    f.close()
    return "Variabile salvata nel file "+nome_file
 
# loads a variable
def carica(nome_file):
    f=open(nome_file)
    contenuto=pickle.load(f)
    f.close()
    return contenuto
 
# Authors' data managing
 
# Reads txt file with Authors' templates, calls "parser" and writes the result into a txt file
def parse_lista_autori(lista):
    dati=""
    pagine_autori=carica("autori_pagina.pcl")
    for i in range(len(lista)):
        elemento=parse_autore(pagine_autori[lista[i]])
        dati=dati+elemento
    f=open("dati_xml_autori.txt","w")
    f.write(dati)
    f.close()
    return dati
 
 
# translates a Author template into a xml structure
# as:  <autore>Alessandro Manzoni<dati><nome>Alessandro</nome>....</dati></autore>   
def parse_autore(pag_autore):
    l=""
    autore=find_stringa(pag_autore,"Nome =","\n").strip()+" "+find_stringa(pag_autore,"Cognome =","\n").strip()
    autore=autore.strip()
    l=l+tagger("nome",(find_stringa(pag_autore,"Nome =","\n")))    #0
    l=l+tagger("cognome",(find_stringa(pag_autore,"Cognome =","\n"))) #1
    l=l+tagger("iniziali",(find_stringa(pag_autore,"Iniziale del cognome =","\n")))    #2
    l=l+tagger("altronome",(find_stringa(pag_autore,"Eventuale altro nome =","\n")))    #3
    l=l+tagger("pseudonimo",(find_stringa(pag_autore,"Eventuale pseudonimo =","\n")))    #4
    l=l+tagger("reale",(find_stringa(pag_autore,"Eventuale nome reale =","\n")))    #5
    l=l+tagger("gm_nascita",(find_stringa(pag_autore,"Giorno e mese di nascita =","\n")))#6
    l=l+tagger("a_nascita",(find_stringa(pag_autore,"Anno di nascita =","\n")))         #7
    l=l+tagger("gm_morte",(find_stringa(pag_autore,"Giorno e mese di morte =","\n")))  #8
    l=l+tagger("a_morte",(find_stringa(pag_autore,"Anno di morte =","\n")))           #9
    l=l+tagger("secolo1",(find_stringa(pag_autore,"Secolo di attività =","\n")))     #10
    l=l+tagger("secolo2",(find_stringa(pag_autore,"Eventuale secondo secolo di attività =","\n")))   #11
    l=l+tagger("descrizione",(find_stringa(pag_autore,"Professione e nazionalit\xe0 =","\n")))   #12
    l=l+tagger("wikipedia",(find_stringa(pag_autore,"Nome della pagina su Wikipedia =","\n")))  #13
    l=l+tagger("wikiquote",(find_stringa(pag_autore,"Nome della pagina su Wikiquote =","\n")))  #14
    l=l+tagger("wikinotizie",(find_stringa(pag_autore,"Nome della pagina su Wikinotizie =","\n")))#15
    l=l+tagger("commons",(find_stringa(pag_autore,"Nome della pagina su Commons =","\n")))    #16
    l=l+tagger("immagine",(find_stringa(pag_autore,"Nome dell'immagine =","\n")))              #17
    l=l+tagger("indice",(find_stringa(pag_autore,"Vuoi l'indice dei contenuti? =","\n")))    #18
    l=l+tagger("testi",(find_stringa(pag_autore,"Abbiamo testi scritti dall'autore? =","\n")))  #19
    l=l+tagger("traduzioni",(find_stringa(pag_autore,"Abbiamo traduzioni scritte dall'autore? =","\n"))) #20
    l=l+tagger("citazioni",(find_stringa(pag_autore,"L'autore \xe8 citato in altre opere pubblicate? =","\n")))#21
    l=tagger("dati",l)
    l=tagger("autore",autore+l)+"\n"
    return l
 
# adds tags to data (subroutine used by the previous procedure
def tagger(tag, dato):
    dato_tagged="<"+tag+">"+dato+"</"+tag+">"
    return dato_tagged
 
# test unused routine: builds a table from an old list-based data set
def tabella_autori(iniziale="A"):
    dati=carica("voc_dati_autori_new.pcl")
    tabella="{|\n"
    tabella=tabella+"|'''Cognome e nome'''||'''Data nascita e morte'''||'''Descrizione'''||'''Testi disponibili'''\n"
    l_a=dati.keys()
    l=[]
    for i in range(len(l_a)):
        if dati[l_a[i]][2]==iniziale:
            l.append([dati[l_a[i]][1],  l_a[i]])
    l.sort()
    for i in range(len(l)):
        l[i]=l[i][1]
        el1="[[Autore:"+l[i]+"|"+dati[l[i]][1]+", "+dati[l[i]][0]+"]]"
        el1=el1.replace("|, ","|")
        el2=dati[l[i]][6]+" "+dati[l[i]][7]+" - "+dati[l[i]][8]+" "+dati[l[i]][9]
        el3=dati[l[i]][12]
        el4=dati[l[i]][19]
        riga="|-\n|"+el1+"||"+el2+"||"+el3+"||"+el4+"\n"
        tabella=tabella+riga
    tabella=tabella+"|}\n"
    return tabella
 
# test unused routine: builds a dictionary of entries into Autore template
def var_autore():
    voc_var={'citazioni': 21, 'professione e nazionalit\xc3\xa0': 12, 'testi': 19, 'indice contenuti': 18, 'anno nascita': 7, 'secolo': 10, 'nome': 0, 'secondo secolo': 11, 'nome immagine': 17, 'giorno e mese nascita': 6, 'altro nome': 3, 'nome reale': 5, 'pagina commons': 16, 'pagina wikipedia': 13, 'iniziale cognome': 2, 'pseudonimo': 4, 'pagina wikinotizie': 15, 'traduzioni': 20, 'anno morte': 9, 'cognome': 1, 'giorno e mese morte': 8, 'pagina wikiquote': 14}
    return voc_var
 
# saves Alebot.py code into Utente:Alebot/Scripts (t.i. here)
def aggiorna_pagina_scripts():
    f=open("Alebot.py")
    scripts="<"+"pre>"+f.read()+"<"+"/pre>"
    f.close()
    pagina=leggi_pagina("Utente:Alebot/Scripts")
    intestazione=find_stringa(pagina,'<section begin="s1" />','<section end="s1" />',1)+"\n"
    firma="\n--~~~~"
    pagina=pagina[0]+"--~~~~"
    f=open("Alebot.txt","w")
    f.write(pagina)
    f.close()
    pagina=intestazione+scripts+firma
    scrivi_pagina(pagina,False,"Utente:Alebot/Scripts")
 
    return "Fatto"
 
# unused routine for Page: namespace.
# It reads Pagina: pages from page ini to page fin into a string 
# legge da it.wikisource le pagine Pagina dalla pagina ini alla pagina fin
# e le carica in una lista di liste [numero pagina,testo pagina]
def leggi_indice(name,ini,fin):
    text=[]
    for i in range(ini,fin):
        t=u2t(leggi_pagina("Pagina:"+name+".djvu/"+str(i)))
        text.append([i,t])
    stopme()
    return text
 
 
# shortcut for stopme
def stopme():
    wikipedia.stopme()
    return
 
## General string management routines for RegEx ignorants :-(
 
# finds a substring beginning with idi and ending with idf; returns it
# with (dc=1) or without (default dc=0) idi and idf; used for xml managing too
 
def find_stringa(stringa,idi,idf,dc=0):
    idip=stringa.find(idi)
    idfp=stringa.find(idf,idip)+len(idf)
    if idip>-1 and idfp>0:
        if dc==0:
            vvalore=stringa[idip+len(idi):idfp-len(idf)]
        else:
            vvalore=stringa[idip:idfp]
    else:
        vvalore=""
    return vvalore
 
# returns a list of all substrings (beginning with idi and ending with idf)
# of a string; used for xml data managing too
 
def el_st(stringa,idi,idf,new=""):
    idip=stringa.find(idi)
    idfp=stringa.find(idf,idip)+len(idf)
    if idip>-1 and idfp>0:
        vvalore=stringa[idip:idfp]
        valore=stringa.replace(vvalore,new,1)
    else:
        valore=stringa
    return valore, vvalore
 
# returns a list of all substrings (beginning with idi and ending with idf)
# of a string; ; used for xml data managing too
def produci_lista(testo,idi,idf,dc=0):
    t=testo[:]
    lista=[]
    while not find_stringa(t,idi,idf)=="":
        el=find_stringa(t,idi,idf,1)
        t=t.replace(el,"")
        #print el
        el=find_stringa(el,idi,idf,0)
        #print el
        #print
        lista.append(el)
    return lista
Personal tools
Share
In other languages