User:Şêr/newentry.py

From Wiktionary, the free dictionary
Jump to navigation Jump to search
import pywikibot

def edit_wiktionary_from_file(file_path):
    site = pywikibot.Site('en', 'wiktionary')

    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

        pages = content.split("xxxx")[1:]

        for page_content in pages:
            title_start = page_content.find("'''") + 3
            title_end = page_content.find("'''", title_start)
            page_title = page_content[title_start:title_end]

            content_start = title_end + 3
            content_end = page_content.find("yyyy")
            page_content = page_content[content_start:content_end].strip()

            edit_wiktionary_page(site, page_title, page_content)

def edit_wiktionary_page(site, page_title, new_content):
    print(f'Sernavê rûpelê: {page_title}')
    page = pywikibot.Page(site, page_title)

    if page.exists():
        print(f'Rûpela {page_title} heye. Bername derbas dibe.')
        return

    page.text = new_content

    page.save(f"Rûpela {page_title} hat afirandin.")


if __name__ == "__main__":
    # Lîsteya agahiyan
    input_file_path = 'danegeh.txt'
    edit_wiktionary_from_file(input_file_path)