forked from kvnZero/IGPSPORT2Xingzhe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGarminSync.py
74 lines (62 loc) · 2.5 KB
/
GarminSync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import base64
import os.path
import requests
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from garth.http import Client
import zipfile
def encrpt(password, public_key):
rsa = RSA.importKey(public_key)
cipher = PKCS1_v1_5.new(rsa)
return base64.b64encode(cipher.encrypt(password.encode())).decode()
def syncData(garmin_email, garmin_password):
if garmin_password is None or garmin_password == '':
print("未设置账号密码,跳过脚本")
return
global_garth = Client()
garth = Client()
garth.configure(domain="garmin.cn")
try:
garth.login(garmin_email, garmin_password)
global_garth.login(garmin_email, garmin_password)
except Exception:
print("登录态失败")
return False
global_activities = global_garth.connectapi(
f"/activitylist-service/activities/search/activities",
params={"activityType": "running", "limit": 5, "start": 0, 'excludeChildren': False}, #这里是跑步数据 骑行数据可修改成cycling
)
activities = garth.connectapi(
f"/activitylist-service/activities/search/activities",
params={"activityType": "running", "limit": 5, "start": 0, 'excludeChildren': False},
)
add_list = []
has_exist = []
for item2 in global_activities:
has_exist.append(item2['startTimeGMT'])
for item in activities:
if item['startTimeGMT'] not in has_exist:
rid = item['activityId']
rid = str(rid)
add_list.append(rid)
if not os.path.isfile(rid+".zip"):
res = garth.download(
f"/download-service/files/activity/{rid}",
)
with open(rid+".zip", "wb") as f:
f.write(res)
with zipfile.ZipFile(rid + ".zip", 'r') as zip_ref:
zip_ref.extractall(rid)
else:
print("%s:已同步" % item['activityId'])
print(add_list)
print("本次同步数量:%s" % len(add_list))
for u in add_list:
with open(u + "/" + u + "_ACTIVITY.fit", 'rb') as fd:
uploaded = requests.post('https://connectapi.garmin.com/upload-service/upload',
files={'file': fd},
headers={'authorization': global_garth.oauth2_token.__str__()})
# uploaded = global_garth.upload(fd)
print(uploaded.content)
return True
activity = syncData(os.getenv("GARMIN_RUN_EMAIL"), os.getenv("GARMIN_RUN_PASSWORD"))