Gebruiker:Bitbotje/bulkcategorymove.py

import pwb
import pywikibot
from pywikibot.backports import Tuple
import scripts.category
from scripts.category import CategoryMoveRobot

def main(*args: Tuple[str, ...]) -> None:
    summary = ''
    
    local_args = pywikibot.handle_args(args)
    for arg in local_args:
        option, _, value = arg[1:].partition(':')
        if option == 'summary':
            summary = value
    
    inputFile = open('input.txt', 'r')
    site = pywikibot.Site()
    for line in inputFile:
        old_cat_title, new_cat_title = line.split('\t')

        # call category.py
        bot = CategoryMoveRobot(oldcat=old_cat_title,
                                newcat=new_cat_title,
                                batch=False,
                                comment=summary,
                                inplace=False,
                                delete_oldcat=True,
                                title_regex=False,
                                history=False,
                                pagesonly=False,
                                deletion_comment=False,
                                wikibase=True,
                                allow_split=False,
                                move_together=False,
                                keep_sortkey=True)
        try:
            bot.run()
        except Error:
            pywikibot.error('Fatal error:', exc_info=True)

        # update Wikidata label
        page = pywikibot.Page(site, new_cat_title, ns=14)
        try:
            item = pywikibot.ItemPage.fromPage(page)
            item.get()
            item.editLabels(labels={'nl': page.title(with_ns=True)}, summary='Update [nl] label')
        except pywikibot.exceptions.NoPageError:
            pass

    inputFile.close()
    print('Done.')


if __name__ == '__main__':
    main()