bas.py 1.96 KB
Newer Older
1 2 3
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script de nettoyage du bac à sable de Vikidia
4

Linedwell's avatar
Linedwell committed
5
# (C) Linedwell, 2011-2019
6 7 8 9
#
# Distribué sous licence GNU GPLv3
# Distributed under the terms of the GNU GPLv3 license
# http://www.gnu.org/licenses/gpl.html
10

Linedwell's avatar
Linedwell committed
11
import sys, getopt
12 13
sys.path.insert(1, '..') #ajoute au PYTHONPATH le répertoire parent

14
import mylogging
Linedwell's avatar
Linedwell committed
15

16
import pywikibot
Linedwell's avatar
Linedwell committed
17
from datetime import datetime, timedelta
18 19

# Déclarations
20 21 22 23 24 25 26
dicoFR = {
    'site' : pywikibot.Site('fr','vikidia'),
    'page' : u"Vikidia:Bac à sable",
    'template' : u"{{subst:Vikidia:Bac à sable/Zéro}}",
    'summary' : u"[[Vikidia:Robot|Robot]] : Nettoyage du bac à sable",
    'delay' : 30,
}
Linedwell's avatar
Linedwell committed
27

28 29 30 31 32 33 34
dicoIT = {
    'site' : pywikibot.Site('it','vikidia'),
    'page' : u"Vikidia:Area giochi",
    'template' : u"{{subst:Vikidia:Area giochi/Zero}}",
    'summary' : u"[[Vikidia:Bot|Bot]] : Pulizia dell'area giochi",
    'delay' : 30,
}
Linedwell's avatar
Linedwell committed
35

36 37

#Recharge le bac à sable avec un contenu prédéfini
Linedwell's avatar
Linedwell committed
38
def clean(dico, force=False):
39 40 41 42 43
    site = dico['site']
    page = pywikibot.Page(site,dico['page'])
    template = dico['template']
    summary = dico['summary']
    delay = dico['delay']
Linedwell's avatar
Linedwell committed
44
    site.login() # force login for below check
45 46
    if not page.userName() == site.user():
        limite = calcLimit(delay)
Linedwell's avatar
Linedwell committed
47
        if (page.editTime() < limite) or force:
48 49
            page.text = template
            page.save(summary)
50 51 52 53 54 55 56 57 58 59

#Calcule la "date" avant laquelle on s'autorise à blanchir le bas
def calcLimit(delay):
    today = datetime.utcnow()
    limit = today - timedelta(minutes=delay)
    return limit


#Exécution
def main():
Linedwell's avatar
Linedwell committed
60 61 62 63 64 65 66 67 68 69 70 71 72
    force = False

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'f', ['force'])
    except getopt.GetoptError:
        sys.exit(2)

    for opt, arg in opts:
        if opt in ('-f', '--force'):
            force = True

    clean(dicoFR, force) #nettoyage fr
    clean(dicoIT, force) #nettoyage it
73 74 75 76 77 78

if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()