forked from glennreyes/runtastic-gpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (254 loc) · 8.11 KB
/
index.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const chalk = require('chalk');
const fs = require('fs');
const ora = require('ora');
const util = require('util');
const FILES_PER_SEGMENT = 25;
const SPORT_TYPES = {
basketball: '45', // Unsupported
crossfit: '69',
elliptical: '16',
indoor_ride: '15',
indoor_run: '14',
ride: '3',
roadbike: '22',
run: '1',
walk: '19',
weight_training: '34',
};
const parse = buffer => {
const str = buffer.toString();
try {
const content = JSON.parse(str);
return content;
} catch (error) {
throw error;
}
};
const filterContent = content => {
return content.filter(item =>
Object.values(SPORT_TYPES).includes(item.sport_type_id),
);
};
const matchGpsWithHeartRate = hr => gps =>
hr.timestamp === gps.timestamp ||
hr.duration === gps.duration ||
hr.distance === gps.distance;
const nearestDate = (dates, target) => {
const targetDate = new Date(target);
let nearestDistance = Infinity;
let nearestDate = null;
dates.forEach(date => {
const distance = Math.abs(new Date(date) - targetDate);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestDate = date;
}
});
return nearestDate;
};
const createFolder = path =>
util
.promisify(fs.mkdir)(path, {
recursive: true,
})
.catch(console.error);
const template = (data, name) => {
const hourStarted = new Date(data.start_time).getHours();
const dayTime =
hourStarted >= 0 && hourStarted < 4
? 'Night'
: hourStarted < 11
? 'Morning'
: hourStarted < 4
? 'Lunch'
: hourStarted < 18
? 'Afternoon'
: hourStarted < 22
? 'Evening'
: 'Night';
const type = name === 'Run' ? 9 : name === 'Ride' ? 1 : '';
return `
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>${new Date(data.created_at).toISOString()}</time>
</metadata>
<trk>
<name>${dayTime} ${name}</name>${
type
? `
<type>${type}</type>
`.trim()
: ''
}
<trkseg>
${
Array.isArray(data.gps) && data.gps.length > 0
? `
${data.gps
.map(gpsItem => {
const heartRate =
data.heart_rate &&
Array.isArray(data.heart_rate) &&
data.heart_rate.some(matchGpsWithHeartRate(gpsItem))
? data.heart_rate.find(matchGpsWithHeartRate(gpsItem)).heart_rate
: ''
? data.heart_rate.find(
hr =>
new Date(hr.timestamp).getTime() ===
nearestDate(
data.heart_rate.map(h => h.timestamp),
gpsItem.timestamp,
),
).heart_rate
: null;
return `
<trkpt lon="${gpsItem.longitude}" lat="${gpsItem.latitude}">
<ele>${gpsItem.altitude}</ele>
<time>${new Date(gpsItem.timestamp).toISOString()}</time>${
heartRate
? `
<extensions>
<gpxtpx:TrackPointExtension>
<gpxtpx:hr>${heartRate}</gpxtpx:hr>
</gpxtpx:TrackPointExtension>
</extensions>
`.trim()
: ''
}
</trkpt>`;
})
.join('')
.trim()}`.trim()
: data.heart_rate &&
Array.isArray(data.heart_rate) &&
data.heart_rate.length > 0
? data.heart_rate
.map(
heartRate => `
<trkpt lon="0" lat="0">
<time>${new Date(heartRate.timestamp).toISOString()}</time>
<extensions>
<gpxtpx:TrackPointExtension>
<gpxtpx:hr>${heartRate.heart_rate}</gpxtpx:hr>
</gpxtpx:TrackPointExtension>
</extensions>
</trkpt>`,
)
.join('')
.trim()
: ''
}</trkseg>
</trk>
</gpx>
`.trim();
};
let amountExportedFiles = 0;
const start = async ([exportPath, outputPath = `${process.cwd()}/export`]) => {
const exportSession = async (data, type) => {
const name = type
.replace('indoor_', '')
.split('_')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
const prefix = chalk.gray(
`${type.startsWith('indoor') ? 'Indoor ' : ''}${name} `,
);
const dataSpinner = ora(`${prefix}Load GPS and heart rate data`).start();
const sessions = await Promise.all(
data
.filter(item => SPORT_TYPES[type] === item.sport_type_id)
.map(async item => {
const gpsPath = `${exportPath}/Sport-sessions/GPS-data/${item.id}.json`;
const heartRatePath = `${exportPath}/Sport-sessions/Heart-rate-data/${item.id}.json`;
let gpsFile = null;
let heartRateFile = null;
if (fs.existsSync(gpsPath)) {
gpsFile = await util
.promisify(fs.readFile)(gpsPath)
.catch(console.error);
}
if (fs.existsSync(heartRatePath)) {
heartRateFile = await util
.promisify(fs.readFile)(heartRatePath)
.catch(console.error);
}
return {
...item,
gps: gpsFile ? parse(gpsFile) : [],
heart_rate: heartRateFile ? parse(heartRateFile) : [],
};
}),
);
dataSpinner.succeed();
const exportSpinner = ora(`${prefix}Export GPX files`).start();
let amountExportedSegmentFiles = 0;
await Promise.all(
sessions
.filter(
session =>
!(
((type === 'ride' || type === 'run') &&
session.gps.length === 0) ||
((type === 'indoor_ride' || type === 'indoor_run') &&
session.heart_rate.length === 0)
),
)
.map(async session => {
amountExportedFiles++;
amountExportedSegmentFiles++;
const segment =
Math.round(amountExportedFiles / FILES_PER_SEGMENT) + 1;
const segmentPath = `${outputPath}/${type}/${segment}`;
if (!fs.existsSync(segmentPath)) {
await createFolder(segmentPath);
}
return util
.promisify(fs.writeFile)(
`${segmentPath}/${session.id}.gpx`,
template(session, name),
)
.catch(console.error);
}),
);
exportSpinner.succeed();
};
const exportAllSessions = async data => {
await exportSession(data, 'ride');
await exportSession(data, 'run');
await exportSession(data, 'roadbike');
await exportSession(data, 'indoor_ride');
await exportSession(data, 'indoor_run');
await exportSession(data, 'elliptical');
await exportSession(data, 'weight_training');
await exportSession(data, 'crossfit');
await exportSession(data, 'walk');
console.log(
`\n${chalk.green(
amountExportedFiles,
)} activities successfully exported to ${chalk.yellow(outputPath)}. 💃`,
);
};
const filesSpinner = ora(`Load all activities`).start();
const files = await util
.promisify(fs.readdir)(`${exportPath}/Sport-sessions`)
.catch(console.error);
const content = await Promise.all(
files
.filter(file => file.endsWith('.json'))
.map(async file => {
const data = await util
.promisify(fs.readFile)(`${exportPath}/Sport-sessions/${file}`)
.catch(console.error);
return data ? parse(data) : null;
}),
);
const compact = content.filter(Boolean);
const data = filterContent(compact);
filesSpinner.succeed();
await exportAllSessions(data);
};
module.exports = start;