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:Updatelog.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
This is a little (always little, eh? ^__^) script that is used in wiktionary-it to pick up
the log written from import-it.py and add it in the page: Wikipedia:Proposte_di_trasferimento/transferlist.js
"""
#
# (C) Filnik, 2007
#
# Distributed under the terms of the MIT license.
#
# Version: 1.2
#
 
import wikipedia, config
import re
 
# Defing the most important function of the code.
def getstring(site, raw, title = list(), dizionario = {}):
    pos = 0
    parseList = list()
    while 1:
        # This is the regex used to load the strings
        regex = """\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\","""
        pl = re.compile(regex, re.UNICODE)
        m = pl.search(raw, pos)
        if m == None:
            if len(parseList) >= 1:
                wikipedia.output(u"Data taken...\n")
                break
            elif len(parseList) == 0:
                wikipedia.output(u"Find nothing! Wrong regex! Pls check!")
                break
                wikipedia.stopme()
        pos = m.end()
        # This is the page from we import
        pagefrom = m.group(1).encode(site.encoding())
        # The project in which we import
        project = m.group(2).encode(site.encoding())
        # The comment used to import
        comment = m.group(3).encode(site.encoding())
        # And the final page where we import
        pageto = m.group(4).encode(site.encoding())
        # That's a list that take all these parameters
        singlelist = [pagefrom, project, comment, pageto]
        # Adding the list to parseList (that is useless, but i use
        # it to understand if the regex is wrong or not)
        if singlelist not in parseList:
            parseList += singlelist
            # That's a list with only the first page
            title += [pagefrom]
            # And that's a dictionary where the keys are the first pages :-)
            dizionario[pagefrom] = singlelist
            continue
        else:
            continue
    return (title, dizionario)
 
texttoadd = """/* <pre> */
// SI PREGA DI INSERIRE LE PAGINE IN ***ORDINE ALFABETICO***. Grazie.
function setup_transferlist()
{
transferlist = Array("""
 
textend = \
"""
""
);}
/* </pre> */"""
 
# Main code :-)
try:
    # Defing the first site where we load the pages
    site = wikipedia.getSite('it', 'wikipedia')
    Loadpage = wikipedia.Page(site, 'Wikipedia:Proposte_di_trasferimento/transferlist.js')
    raw = Loadpage.get() # <-- The text loaded
    (title, dizionario) = getstring(site, raw) # <-- Using the function to parse the results
    # Here there is the second site
    wiktsite = wikipedia.getSite('it', 'wiktionary')
    wiktpage = wikipedia.Page(wiktsite, 'Utente:Filbot/Log')
    raw1 = wiktpage.get()
    (totaltitle, dizionario) = getstring(wiktsite, raw1, title, dizionario) #<-- Parsing..
    totaltitle.sort() #<-- Better if we sort the results ;-)
 
    # Rebilding the data into strings
    for a in totaltitle:
        #print dizionario
        lista = dizionario[a] # Understood why i need a list and a dictionary? ^_-
        nome1 = lista[0].decode(site.encoding())
        nome2 = lista[1].decode(site.encoding())
        nome3 = lista[2].decode(site.encoding())
        nome4 = lista[3].decode(site.encoding())
        wikipedia.output(u'Checking the existance of %s...' % nome1)
        if not wikipedia.Page(site, nome1).exists():
            string = '"%s", "%s", "%s", "%s",' % (nome1, nome2, nome3, nome4)
            texttoadd = texttoadd + '\n' + string
    # Final text! :-)
    totaltxt = texttoadd + textend
    # And at the end, we have to put the result! :-)
    Loadpage.put(totaltxt, 'Bot: Aggiungo voci caricate su wiktionary')
    wiktpage.put('This is a report page for the imported pages, please translate me. --[[User:Filbot|Filbot]]')
finally:
    wikipedia.output(u'Done.')
    wikipedia.stopme()
Personal tools
Share