import time

import requests
import json

number_of_results = 1
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}

language_code = 'ja'
base_url = 'https://api.wikimedia.org/core/v1/wikipedia/'
endpoint = '/search/page'
url = base_url + language_code + endpoint
proxies = {"http": "http://127.0.0.1:10809", "https": "http://127.0.0.1:10809"}
if __name__ == '__main__':
    s_file = open("E:\yuxin\org_wiki.txt", 'r', encoding='utf-8')
    s_file_content = s_file.read().split('\n')
    s_file.close()
    count = 0
    for search_query in s_file_content:
        count += 1
        print(f"[No {count}/{len(s_file_content)}] Searching \"{search_query}\"")
        parameters = {'q': search_query, 'limit': 3}
        time.sleep(0.3)
        try:
            response = requests.get(url, headers=headers, params=parameters, proxies=proxies)
            rsp_body = json.loads(response.text)
            if 'pages' in rsp_body and len(rsp_body['pages']) > 0:
                pages = rsp_body['pages']
                matched_page = pages[0]
                print('https://ja.wikipedia.org/wiki/' + matched_page['key'])
                # print('https://zh.wikipedia.org/zh-cn/' + matched_page['key'])
            else:
                print("=== No matching content ===")
        except Exception as e:
            print("=== Request Error: {} ===".format(repr(e)))
