# -*- coding: utf-8 -*-
import pywikibot

def fetch_pages_in_category(site, category_title):
    category = pywikibot.Category(site, 'Kategorî:' + category_title)
    page_titles = [page.title() for page in category.members()]
    return page_titles

def fetch_page_data(site, page_titles):
    page_data = []

    for title in page_titles:
        page = pywikibot.Page(site, title)
        if page.exists():
            page_data.append((title, page.text))

    return page_data

def save_page_data_to_file(page_data, file_path):
    with open(file_path, 'w', encoding='utf-8') as file:
        for title, content in page_data:
            file.write(f'== {title} ==\n\n{content}\n\n')

def main():
    # Malper
    site = pywikibot.Site('ku', 'wiktionary')

    # Pirsîna navê kategoriyê
    category_title = input("Navê kategoriyê binivîse (wek mînak 'Tewandin:lêker (kurdî)'): ")

    page_titles_to_fetch = fetch_pages_in_category(site, category_title)

    pages_data = fetch_page_data(site, page_titles_to_fetch)

    file_path = 'danegeh.txt'

    save_page_data_to_file(pages_data, file_path)

if __name__ == "__main__":
    main()