Files
wiki_crawler/nodes/parse_register.py

40 lines
975 B
Python
Raw Normal View History

2025-12-28 10:33:47 +08:00
import json
def parse_response(status_code: float, body: str):
2025-12-22 22:08:51 +08:00
'''
检查状态码和约定的返回值
2025-12-28 10:33:47 +08:00
并且返回正确的body
2025-12-22 22:08:51 +08:00
'''
if status_code != 200:
raise Exception(f"注册任务失败,状态码:{status_code}")
2025-12-28 10:33:47 +08:00
data = json.loads(body)
if "code" not in data or data["code"] != 1:
2025-12-22 22:08:51 +08:00
raise Exception(f"注册任务失败,返回值:{body}")
2025-12-28 10:33:47 +08:00
return data["data"]
2025-12-22 22:08:51 +08:00
def main(status_code: float, body: str):
try:
2025-12-28 10:33:47 +08:00
data = parse_response(status_code, body)
2025-12-22 22:08:51 +08:00
except Exception as e:
raise e
2025-12-28 10:33:47 +08:00
task_id = data["task_id"]
is_new_task = data["is_new_task"]
2025-12-22 22:08:51 +08:00
return {
"task_id": task_id,
"is_new_task": is_new_task
}
2025-12-28 10:33:47 +08:00
def test():
import json
with open("nodes\parse_register.json", "r") as f:
data = json.load(f)
status_code = data["status_code"]
body = data["body"]
res = main(status_code, body)
print(res)
test()