Jump to content

User:OrenBochman/Rbot/Variables.py

From Meta, a Wikimedia project coordination wiki

This script is written in python it provides the actual parameters of differnt wikis for use by Template:Variables.

# -*- coding: utf-8  -*-
# Código GPL, autor: BigSus
# http://es.wikipedia.org/wiki/Usuario:Bigsus-bot/Variables.py

import wikipedia, time, re
from urllib import urlopen
from operator import itemgetter

# mode = don't updated listings of all wikis
mode = 'es'

parameters = {
    'en': {
        'page':    u'Template:NUMBEROF/data',
        'summary':   u'Bot: Automatic update of variables',
        'principio': u'<!-- If you want to add a new language, ask for it in the talk pages, not here directly. -->',
        'final':     u''
    }   
}
      
def generate_data():
    """   
    Browse all wikis in search of the data we need.
    It returns an array with all data for future processing.
    """
    mode2 = mode
    if mode2==None:
        mode2 = 'all'

    # languages para revisar (listado automática)
    lista_languages = wikipedia.Site.languages(wikipedia.Site('es', 'wikipedia'))
   
    wikipedia.output('Iniciando variables.py...\nFecha y hora al comenzar el análisis: \03{lightred}%s\03{default}. language: \03{lightred}%s\03{default}' % (time.strftime("%d-%m-%Y %H:%M:%S", time.localtime()), mode2))
    wikipedia.output('Se han encontrado %s languages para revisar.\n' % len(lista_languages))
    wikipedia.output('\t+---------+------------+-----------+\n\t|language ··|·· total  ··|·· arts. ··|\n\t+---------+------------+-----------+')

    # Variables comunes
    parserData = r"total=(?P<total>.*);good=(?P<good>.*);views=(?P<views>.*);edits=(?P<edits>.*);users=(?P<users>.*);activeusers=(?P<activeusers>.*);admins=(?P<admins>.*);images=(?P<images>.*);jobs=(?P<jobs>.*)"
    partesPage = r""

    articlesT = 0
    filesT = 0
    pagesT = 0
    usersT = 0
    activeUsersT  = 0
    adminsT = 0
    editsT = 0

    data = []
    i = 0

    for language in lista_languages:
        text = urlopen('http://%s.wikipedia.org/wiki/Special:Statistics?action=raw' % (language)).read()
        param = re.search(parserData, text)
        wikipedia.output(' %s    \03{lightpurple}%s\03{default}    ... %s    ... %s' % (i+1, language, param.group('total'), param.group('good')))

        data.append([language, int(param.group('good')), int(param.group('images')), int(param.group('total')), int(param.group('users')), int(param.group('activeusers')), int(param.group('admins')), int(param.group('edits'))])

        # Calculate totals
        articlesT += data[i][1]
        filesT += data[i][2]
        pagesT += data[i][3]
        usersT += data[i][4]
        activeUsersT  += data[i][5]
        adminsT += data[i][6]
        editsT += data[i][7]

        i += 1

    # Sort by number of articles
    data.sort(key=itemgetter(1), reverse=True)

    # Add the total end of the list
    data.append(['total', articlesT, filesT, pagesT, usersT, activeUsersT , adminsT, editsT])

    return data


def generate_text(data, language):
    """
    Generates the corresponding text for each language.
    """
    new = parameters[language]['principio'] + u'<onlyinclude>{{#switch:{{{1}}}\n'
   
    for elem in data:
        new += u"| " + elem[0] + u' = {{#switch:{{{2}}}'
       
        if elem[0]==language and language<>None:
            new += u"\n\t| NUMBEROFARTICLES | ARTICLES = {{NUMBEROFARTICLES:R}}"
            new += u"\n\t| NUMBEROFFILES | FILES = {{NUMBEROFFILES:R}}"
            new += u"\n\t| NUMBEROFPAGES |PAGES = {{NUMBEROFPAGES:R}}"
            new += u"\n\t| NUMBEROFUSERS | USERS = {{NUMBEROFUSERS:R}}"
            new += u"\n\t| NUMBEROFACTIVEUSERS | ACTIVEUSERS = {{NUMBEROFACTIVEUSERS:R}}"
            new += u"\n\t| NUMBEROFADMINS | ADMINS = {{NUMBEROFADMINS:R}}"
            new += u"\n\t| NUMBEROFEDITS | EDITS = {{NUMBEROFEDITS:R}}"
        else:
            new += u"\n\t| NUMBEROFARTICLES | ARTICLES = " + str(elem[1])
            new += u"\n\t| NUMBEROFFILES | FILES = " + str(elem[2])
            new += u"\n\t| NUMBEROFPAGES | PAGES = " + str(elem[3])
            new += u"\n\t| NUMBEROFUSERS | USERS = " + str(elem[4])
            new += u"\n\t| NUMBEROFACTIVEUSERS | ACTIVEUSERS = " + str(elem[5])
            new += u"\n\t| NUMBEROFADMINS | ADMINS = " + str(elem[6])
            new += u"\n\t| NUMBEROFEDITS | EDITS = " + str(elem[7])

        new += u"\n\t| 0 }}\n"

    new += u"| 0 }}</onlyinclude>" + parameters[language]['final']

    return new

def save(data, language):
    """
    Save the text generated for the selected language
    """
    page = wikipedia.Page(language, parameters[language]['page'])
    new = generate_text(data, language)
    page.put(new, parameters[language]['summary'])


data = generate_data()
wikipedia.output('\n')

if mode not in parameters:
    for i in parameters:
        save(data, i)
else:
    save(data, mode)