Utente:ItwikiBot/datiwikipedie.py

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (C) https://it.wikipedia.org/wiki/Utente:Rotpunkt, 2018, under the MIT License
# Aggiorna le statistiche di https://it.wikipedia.org/wiki/Template:Dati_Wikipedie/dati
#

import datetime
from pywikibot.data.wikistats import WikiStats
import pywikibot

def formatStat(args):
        return '''| {prefix} = {{{{#switch:{{{{{{2}}}}}}
        | NUMBEROFARTICLES | ARTICLES = {good}
        | NUMBEROFFILES | FILES = {images}
        | NUMBEROFPAGES | PAGES = {total}
        | NUMBEROFUSERS | USERS = {users}
        | NUMBEROFACTIVEUSERS | ACTIVEUSERS = {activeusers}
        | NUMBEROFADMINS | ADMINS = {admins}
        | NUMBEROFEDITS | EDITS = {edits}
        | 0 }}}}\n'''.format(**args)

def formatMonth(value):
    month_name = {
        1: 'gennaio',
        2: 'febbraio',
        3: 'marzo',
        4: 'aprile',
        5: 'maggio',
        6: 'giugno',
        7: 'luglio',
        8: 'agosto',
        9: 'settembre',
        10: 'ottobre',
        11: 'novembre',
        12: 'dicembre'
    }
    return month_name[value]

def main():
        ws = WikiStats()
        stats = ws.sorted('wikipedia', 'good')
        # rimuove mo.wikipedia.org redirect a ro.wikipedia.org
        stats = [stat for stat in stats if stat['prefix'] != 'mo']
        now = datetime.datetime.now()
        text = '{{#switch:{{{1}}}\n'
        text += '| data = %d %s %d\n' % (now.day, formatMonth(now.month), now.year)
        total = { 'good': 0, 'images': 0, 'total': 0, 'users': 0, 'activeusers': 0, 'admins': 0, 'edits': 0 }
        for stat in stats:
                text += formatStat(stat)
                for key in total:
                        total[key] += int(stat[key])

        total['prefix'] = 'total'
        text += formatStat(total)
        text += '| 0 }}\n'

        pywikibot.handleArgs()
        site = pywikibot.Site()
        page = pywikibot.Page(site, 'Template:Dati Wikipedie/dati')
        page.put(text, 'Bot: aggiornamento dati')

if __name__ == '__main__':
        main()