Bikarhêner:Balyozxane/skrîpt/py/category creator run.py

Ji Wîkîpediya, ensîklopediya azad.
"""
Bi alîkariya [[Bikarhêner:Balyozxane/skrîpt/py/category creator.py]] kategoriyên ku tên xwestin çêdike. Wisa:

python pwb.py category_creator_run -lang:ku -family:wikipedia -wantedcategories
"""

import pywikibot
from pywikibot.bot import SingleSiteBot, ConfigParserBot, AutomaticTWSummaryBot
from pywikibot import pagegenerators
from pywikibot.exceptions import LockedPageError
import re
from category_creator import CategoryCreator  # Import the CategoryCreator class
bedeng = False

def print_sirove(sirove):
    if not bedeng:
        return pywikibot.output(sirove)

class AppendTextBot(
    SingleSiteBot,
    ConfigParserBot,
    AutomaticTWSummaryBot,
):
    summary_key = 'basic-changing'
    use_redirects = False
    update_options = {
        'summary': None,
        'text': '',
        'top': False,
    }

    def __init__(self, *args, category_creator=None, **kwargs):
        self.category_creator = category_creator
        super().__init__(*args, **kwargs)

    def treat_page(self) -> None:
        page = self.current_page

        if page.exists():
            pywikibot.output(f"Skipping {page.title()} already exists.")
            return

        self.category_creator.create_ku_category(page.title(with_ns=False))
        
def main(*args: str) -> None:
    local_args = pywikibot.handle_args(args)
    gen_factory = pagegenerators.GeneratorFactory()
    local_args = gen_factory.handle_args(local_args)

    options = {'text': ''}

    for arg in local_args:
        option, _, value = arg.partition(':')
        if option in ('summary', 'text'):
            if not value:
                pywikibot.input(f'Please enter a value for {option}')
            options[option] = value
        else:
            options[option] = True
    category_creator = CategoryCreator()
    gen = gen_factory.getCombinedGenerator(preload=True)

    if not pywikibot.bot.suggest_help(missing_generator=not gen):
        bot = AppendTextBot(generator=gen, category_creator=category_creator, **options)
        bot.run()

if __name__ == '__main__':
    main()