pas.py 4.57 KB
Newer Older
Linedwell's avatar
Linedwell committed
1 2
#!/usr/bin/python
# -*- coding: utf-8 -*-
3 4
# Script de nettoyage automatique de la page des PàS

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

Linedwell's avatar
Linedwell committed
11 12 13 14

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

15
import mylogging
Linedwell's avatar
Linedwell committed
16

Linedwell's avatar
Linedwell committed
17 18 19 20 21
import re
import time
import pywikibot

#Variables globales
22
site = pywikibot.Site('fr','wikipedia')
Linedwell's avatar
Linedwell committed
23

24
delimiterBegin = u"= Propositions =\n"
25
delimiterEnd = u"= Maintenance : mise à jour =\n"
26
    
Linedwell's avatar
Linedwell committed
27 28
delimiterBeginRegex = ur"=\s*Propositions\s*=\n"
delimiterEndRegex = ur"=\s*Maintenance : mise à jour\s*=\n"
29

Linedwell's avatar
Linedwell committed
30 31
summary = ""

Linedwell's avatar
Linedwell committed
32 33 34 35
#Créé la section PàS du lendemain
def pasNewSection(pageTemp):
    global summary
    
36
    month = [u"",u"janvier",u"février",u"mars",u"avril",u"mai",u"juin",u"juillet",u"août",u"septembre",u"octobre",u"novembre",u"décembre"]
Linedwell's avatar
Linedwell committed
37 38 39 40 41 42
    pad = 1 #nombre de jours de decalage que l'on souhaite
    dateD = time.localtime(time.time() + pad * 24 * 3600)
    year = int(dateD.tm_year)
    monthNum = int(dateD.tm_mon)
    dayNum = int(dateD.tm_mday)
    
43
    if pageTemp.find(u"== " + str(dayNum) + " " + month[monthNum] + " ==") == -1: #La section de dans 'pad' n'existe pas encore
44
        pageTempBegin, pageTempEnd = re.split(delimiterEndRegex,pageTemp)
45
        pageTempEnd = delimiterEnd + pageTempEnd
Linedwell's avatar
Linedwell committed
46
        
47 48 49
        newSection = u"== " + str(dayNum) + " " + month[monthNum] + " ==\n"
        newSection += u"{{En-tête section PàS|" + str(dayNum)+ "|" + month[monthNum] + "|" + str(year) + "}}\n\n"
        newSection += u"{{Boîte déroulante/début|titre=Requêtes traitées}}\n{{Boîte déroulante/fin}}\n\n"
Linedwell's avatar
Linedwell committed
50 51 52 53
        
        pageTemp = pageTempBegin + newSection + pageTempEnd
        
        if summary != u'':
54
            summary += u" ; initialisation de la section PàS du %s %s" %(dayNum,month[monthNum])
Linedwell's avatar
Linedwell committed
55
        else:
56
            summary += u"initialisation de la section PàS du %s %s" %(dayNum,month[monthNum])
Linedwell's avatar
Linedwell committed
57 58
    
    else:
59 60
        pywikibot.output(u"Aucune modification, la section du %s %s existe."
                         %(dayNum,month[monthNum]))
Linedwell's avatar
Linedwell committed
61 62 63

    return pageTemp

Linedwell's avatar
Linedwell committed
64
#Archive les sections vidées
Linedwell's avatar
Linedwell committed
65 66 67
def pasRemoveSection(pageTemp):
    global summary

68
    pageTempBegin, pageTempBody = re.split(delimiterBeginRegex,pageTemp)
Linedwell's avatar
Linedwell committed
69 70
    pageTempBegin += delimiterBegin
    
71
    pageTempBody, pageTempEnd = re.split(delimiterEndRegex,pageTempBody)
Linedwell's avatar
Linedwell committed
72 73
    pageTempEnd = delimiterEnd + pageTempEnd
    
Linedwell's avatar
Linedwell committed
74
    section = re.split(r'(==\s*\d*(?:er)? \w*\s*==\n)',pageTempBody,flags=re.U)
75
    parser = re.compile(ur'{{En-tête section PàS\|.*?}}\s*{{Boîte déroulante\/début\|.*?}}', re.U | re.I | re.M)
Linedwell's avatar
Linedwell committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    
    pageTempBody = u''
    count = 0

    for i in range(2,len(section),2):
        result = parser.search(section[i])
        if result:
            section[i] = section[i-1] = ''
            count += 1

    if count > 0:
        for s in section:
            pageTempBody += s

        pageTemp = pageTempBegin + pageTempBody + pageTempEnd
91
        summary = u"archivage de %s section(s)" %(count)
Linedwell's avatar
Linedwell committed
92 93 94

    return pageTemp

Linedwell's avatar
Linedwell committed
95
#Exécution
Linedwell's avatar
Linedwell committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
def main(argv):
    global summary
    
    archiveOnly = False
    try:
        opts, args = getopt.getopt(argv, 'a',['archive'])
    except getopt.GetoptError:
        print "usage: pas.py [-a]"
        exit(2)

    for opt, args in opts:
        if opt in ('-a', '--archive'):
            archiveOnly = True

    summary = u''
111
    target = u"Wikipédia:Pages à supprimer"
Linedwell's avatar
Linedwell committed
112 113
    page = pywikibot.Page(site,target)

114 115 116 117 118 119 120 121 122 123 124 125 126
    try:
        pageTemp = page.get()
        
    except pywikibot.NoPage:
        pywikibot.output(u"Page %s does not exist; skipping."
                         % page.title(asLink=True))
    except pywikibot.IsRedirectPage:
        pywikibot.output(u"Page %s is a redirect; skipping."
                         % page.title(asLink=True))
    except pywikibot.LockedPage:
        pywikibot.output(u"Page %s is locked; skipping."
                         % page.title(asLink=True))
    else:
Linedwell's avatar
Linedwell committed
127

128
        pageTempNew = pasRemoveSection(pageTemp)
Linedwell's avatar
Linedwell committed
129

130 131 132 133
        if not archiveOnly:
            pageTempNew = pasNewSection(pageTempNew)

        if pageTempNew != pageTemp:
134 135
            page.text = pageTempNew
            page.save("[[WP:Bot|Robot]] : " + summary)
Linedwell's avatar
Linedwell committed
136

137
        else:
Linedwell's avatar
Linedwell committed
138
            pywikibot.output("Aucune action aujourd'hui, archivage et section du lendemain non requis.")
Linedwell's avatar
Linedwell committed
139 140 141 142 143 144 145
    


if __name__ == "__main__":
    try:
        main(sys.argv[1:])
    finally:
146
        pywikibot.stopme()