From d730cb5dd85b5d8fb7f16eb0d75d4938ea47bf80 Mon Sep 17 00:00:00 2001 From: zhangyulai Date: Fri, 7 Jul 2023 17:35:26 +0800 Subject: [PATCH] feat:support for download Garmin fit file (#446) * feat:support for download Garmin fit file --- FIT_OUT/.gitkeep | 0 README-CN.md | 2 ++ README.md | 2 ++ scripts/config.py | 2 ++ scripts/garmin_sync.py | 26 ++++++++++++++++++++++++++ 5 files changed, 32 insertions(+) create mode 100644 FIT_OUT/.gitkeep diff --git a/FIT_OUT/.gitkeep b/FIT_OUT/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/README-CN.md b/README-CN.md index 1954aa48836..1f6da04e3aa 100644 --- a/README-CN.md +++ b/README-CN.md @@ -85,6 +85,8 @@ python3(python) scripts/xingzhe_sync.py 13333xxxx xxxx > 注:我增加了 行者 可以导出 gpx 功能, 执行如下命令,导出的 gpx 会加入到 GPX_OUT 中,方便上传到其它软件 +如果你想同步 `fit` 格式,增加命令 --fit + ```python python3(python) scripts/xingzhe_sync.py ${your mobile or email} ${your password} --with-gpx ``` diff --git a/README.md b/README.md index 035257d50c4..468df4e9f41 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ python3(python) scripts/codoon_sync.py 54bxxxxxxx fefxxxxx-xxxx-xxxx --from-auth
Get your Xingzhe data +If you only want `fit` files add args --fit + ```python python3(python) scripts/xingzhe_sync.py ${your mobile or email} ${your password} ``` diff --git a/scripts/config.py b/scripts/config.py index a7b33f48dd9..83a7a012e24 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -9,10 +9,12 @@ OUTPUT_DIR = os.path.join(parent, "activities") GPX_FOLDER = os.path.join(parent, "GPX_OUT") TCX_FOLDER = os.path.join(parent, "TCX_OUT") +FIT_FOLDER = os.path.join(parent, "FIT_OUT") ENDOMONDO_FILE_DIR = os.path.join(parent, "Workouts") FOLDER_DICT = { "gpx": GPX_FOLDER, "tcx": TCX_FOLDER, + "fit": FIT_FOLDER, } SQL_FILE = os.path.join(parent, "scripts", "data.db") JSON_FILE = os.path.join(parent, "src", "static", "activities.json") diff --git a/scripts/garmin_sync.py b/scripts/garmin_sync.py index fbea0010435..7c1975802ce 100644 --- a/scripts/garmin_sync.py +++ b/scripts/garmin_sync.py @@ -12,6 +12,7 @@ import sys import time import traceback +import zipfile import aiofiles import cloudscraper @@ -185,6 +186,10 @@ async def get_activities(self, start, limit): async def download_activity(self, activity_id, file_type="gpx"): url = f"{self.modern_url}/proxy/download-service/export/{file_type}/activity/{activity_id}" + if file_type == "fit": + url = ( + f"{self.modern_url}/proxy/download-service/files/activity/{activity_id}" + ) logger.info(f"Download activity from {url}") response = await self.req.get(url, headers=self.headers) response.raise_for_status() @@ -290,8 +295,21 @@ async def download_garmin_data(client, activity_id, file_type="gpx"): try: file_data = await client.download_activity(activity_id, file_type=file_type) file_path = os.path.join(folder, f"{activity_id}.{file_type}") + need_unzip = False + if file_type == "fit": + file_path = os.path.join(folder, f"{activity_id}.zip") + need_unzip = True async with aiofiles.open(file_path, "wb") as fb: await fb.write(file_data) + if need_unzip: + zip_file = zipfile.ZipFile(file_path, "r") + for file_info in zip_file.infolist(): + zip_file.extract(file_info, folder) + os.rename( + os.path.join(folder, f"{activity_id}_ACTIVITY.fit"), + os.path.join(folder, f"{activity_id}.fit"), + ) + os.remove(file_path) except: print(f"Failed to download activity {activity_id}: ") traceback.print_exc() @@ -379,6 +397,14 @@ async def download_new_activities( default="gpx", help="to download personal documents or ebook", ) + parser.add_argument( + "--fit", + dest="download_file_type", + action="store_const", + const="fit", + default="gpx", + help="to download personal documents or ebook", + ) options = parser.parse_args() email = options.email or config("sync", "garmin", "email") password = options.password or config("sync", "garmin", "password")