Skip to content

Commit

Permalink
Fix codoon and Upload tcx_to_garmin (yihong0618#662)
Browse files Browse the repository at this point in the history
* add oppo_sync and tcx_to_garmin.py

also modify some format problem when uploading the tcx file export from
codoon.

* 修改readme.md

* 修复CI遇到的问题,已经根据评审修改相关的代码

* 解决运行时参数少了的异常

* Optimise the lap details in tcx file.

* type: 修改漏掉的文件

scope:

subject:

* 完善代码逻辑

* 修改代码漏洞

* 修改oppo的请求接口地址

* 解决编译问题

* 解决咕咚tcx生成异常问题,同时解决当前存在的CI问题

* 使用black完成代码format
  • Loading branch information
Vensent authored Apr 24, 2024
1 parent bb7aed4 commit c91c325
Show file tree
Hide file tree
Showing 8 changed files with 921 additions and 11 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/run_data_sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ on:
- run_page/keep_sync.py
- run_page/gpx_sync.py
- run_page/tcx_sync.py
- run_page/tcx_to_garmin_sync.py
- run_page/garmin_to_strava_sync.py
- run_page/keep_to_strava_sync.py
- run_page/oppo_sync.py
- requirements.txt

env:
# please change to your own config.
RUN_TYPE: pass # support strava/nike/garmin/coros/garmin_cn/garmin_sync_cn_global/keep/only_gpx/only_fit/nike_to_strava/strava_to_garmin/strava_to_garmin_cn/garmin_to_strava/garmin_to_strava_cn/codoon, Please change the 'pass' it to your own
RUN_TYPE: pass # support strava/nike/garmin/coros/garmin_cn/garmin_sync_cn_global/keep/only_gpx/only_fit/nike_to_strava/strava_to_garmin/tcx_to_garmin/strava_to_garmin_cn/garmin_to_strava/garmin_to_strava_cn/codoon/oppo, Please change the 'pass' it to your own
ATHLETE: yihong0618
TITLE: Yihong0618 Running
MIN_GRID_DISTANCE: 10 # change min distance here
Expand Down Expand Up @@ -121,6 +123,12 @@ jobs:
run: |
python run_page/codoon_sync.py ${{ secrets.CODOON_MOBILE }} ${{ secrets.CODOON_PASSWORD }}
- name: Run sync tcx to Garmin script
if: env.RUN_TYPE == 'tcx_to_garmin'
run: |
# python run_page/tcx_to_garmin_sync.py ${{ secrets.GARMIN_SECRET_STRING }}
python run_page/tcx_to_garmin_sync.py ${{ secrets.GARMIN_SECRET_STRING_CN }} --is-cn
# for garmin if you want generate `tcx` you can add --tcx command in the args.
- name: Run sync Garmin script
if: env.RUN_TYPE == 'garmin'
Expand Down Expand Up @@ -187,6 +195,13 @@ jobs:
run: |
python run_page/tulipsport_sync.py ${{ secrets.TULIPSPORT_TOKEN }} --with-gpx
- name: Run sync Oppo heytap script, note currently this script is not worked
if: env.RUN_TYPE == 'oppo'
run: |
python run_page/oppo_sync.py ${{ secrets.OPPO_ID }} ${{ secrets.OPPO_CLIENT_SECRET }} ${{ secrets.OPPO_CLIENT_REFRESH_TOKEN }} --with-tcx
# If you want to sync fit activity in gpx format, please consider the following script:
# python run_page/oppo_sync.py ${{ secrets.OPPO_ID }} ${{ secrets.OPPO_CLIENT_SECRET }} ${{ secrets.OPPO_CLIENT_REFRESH_TOKEN }} --with-gpx

- name: Make svg GitHub profile
if: env.RUN_TYPE != 'pass'
run: |
Expand Down
26 changes: 26 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ R.I.P. 希望大家都能健康顺利的跑过终点,逝者安息。
- **[FIT](#fit)**
- **[佳明国内同步国际](#Garmin-CN-to-Garmin)**
- **[Tcx+Strava(upload all tcx data to strava)](#tcx_to_strava)**
- **[Tcx+Garmin(upload all tcx data to Garmin)](#tcx_to_garmin)**
- **[Gpx+Strava(upload all tcx data to strava)](#gpx_to_strava)**
- **[Nike+Strava(Using NRC Run, Strava backup data)](#nikestrava)**
- **[Garmin_to_Strava(Using Garmin Run, Strava backup data)](#garmin_to_strava)**
Expand Down Expand Up @@ -779,6 +780,31 @@ python3(python) run_page/tcx_to_strava_sync.py xxx xxx xxx --all

> 如果你已经上传过需要跳过判断增加参数 `--all`
### TCX_to_Garmin

<details>
<summary>上传所有的 tcx 格式的跑步数据到 Garmin</summary>

<br>

1. 完成 garmin 的步骤
2. 把 tcx 文件全部拷贝到 TCX_OUT 中
3. 在项目根目录下执行:

```bash
python3 run_page/tcx_to_garmin_sync.py ${{ secrets.GARMIN_SECRET_STRING_CN }} --is-cn
```

示例:

```bash
python run_page/tcx_to_garmin_sync.py xxx --is-cn
或佳明国际
python run_page/tcx_to_garmin_sync.py xxx
```

> 如果你已经上传过需要跳过判断增加参数 `--all`
</details>

### GPX_to_Strava
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ English | [简体中文](https://github.com/yihong0618/running_page/blob/master/
- **[Garmin-CN_to_Garmin(Sync Garmin-CN activities to Garmin Global)](#garmin-cn-to-garmin)**
- **[Nike_to_Strava(Using NRC Run, Strava backup data)](#nike_to_strava)**
- **[Tcx_to_Strava(upload all tcx data to strava)](#tcx_to_strava)**
- **[Tcx_to_Garmin(upload all tcx data to Garmin)](#tcx_to_garmin)**
- **[Gpx_to_Strava(upload all gpx data to strava)](#gpx_to_strava)**
- **[Garmin_to_Strava(Using Garmin Run, Strava backup data)](#garmin_to_strava)**
- **[Strava_to_Garmin(Using Strava Run, Garmin backup data)](#strava_to_garmin)**
Expand Down Expand Up @@ -587,6 +588,33 @@ python3(python) run_page/tcx_to_strava_sync.py xxx xxx xxx --all

</details>

### TCX_to_Garmin

<details>
<summary>upload all tcx files to garmin</summary>

<br>

1. follow the garmin steps
2. copy all your tcx files to TCX_OUT
3. Execute in the root directory:

```bash
python3 run_page/tcx_to_garmin_sync.py ${{ secrets.GARMIN_SECRET_STRING_CN }} --is-cn
```

example:

```bash
python run_page/tcx_to_garmin_sync.py xxx --is-cn
or Garmin Global
python run_page/tcx_to_garmin_sync.py xxx
```

4. if you want to all files add args `--all`

</details>

### GPX_to_Strava

<details>
Expand Down
38 changes: 29 additions & 9 deletions run_page/codoon_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import xml.etree.ElementTree as ET
from collections import namedtuple
from datetime import datetime, timedelta
from xml.dom import minidom

import eviltransform
import gpxpy
Expand Down Expand Up @@ -45,6 +46,8 @@
# device info
user_agent = "CodoonSport(8.9.0 1170;Android 7;Sony XZ1)"
did = "24-00000000-03e1-7dd7-0033-c5870033c588"
# May be Forerunner 945?
CONNECT_API_PART_NUMBER = "006-D2449-00"

# fixed params
base_url = "https://api.codoon.com"
Expand All @@ -61,9 +64,9 @@

# for tcx type
TCX_TYPE_DICT = {
0: "Hike",
0: "Hiking",
1: "Running",
2: "Ride",
2: "Biking",
}

# only for running sports, if you want others, please change the True to False
Expand Down Expand Up @@ -127,6 +130,9 @@ def formated_input(


def tcx_output(fit_array, run_data):
"""
If you want to make a more detailed tcx file, please refer to oppo_sync.py
"""
# route ID
fit_id = str(run_data["id"])
# local time
Expand All @@ -149,7 +155,7 @@ def tcx_output(fit_array, run_data):
},
)
# xml tree
tree = ET.ElementTree(training_center_database)
ET.ElementTree(training_center_database)
# Activities
activities = ET.Element("Activities")
training_center_database.append(activities)
Expand All @@ -163,12 +169,15 @@ def tcx_output(fit_array, run_data):
activity_id.text = fit_start_time # Codoon use start_time as ID
activity.append(activity_id)
# Creator
activity_creator = ET.Element("Creator")
activity_creator = ET.Element("Creator", {"xsi:type": "Device_t"})
activity.append(activity_creator)
# Name
activity_creator_name = ET.Element("Name")
activity_creator_name.text = "咕咚"
activity_creator_name.text = "Codoon"
activity_creator.append(activity_creator_name)
activity_creator_product = ET.Element("ProductID")
activity_creator_product.text = "3441"
activity_creator.append(activity_creator_product)
# Lap
activity_lap = ET.Element("Lap", {"StartTime": fit_start_time})
activity.append(activity_lap)
Expand Down Expand Up @@ -215,11 +224,22 @@ def tcx_output(fit_array, run_data):
altitude_meters = ET.Element("AltitudeMeters")
altitude_meters.text = bytes.decode(i["elevation"])
tp.append(altitude_meters)

# Author
author = ET.Element("Author", {"xsi:type": "Application_t"})
training_center_database.append(author)
author_name = ET.Element("Name")
author_name.text = "Connect Api"
author.append(author_name)
author_lang = ET.Element("LangID")
author_lang.text = "en"
author.append(author_lang)
author_part = ET.Element("PartNumber")
author_part.text = CONNECT_API_PART_NUMBER
author.append(author_part)
# write to TCX file
tree.write(
TCX_FOLDER + "/" + fit_id + ".tcx", encoding="utf-8", xml_declaration=True
)
xml_str = minidom.parseString(ET.tostring(training_center_database)).toprettyxml()
with open(TCX_FOLDER + "/" + fit_id + ".tcx", "w") as f:
f.write(str(xml_str))


# TODO time complexity is too heigh, need to be reduced
Expand Down
2 changes: 1 addition & 1 deletion run_page/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


BASE_TIMEZONE = "Asia/Shanghai"

UTC_TIMEZONE = "UTC"

start_point = namedtuple("start_point", "lat lon")
run_map = namedtuple("polyline", "summary_polyline")
Expand Down
13 changes: 13 additions & 0 deletions run_page/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,16 @@ def get_old_tracks_ids(self):
# pass the error
print(f"something wrong with {str(e)}")
return []

def get_old_tracks_dates(self):
try:
activities = (
self.session.query(Activity)
.order_by(Activity.start_date_local.desc())
.all()
)
return [str(a.start_date_local) for a in activities]
except Exception as e:
# pass the error
print(f"something wrong with {str(e)}")
return []
Loading

0 comments on commit c91c325

Please sign in to comment.