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
import { createTidbytWorkoutsImage } from "https://esm.town/v/andreterron/createTidbytWorkoutsImage";
import { setTidbytImage } from "https://esm.town/v/andreterron/setTidbytImage";
import { weekWorkoutIcons } from "https://esm.town/v/andreterron/weekWorkoutIcons";
import { workedOutByDay } from "https://esm.town/v/andreterron/workedOutByDay";
import { blob } from "https://esm.town/v/std/blob?v=11";
export let updateTidbytWorkout = async (force: boolean = false) => {
const timezone = "America/Los_Angeles";
// byDay is a Record<"YYYY-MM-DD", truthy | falsy> that
// stores which days you worked out. Recommended to get
// the current and previous week of data.
// e.g.: {"2023-11-15": true, "2023-11-13": true}
const byDay = await workedOutByDay(timezone);
// icons is an Array with one icon type for each of the 7 days
// e.g.: ["done", "skipped", "done", "done", "today", "future", "future"]
const icons = weekWorkoutIcons(byDay, timezone);
// Don't update the image if it didn't change
let iconsCache: string[] | undefined;
// try {
// iconsCache = await blob.getJSON("tidbytWorkoutCache");
// } catch (e) {
// console.error(e);
// }
await blob.setJSON("tidbytWorkoutCache", icons);
if (!force && iconsCache && iconsCache.every((v, i) => v === icons[i])) {
console.log("No changes detected, skipping updating tidbyt app");
return;
}
// img is the resulting jimp image
const img = await createTidbytWorkoutsImage(icons);
// Send the image to Tidbyt
await setTidbytImage({
image: (await img.getBufferAsync(img.getMIME())).toString("base64"),
});
};