-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
51 lines (41 loc) · 1.21 KB
/
main.ts
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
import * as fs from "fs";
import * as nike from "./api/nike";
import { nikeActivityToGpx, gpxActivityToXml, msToISOString } from "./helpers";
const LAST_SYNC_PATH = `${__dirname}/last_sync.txt`;
const NIKE_BEARER = process.argv[2];
function getLastSyncMs(): number {
if (fs.existsSync(LAST_SYNC_PATH)) {
const lastSyncString = fs.readFileSync(LAST_SYNC_PATH, {
encoding: "utf-8",
});
return Number.parseInt(lastSyncString) || 0;
} else {
return 0;
}
}
async function main() {
const activitiesIds = await nike.getRunningActivitiesIdsAfterTime(
NIKE_BEARER,
getLastSyncMs()
);
console.log(`got ${activitiesIds.length} ids`);
const activities = await Promise.all(
activitiesIds.map((id) => nike.getActivityById(NIKE_BEARER, id))
);
console.log("all fetched");
for (const activity of activities) {
const gpx = nikeActivityToGpx(activity);
const xml = gpxActivityToXml(gpx);
const activityDate = msToISOString(activity.start_epoch_ms).replace(
/:/g,
";"
);
fs.writeFileSync(
`${__dirname}/activities/${activityDate}_${activity.id}.gpx`,
xml
);
}
const now = Date.now();
fs.writeFileSync(LAST_SYNC_PATH, now.toString());
}
main();