Vés al contingut

Usuari:CobainBot/scripts/caquotes/sort categories.py

De la Viquipèdia, l'enciclopèdia lliure
import re
import pywikibot as pwb
from pywikibot import pagegenerators


def main():
    pattern = re.compile(r'\[\[ *[Cc]ategoria *: *[^\]]+ *\]\]')
    for page in pagegenerators.AllpagesPageGenerator(site=site, namespace=0, includeredirects=False):
        text = page.get()
        old_text = str(text)
        for i, old_cat in enumerate(old_cats):
            old_text = old_text.replace(old_cat, f'<cat_{i}/>')
        new_text = str(old_text)
        new_cats = list(sorted(old_cats))
        for j, new_cat in enumerate(new_cats):
            new_text = new_text.replace(f'<cat_{j}/>', new_cat)
        if not debug and new_text != text:
            page.put(new_text, summary='Bot: Ordenant alfabèticament les categories')


if __name__ == '__main__':
    try:
        debug = False
        site = pwb.Site("ca", "wikiquote", "CobainBot")
        main()
    except KeyboardInterrupt:
        pass