You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
import json
|
|
import os
|
|
from datetime import datetime
|
|
|
|
from parsers import PARSERS
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
RAW_DIR = os.path.join(BASE_DIR, "raw_data")
|
|
os.makedirs(RAW_DIR, exist_ok=True)
|
|
|
|
|
|
def parse_only(platform, resp_body, req_body=None):
|
|
"""仅解析, 不保存文件"""
|
|
parser = PARSERS.get(platform)
|
|
if not parser:
|
|
return None
|
|
result = parser(resp_body, req_body)
|
|
result["collect_time"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
return result
|
|
|
|
|
|
def save_raw_and_parse(platform, resp_body, req_body=None):
|
|
"""解析 + 保存原始文件 (用于调试)"""
|
|
import uuid as _uuid
|
|
ts = datetime.now().strftime("%Y%m%d_%H%M%S") + "_" + _uuid.uuid4().hex[:6]
|
|
raw_file = os.path.join(RAW_DIR, f"{platform}_{ts}_raw.txt")
|
|
with open(raw_file, "w", encoding="utf-8") as f:
|
|
f.write(resp_body)
|
|
|
|
if req_body:
|
|
req_file = os.path.join(RAW_DIR, f"{platform}_{ts}_req.json")
|
|
with open(req_file, "w", encoding="utf-8") as f:
|
|
f.write(req_body)
|
|
|
|
parser = PARSERS.get(platform)
|
|
if not parser:
|
|
return None
|
|
|
|
result = parser(resp_body, req_body)
|
|
result["collect_time"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
result["raw_file"] = raw_file
|
|
|
|
parsed_file = os.path.join(RAW_DIR, f"{platform}_{ts}_parsed.json")
|
|
with open(parsed_file, "w", encoding="utf-8") as f:
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
|
|
return result
|