Bikarhêner:Balyozxane/skrîpt/py/parambikejson.py

#!/usr/bin/python3
"""
python pwb.py updatewin -file:"parambikejson.py" -s:"call kategoriyensitilan.py"
"""
import os
import pywikibot
import mwparserfromhell as mw
import json


def parse_rename_templates(page_text):
    parsed_text = mw.parse(page_text)
    rename_templates = parsed_text.filter_templates(matches="AWB rename template parameter")
    rename_info = {}

    for template in rename_templates:
        template_name = template.get(1).value.strip()
        deprecated_param = template.get(2).value.strip()
        new_param = template.get(3).value.strip()

        if template_name not in rename_info:
            rename_info[template_name] = {}

        rename_info[template_name][deprecated_param] = new_param

    return rename_info


def main():
    site = pywikibot.Site('ku', 'wikipedia')
    awb_page = pywikibot.Page(site, 'Wîkîpediya:AutoWikiBrowser/Rename template parameters')
    user_page = pywikibot.Page(site, 'Wîkîpediya:AutoWikiBrowser/Rename template parameters.json')
    page_text = awb_page.text
    rename_info = parse_rename_templates(page_text)

    with open('parambikejson.json', 'w', encoding='utf-8') as json_file:
        json.dump(rename_info, json_file, indent=4, ensure_ascii=False)

    user_page.text = json.dumps(rename_info, indent=4, ensure_ascii=False)
    user_page.save(summary='JSON data hat rojanekirin', botflag=True)

    os.system(f"$HOME/pwbvenv/bin/pwb kategoriyensablonan")


if __name__ == "__main__":
    main()