garmsync garmsync
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

78 lines
2.3 KiB

import gc from 'garmin-connect';
import Trimlog from './lib/client.mjs';
import { generateTrimlogInput, today } from './lib/tcx.mjs';
async function main() {
const garmin = new gc.GarminConnect({
username: process.env.GARMIN_USERNAME,
password: process.env.GARMIN_PASSWORD,
});
const trimlog = new Trimlog({
username: process.env.TRIMLOG_USERNAME,
password: process.env.TRIMLOG_PASSWORD,
})
await trimlog.refreshToken();
await garmin.login();
const blackList = {};
setInterval(async() => {
const looseActivities = await trimlog.getLooseActivities();
const recentWorkouts = await trimlog.getWorkouts();
const activities = await garmin.getActivities();
const todayDate = today();
for (const act of activities) {
const id = Math.round(act.activityId).toFixed(0);
if (blackList[id]) {
continue;
}
if (!["walking", "cycling"].includes(act.activityType?.typeKey)) {
console.error("Skipping", act.activityId, `(${act.activityType?.typeKey} activity not supported)`);
blackList[id] = true;
continue;
}
if (!act.startTimeLocal.startsWith(todayDate)) {
console.error("Skipping", act.activityId, "(not today)");
blackList[id] = true;
continue;
}
if (looseActivities.find(l => l.tags.find(t => t.key === "gc:ActivityID" && t.value === id))) {
console.error("Skipping", act.activityId, "(loose activity exists)");
blackList[id] = true;
continue;
}
if (recentWorkouts.find(l => l.tags.find(t => t.key === "gc:ActivityID" && t.value === id))) {
console.error("Skipping", act.activityId, "(workout exists)");
blackList[id] = true;
continue;
}
console.log("Going ahead with", act.activityId, act.activityType.typeKey)
await garmin.downloadOriginalActivityData(act, "/tmp/", "tcx")
const input = await generateTrimlogInput(`/tmp/${act.activityId}.tcx`, act.activityName)
input.tags.push({key: "gc:ActivityID", value: id})
await trimlog.postWorkout(input);
blackList[id] = true;
}
}, 300000);
process.on('SIGINT', function() {
console.log("INTERRUPTED");
process.exit(0);
});
process.on('SIGTERM', function() {
console.log("TERMINATED");
process.exit(0);
});
}
main();