forked from sam-dumont/cowboybike-strava-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcx.py
156 lines (138 loc) · 6.11 KB
/
tcx.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import lxml.etree
from dateutil import parser
from datetime import timedelta, timezone
import os
import sys, getopt
import json
WATTS_FILTER = int(os.getenv("WATTS_FILTER", 1100))
def add_trackpoint(source, start_time, seconds, latlng, distance, power):
trackpoint = lxml.etree.SubElement(source, "Trackpoint")
lxml.etree.SubElement(trackpoint, "Time").text = str(
(start_time + timedelta(seconds=seconds)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
)
if latlng is not None:
position = lxml.etree.SubElement(trackpoint, "Position")
lxml.etree.SubElement(position, "LatitudeDegrees").text = str(latlng[0])
lxml.etree.SubElement(position, "LongitudeDegrees").text = str(latlng[1])
lxml.etree.SubElement(trackpoint, "DistanceMeters").text = str(distance)
extensions = lxml.etree.SubElement(trackpoint, "Extensions")
tpx = lxml.etree.SubElement(
extensions,
"{http://www.garmin.com/xmlschemas/ActivityExtension/v2}TPX",
)
lxml.etree.SubElement(tpx, "{http://www.garmin.com/xmlschemas/ActivityExtension/v2}Watts",).text = (
str(power) if (power is not None and power < WATTS_FILTER) else "0"
)
def create_tcx(activity_source, charts_source):
start_time = parser.parse(activity_source["started_at"]).astimezone(timezone.utc)
attr_qname = lxml.etree.QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation")
nsmap = {
None: "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2",
"up2": "http://www.garmin.com/xmlschemas/UserProfile/v2",
"ns3": "http://www.garmin.com/xmlschemas/ActivityExtension/v2",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
}
doc = lxml.etree.Element(
"TrainingCenterDatabase",
{
attr_qname: "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 https://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd http://www.garmin.com/xmlschemas/UserProfile/v2 https://www8.garmin.com/xmlschemas/UserProfileExtensionv2.xsd http://www.garmin.com/xmlschemas/ActivityExtension/v2 https://www8.garmin.com/xmlschemas/ActivityExtensionv2.xsd"
},
nsmap=nsmap,
)
attr_qname = lxml.etree.QName("http://www.w3.org/2001/XMLSchema-instance", "type")
author = lxml.etree.SubElement(
doc,
"Author",
{attr_qname: "Application_t"},
)
lxml.etree.SubElement(author, "Name").text = "Cowboy for Strava"
lxml.etree.SubElement(author, "LangID").text = "en"
lxml.etree.SubElement(author, "PartNumber").text = "123-4567"
build = lxml.etree.SubElement(author, "Build")
version = lxml.etree.SubElement(build, "Version")
lxml.etree.SubElement(version, "VersionMajor").text = "1"
lxml.etree.SubElement(version, "VersionMinor").text = "0"
lxml.etree.SubElement(version, "BuildMajor").text = "0"
lxml.etree.SubElement(version, "BuildMinor").text = "0"
activities = lxml.etree.SubElement(doc, "Activities")
activity = lxml.etree.SubElement(activities, "Activity", Sport="Biking")
lxml.etree.SubElement(activity, "Id").text = start_time.strftime("%Y-%m-%dT%H:%M:%S.000Z")
lxml.etree.SubElement(activity, "Notes").text = activity_source["title"]
creator = lxml.etree.SubElement(activity, "Creator", {attr_qname: "Device_t"})
lxml.etree.SubElement(creator, "Name").text = "Cowboy for Strava"
lxml.etree.SubElement(creator, "UnitId").text = "0"
lxml.etree.SubElement(creator, "ProductID").text = "0"
versionc = lxml.etree.SubElement(creator, "Version")
lxml.etree.SubElement(versionc, "VersionMajor").text = "1"
lxml.etree.SubElement(versionc, "VersionMinor").text = "0"
lxml.etree.SubElement(versionc, "BuildMajor").text = "0"
lxml.etree.SubElement(versionc, "BuildMinor").text = "0"
lap = lxml.etree.SubElement(activity, "Lap", StartTime=start_time.strftime("%Y-%m-%dT%H:%M:%S.000Z"))
lxml.etree.SubElement(lap, "TriggerMethod").text = "Manual"
lxml.etree.SubElement(lap, "TotalTimeSeconds").text = str(activity_source["unlocked_time"])
lxml.etree.SubElement(lap, "DistanceMeters").text = str(round(activity_source["distance"] * 1000, 3))
track = lxml.etree.SubElement(lap, "Track")
add_trackpoint(
track,
start_time,
0,
[
charts_source["positions"][0],
charts_source["distances"][0],
],
0,
0,
)
for index, point in enumerate(charts_source["durations"]):
if charts_source["distances"][index] is not None:
add_trackpoint(
track,
start_time,
point,
charts_source["positions"][index],
charts_source["distances"][index],
charts_source["charts"]["user_power"]["data"][index],
)
add_trackpoint(
track,
start_time,
0,
[
charts_source["positions"][-1],
charts_source["distances"][-1],
],
len(charts_source["positions"]) + 1,
0,
)
return lxml.etree.ElementTree(doc)
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print('test.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
#print( 'Input file is "', inputfile)
#print( 'Output file is "', outputfile)
txt = outputfile[:len(outputfile) - len(outputfile.split("/")[4])]
f = open(outputfile, "r")
tcx = create_tcx(json.loads(inputfile),json.loads(f.read()))
tcx.write(
f"{txt}output_{json.loads(inputfile)['id']}.tcx",
pretty_print=True,
xml_declaration=True,
encoding="utf-8",
)
print(f"{txt}output_{json.loads(inputfile)['id']}.tcx")
os.remove(outputfile)
if __name__ == "__main__":
main(sys.argv[1:])