ChinaHamRadioQuest/utils/updateQuestLibrary.py
2024-10-18 20:24:15 +08:00

51 lines
1.2 KiB
Python

"""
updateQuestLibrary.py
"""
import os
import json
import requests
baseURL = 'https://www.cqid.cn/api/getAll/'
def post_quest_library(type_str):
"""
postQuestLibrary function
method POST
content-type: application/x-www-form-urlencoded
body: type=type_str
"""
headers = {
'content-type': 'application/x-www-form-urlencoded'
}
body = {
'type': type_str
}
response = requests.post(baseURL, headers=headers, data=body)
return response
def remove_answer_list(json_content):
for i in range(len(json_content)):
if 'answer' in json_content[i]:
json_content[i].pop('answer')
return json_content
def main():
os.path.abspath(os.path.dirname(__file__))
typeList = ['A', 'B', 'C']
for type_str in typeList:
response = post_quest_library(type_str).text
print(f"Get quests of type {type_str}.")
json_content = remove_answer_list(json.loads(response))
# write to file ../quests/quests_x.json
with open(f'../quests/quests_{type_str}.json', 'w', encoding='utf-8') as f:
json.dump(json_content, f, ensure_ascii=False, indent=4)
print(f"Write quests of type {type_str} to file.")
if __name__ == '__main__':
main()