admissibilite.py 2.97 KB
Newer Older
1 2 3
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script indiquant les changements récents (ajouts / retraits) dans la Catégorie:Admissibilité à vérifier
4

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

11 12 13 14

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

Linedwell's avatar
Linedwell committed
15 16 17
import time
import shutil

18
import mylogging
Linedwell's avatar
Linedwell committed
19

20 21
import pywikibot
from pywikibot import pagegenerators
22
import callback
23
import logger
24
import grapher
Linedwell's avatar
Linedwell committed
25

26
# Déclarations
Linedwell's avatar
Linedwell committed
27
site = pywikibot.Site('fr', 'wikipedia')
28

Linedwell's avatar
Linedwell committed
29 30
def admissibilite(pagesList):
    log = u''
31 32
    nbAdd = 0
    nbRem = 0
Linedwell's avatar
Linedwell committed
33 34
    backupList = loadBackupFile()
    actualList = titleList(pagesList)
35
    total = len(actualList)
Linedwell's avatar
Linedwell committed
36 37 38 39

    addList = list(set(actualList) - set(backupList))
    remList = list(set(backupList) - set(actualList))

40
    for add in addList:
41
        log += u"* {{Vert|'''+'''}} ajout du bandeau sur [[%s]]\n" %(add)
42 43 44
        nbAdd += 1

    for rem in remList:
45
        log += u"* {{Rouge|'''-'''}} retrait du bandeau sur [[%s]]\n" %(rem)
46 47
        nbRem += 1

Linedwell's avatar
Linedwell committed
48
    saveBackupFile(actualList)
49

Linedwell's avatar
Linedwell committed
50
    summary = u"Mise à jour (+%s; -%s; =%s)" %(nbAdd, nbRem, total)
51

52
    return log, summary, total
53

Linedwell's avatar
Linedwell committed
54
#Return the content of the given category (only pages from namespace 0)
55
def getCategoryContent(catname):
Linedwell's avatar
Linedwell committed
56
    cat = pywikibot.Category(site, catname)
57
    pagesInCat = list(cat.articles(False))
Linedwell's avatar
Linedwell committed
58
    pagesList = pagegenerators.PreloadingGenerator(pagesInCat) # On génère la liste des pages incluses dans la catégorie
Linedwell's avatar
Linedwell committed
59
    pagesList = pagegenerators.NamespaceFilterPageGenerator(pagesList, [0]) #On ne garde que les articles (Namespace 0)
Linedwell's avatar
Linedwell committed
60
    
61 62
    return pagesList

Linedwell's avatar
Linedwell committed
63
#Return the list of titles from pagesList
64
def titleList(pagesList):
Linedwell's avatar
Linedwell committed
65
    tlist = []
66
    for page in pagesList:
Linedwell's avatar
Linedwell committed
67
        tlist.append(page.title())
68
    
Linedwell's avatar
Linedwell committed
69
    return tlist
70

Linedwell's avatar
Linedwell committed
71
#Save a list to the backup file
Linedwell's avatar
Linedwell committed
72
def saveBackupFile(tlist):
Linedwell's avatar
Linedwell committed
73
    bfile = open('_admissibilite.bak', 'w+')
Linedwell's avatar
Linedwell committed
74 75 76
    for s in tlist:
        bfile.write(s.encode('utf-8') + '\n')
    bfile.close()
77

Linedwell's avatar
Linedwell committed
78
#Load a list from the backup file
79
def loadBackupFile():
Linedwell's avatar
Linedwell committed
80
    bfile = open('_admissibilite.bak', 'r')
Linedwell's avatar
Linedwell committed
81
    oldList = bfile.readlines()
82 83
    oldList = [s.strip('\n') for s in oldList]
    oldList = [s.decode('utf-8') for s in oldList]
Linedwell's avatar
Linedwell committed
84
    bfile.close()
85 86 87 88 89 90
    
    return oldList


#Exécution
def main():
Linedwell's avatar
Linedwell committed
91
    log = u''
92
    timeStart = time.time()
Linedwell's avatar
Linedwell committed
93
    shutil.copyfile('_admissibilite.bak', '_admissibilite.bak.bak')
94
    catname = u"Tous les articles dont l'admissibilité est à vérifier"
Linedwell's avatar
Linedwell committed
95
    pagesList = getCategoryContent(catname)
96 97
    log, summary, total = admissibilite(pagesList)
    grapher.update(total)
Linedwell's avatar
Linedwell committed
98
    logger.editLog(site, log, page=u"Projet:Maintenance/Suivi d'admissibilité", summary=summary, ar=False, cl=15)
99
    pywikibot.output(summary)
100
    timeEnd = time.time()
Linedwell's avatar
Linedwell committed
101 102 103
    pywikibot.output(u"%s in %s s."
                        %(summary, round(timeEnd-timeStart, 2)))

104 105 106 107 108 109

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