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.

70 lines
1.8 KiB

11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
11 months ago
11 months ago
3 months ago
11 months ago
  1. import gc from 'garmin-connect';
  2. import Trimlog from './lib/client.mjs';
  3. import fs from "fs/promises";
  4. import { today } from './lib/tcx.mjs';
  5. async function main() {
  6. const garmin = new gc.GarminConnect({
  7. username: process.env.GARMIN_USERNAME,
  8. password: process.env.GARMIN_PASSWORD,
  9. });
  10. const trimlog = new Trimlog({
  11. username: process.env.TRIMLOG_USERNAME,
  12. password: process.env.TRIMLOG_PASSWORD,
  13. })
  14. await trimlog.submit({});
  15. await garmin.login();
  16. console.log("CONNECTED!");
  17. const blackList = {};
  18. setInterval(async() => {
  19. const activities = await garmin.getActivities();
  20. const todayDate = today();
  21. for (const act of activities) {
  22. const id = Math.round(act.activityId).toFixed(0);
  23. if (blackList[id]) {
  24. continue;
  25. }
  26. if (!["walking", "cycling"].includes(act.activityType?.typeKey)) {
  27. console.error("Skipping", act.activityId, `(${act.activityType?.typeKey} activity not supported)`);
  28. blackList[id] = true;
  29. continue;
  30. }
  31. if (!act.startTimeLocal.startsWith(todayDate)) {
  32. console.error("Skipping", act.activityId, "(not today)");
  33. blackList[id] = true;
  34. continue;
  35. }
  36. console.log("Going ahead with", act.activityId, act.activityType.typeKey)
  37. await garmin.downloadOriginalActivityData(act, "/tmp/", "tcx")
  38. const input = (await fs.readFile(`/tmp/${act.activityId}.tcx`)).toString("base64")
  39. await trimlog.submit({addUpload: {
  40. name: `${act.activityId}.tcx`,
  41. base64: input,
  42. }});
  43. blackList[id] = true;
  44. }
  45. }, 300000);
  46. process.on('SIGINT', function() {
  47. console.log("INTERRUPTED");
  48. process.exit(0);
  49. });
  50. process.on('SIGTERM', function() {
  51. console.log("TERMINATED");
  52. process.exit(0);
  53. });
  54. }
  55. main();