From af03fc7c1731886fb97da6f0422f70f0a28ee0ec Mon Sep 17 00:00:00 2001 From: hxiong Date: Thu, 2 May 2024 21:57:08 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E6=A0=B9=E6=8D=AE=E5=8D=95=E6=9D=A1?= =?UTF-8?q?=E7=BA=AA=E5=BD=95=E7=9A=84=E5=93=8D=E5=BA=94=E5=AF=84=E8=BF=87?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=AF=B9=E5=BA=94=E7=9A=84json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ run_page/codoon_sync.py | 5 ++-- run_page/config.py | 1 + run_page/joyrun_sync.py | 5 ++-- run_page/keep_sync.py | 4 ++-- run_page/utils.py | 51 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 6fff8702eb7..2f18d6e12d0 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,7 @@ _site/ config.yaml .venv/ +GPX_OUT +TCX_OUT +FIT_OUT +RESPONSE_OUT diff --git a/run_page/codoon_sync.py b/run_page/codoon_sync.py index 46b7beed0e7..33083627196 100755 --- a/run_page/codoon_sync.py +++ b/run_page/codoon_sync.py @@ -27,7 +27,7 @@ ) from generator import Generator from tzlocal import get_localzone -from utils import adjust_time_to_utc, adjust_timestamp_to_utc, to_date +from utils import adjust_time_to_utc, adjust_timestamp_to_utc, to_date, write_response # struct body FitType = np.dtype( @@ -478,7 +478,8 @@ def parse_points_to_gpx(self, run_points_data): point = gpxpy.gpx.GPXTrackPoint(**p) gpx_segment.points.append(point) return gpx.to_xml() - + + @write_response("codoon") def get_single_run_record(self, route_id): print(f"Get single run for codoon id {route_id}") payload = { diff --git a/run_page/config.py b/run_page/config.py index 1548d5ea80c..d79e9da7d4d 100644 --- a/run_page/config.py +++ b/run_page/config.py @@ -10,6 +10,7 @@ GPX_FOLDER = os.path.join(parent, "GPX_OUT") TCX_FOLDER = os.path.join(parent, "TCX_OUT") FIT_FOLDER = os.path.join(parent, "FIT_OUT") +RESPONSE_OUT = os.path.join(parent, "RESPONSE_OUT") ENDOMONDO_FILE_DIR = os.path.join(parent, "Workouts") FOLDER_DICT = { "gpx": GPX_FOLDER, diff --git a/run_page/joyrun_sync.py b/run_page/joyrun_sync.py index 1d2e88c0325..0ecc0bf3013 100755 --- a/run_page/joyrun_sync.py +++ b/run_page/joyrun_sync.py @@ -14,7 +14,7 @@ from config import BASE_TIMEZONE, GPX_FOLDER, JSON_FILE, SQL_FILE, run_map, start_point from generator import Generator -from utils import adjust_time +from utils import adjust_time, write_response get_md5_data = lambda data: md5(str(data).encode("utf-8")).hexdigest().upper() @@ -243,7 +243,8 @@ def parse_points_to_gpx( gpx_segment.points.append(point) return gpx.to_xml() - + + @write_response("joyrun") def get_single_run_record(self, fid): payload = { "fid": fid, diff --git a/run_page/keep_sync.py b/run_page/keep_sync.py index 66d8eaf60a9..72087499484 100755 --- a/run_page/keep_sync.py +++ b/run_page/keep_sync.py @@ -14,7 +14,7 @@ from config import GPX_FOLDER, JSON_FILE, SQL_FILE, run_map, start_point from Crypto.Cipher import AES from generator import Generator -from utils import adjust_time +from utils import adjust_time,write_response import xml.etree.ElementTree as ET KEEP_SPORT_TYPES = ["running", "hiking", "cycling"] @@ -73,7 +73,7 @@ def get_to_download_runs_ids(session, headers, sport_type): break return result - +@write_response('keep') def get_single_run_data(session, headers, run_id, sport_type): r = session.get( RUN_LOG_API.format(sport_type=sport_type, run_id=run_id), headers=headers diff --git a/run_page/utils.py b/run_page/utils.py index de6945ee48a..6a74dff4525 100644 --- a/run_page/utils.py +++ b/run_page/utils.py @@ -1,6 +1,8 @@ import json import time +import os from datetime import datetime +from config import RESPONSE_OUT import pytz @@ -117,3 +119,52 @@ def upload_file_to_strava(client, file_name, data_type, force_to_run=True): print( f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}." ) + +# 根据单条纪录的响应寄过生成对应的文件名 +def keep_handler(response): + s_type = response['data']['type'] + s_subtype = response['data']['subtype'] + s_time = response['data']['startTime'] + return f'{s_type}_{s_subtype}_{s_time}.json' + +def codoon_handler(response): + s_type = response['data']['activity_type'] + s_subtype = response['data']['sports_type'] + s_time = response['data']['StartDateTime'] + return f'{s_type}_{s_subtype}_{s_time}.json' + +def joyrun_handler(response): + s_type = response['runrecord']['type'] + s_time = response['runrecord']['starttime'] + return f'{s_type}_{s_time}.json' + +def write_response(file_type): + file_name_handler = None + if file_type == "keep": + file_name_handler = keep_handler + elif file_type == "codoon": + file_name_handler = codoon_handler + elif file_type == "joyrun": + file_name_handler = joyrun_handler + + def decorator(func): + def wrapper(*args, **kwargs): + try: + response = func(*args, **kwargs) + if file_name_handler is None: + return response + + file_path = os.path.join(RESPONSE_OUT, file_type) + file_name = file_name_handler(response) + full_file_path = os.path.join(file_path, file_name) + if not os.path.exists(file_path): + os.makedirs(file_path) + response_json = json.dumps(response, indent=4) + with open(full_file_path, "w") as fb: + fb.write(response_json) + return response + except Exception as e: + print(f"Error: {str(e)}") + return None + return wrapper + return decorator \ No newline at end of file