rights-watcher.py 2.58 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script de suivi des modifications de droits utilisateur sur Vikidia.

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


import sys
sys.path.insert(1, '..') #ajoute au PYTHONPATH le répertoire parent

15
import mylogging
16 17 18

from datetime import datetime

Linedwell's avatar
Linedwell committed
19 20
import pywikibot

21 22 23 24 25 26 27
import mailer


# Déclarations
sites = {
    'fr' : pywikibot.Site('fr','vikidia'),
    'it' : pywikibot.Site('it','vikidia'),
Linedwell's avatar
Linedwell committed
28
    'de' : pywikibot.Site('de','vikidia'),
29 30 31 32 33 34
    'en' : pywikibot.Site('en','vikidia'),
    'es' : pywikibot.Site('es','vikidia'),
    'ru' : pywikibot.Site('ru','vikidia'),
    'ca' : pywikibot.Site('ca','vikidia'),
    'eu' : pywikibot.Site('eu','vikidia'),
    'scn' : pywikibot.Site('scn','vikidia'),
35
    #'test' : pywikibot.Site('test','vikidia'),
Linedwell's avatar
Linedwell committed
36
    'central' : pywikibot.Site('central','vikidia'),
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
}

### debug ###
for s in sites:
    sites[s].login()
### end debug

#Recuperation des journaux de modification de droits "sensibles"
def rights_logs(end):
    watched_rights = ["bureaucrat","sysop","nuker","abusefilter","bot","checkuser","oversight","developer"]
    report = ""
    
    for s in sites:
        site = sites[s]
        logevents = site.logevents(logtype="rights",end=end)

        for le in logevents:
            oldgroups = le.oldgroups
            newgroups = le.newgroups
            
            #Différence symétrique entre les deux listes, on regarde quels droits ont été retirés/ajoutés
            diff = list(set(oldgroups).symmetric_difference(set(newgroups)))
            
            # Si l'un des droits modifiés figure dans la liste des droits surveillés
            if (set(diff).intersection(watched_rights)):
            
Linedwell's avatar
Linedwell committed
63
                report += "[" + site.code + "][" + str(le.timestamp()) + "] " + le.user() + " -> " + le.page().title()  + " (" + str(oldgroups) + " -> " + str(newgroups) + ")\n"
64 65 66 67 68 69

    return report

#Envoie par mail le rapport de modification des droits
def mail_report(report):
    if report != "":
Linedwell's avatar
Linedwell committed
70
	#print report
71 72 73 74 75
        mailer.send('[RW] Modification de droits sur Vikidia.',report)

#Exécution
def main():
    last_check_file = 'rights-watcher.last'
Linedwell's avatar
Linedwell committed
76 77 78
    bfile = open(last_check_file,'r')
    last_check = bfile.read()
    bfile.close()
79 80 81 82 83

    report = rights_logs(last_check)
    mail_report(report)
    
    now = datetime.utcnow()
Linedwell's avatar
Linedwell committed
84 85 86
    bfile = open(last_check_file,'w')
    bfile.write(now.isoformat())
    bfile.close()
87 88 89 90 91 92

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