Skip to content

Commit

Permalink
feat:support for download Garmin fit file (yihong0618#446)
Browse files Browse the repository at this point in the history
* feat:support for download Garmin fit file
  • Loading branch information
zhangyulai authored and agassiyzh committed Jul 7, 2023
1 parent e4cbc59 commit d730cb5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
Empty file added FIT_OUT/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ python3(python) scripts/codoon_sync.py 54bxxxxxxx fefxxxxx-xxxx-xxxx --from-auth
<details>
<summary>Get your <code>Xingzhe</code> data</summary>

If you only want `fit` files add args --fit

```python
python3(python) scripts/xingzhe_sync.py ${your mobile or email} ${your password}
```
Expand Down
2 changes: 2 additions & 0 deletions scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 26 additions & 0 deletions scripts/garmin_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import time
import traceback
import zipfile

import aiofiles
import cloudscraper
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d730cb5

Please sign in to comment.