Avatar

crsven

14 public vals
Joined February 2, 2023
1
2
3
4
5
6
7
8
import { poemWidgetJson } from "https://esm.town/v/crsven/poemWidgetJson";
export let renderTimeTestWidgetJson = async (req: Request, res: Response) => {
const newState = { ...poemWidgetJson };
newState.layouts.hello_small.layers[0].rows[0].cells[0].text.string =
"The time is now #current_time";
res.json(newState);
};
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
import { set } from "https://esm.town/v/std/set?v=11";
import { poemWidgetJson } from "https://esm.town/v/crsven/poemWidgetJson";
import { getNextColorCombo } from "https://esm.town/v/crsven/getNextColorCombo";
import { getPoemForTime } from "https://esm.town/v/crsven/getPoemForTime";
export let refreshPoemWidget = async (timeZone?: Interval) => {
const defaultTimeZone = "America/Los_Angeles";
const date = new Date();
const baseTimeParams = {
hour: "numeric",
minute: "numeric",
timeZone: timeZone || defaultTimeZone,
};
const timeForApi = date.toLocaleTimeString("en-GB", {
...baseTimeParams,
hourCycle: "h23",
});
const poem = await getPoemForTime(timeForApi);
const timeForMessage = date.toLocaleTimeString("en-US", baseTimeParams);
const message = `${timeForMessage}: ${poem}`;
const colorCombo = await getNextColorCombo();
const newState = { ...poemWidgetJson };
newState.layouts.hello_small.styles.colors.background.color =
colorCombo.background;
newState.layouts.hello_small.styles.colors.text.color = colorCombo.text;
newState.layouts.hello_small.layers[1].rows[0].cells[0].text.string = message;
await set("poemWidgetJson", newState);
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { set } from "https://esm.town/v/std/set?v=11";
import { poemWidgetJson } from "https://esm.town/v/crsven/poemWidgetJson";
import { getPoemForTime } from "https://esm.town/v/crsven/getPoemForTime";
export let renderPoemWidgetJson = async (req: Request, res: Response) => {
const timeZone = req.query.timezone || "America/Los_Angeles";
const date = new Date();
const baseTimeParams = {
hour: "numeric",
minute: "numeric",
timeZone,
};
const timeForApi = date.toLocaleTimeString("en-GB", {
...baseTimeParams,
hourCycle: "h23",
});
const poem = await getPoemForTime(timeForApi);
const newState = { ...poemWidgetJson };
newState.layouts.hello_small.layers[0].rows[0].cells[0].text.string = poem;
await set("poemWidgetJson", poemWidgetJson);
res.json(newState);
};
1
2
3
4
5
6
7
8
9
import { fetch } from "https://esm.town/v/std/fetch";
export let getPoemForTime = async (time: string) => {
const poemResponse = await fetch(
`https://poem.town/api/v1/clock/poem/${time}`
);
const poemJson = await poemResponse.json();
return poemJson.verse;
};
1
2
3
4
5
6
export let embedBirdSound = async (req, res) => {
const [title, file, image] = req.query.args.split(",");
res.send(
`<div style="text-align: center;"><h3 style="font-size: 5rem; margin: 5rem auto;">${title}</h3><img style="display: block; width: 50vw; margin: 0 auto;" src="${image}" /><audio controls src="${file}" style="width: 50vw; margin: 0 auto 5rem;"></audio></
);
};
1
2
3
4
5
import { animalWidgetJson } from "https://esm.town/v/crsven/animalWidgetJson";
export let renderAnimalWidgetJson = async (req: Request, res: Response) => {
res.json(animalWidgetJson);
};
1
2
3
4
5
6
7
8
9
let { animalWidgetJson } = await import("https://esm.town/v/crsven/animalWidgetJson");
import { getBirdPhotoUrl } from "https://esm.town/v/crsven/getBirdPhotoUrl";
export let refreshAnimalWidgetPicture = async (scientificName: string) => {
const photoUrl = await getBirdPhotoUrl(scientificName);
const newState = { ...animalWidgetJson };
newState.layouts.hello_small.layers[0].rows[0].cells[0].image.url = photoUrl;
animalWidgetJson = newState;
};
1
2
3
4
5
6
7
8
9
10
11
import { fetch } from "https://esm.town/v/std/fetch";
export let fetchABird = async () => {
const MAX_ID = 777905;
const baseUrl = "https://xeno-canto.org/api/2/recordings";
const minAnimalId = Math.floor(Math.random() * (MAX_ID + 10));
const query = `?query=nr:${minAnimalId}-${minAnimalId + 10}`;
const response = await fetch(`${baseUrl}${query}`);
const json = await response.json();
return json.recordings[Math.floor(Math.random() * 9)];
};
1
2
3
import { animalWidgetJson } from "https://esm.town/v/crsven/animalWidgetJson";
export let getAnimalWidgetJson = async () => animalWidgetJson;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { animalWidgetJson } from "https://esm.town/v/crsven/animalWidgetJson";
import { fetchABird } from "https://esm.town/v/crsven/fetchABird";
import { getBirdPhotoUrl } from "https://esm.town/v/crsven/getBirdPhotoUrl";
import { getNextColorCombo } from "https://esm.town/v/crsven/getNextColorCombo";
import { set } from "https://esm.town/v/std/set?v=11";
export let refreshAnimalWidget = async () => {
const bird = await fetchABird();
const photoUrl = await getBirdPhotoUrl(
`${bird.gen} ${bird.sp}`,
);
const message = `${bird.en} (${bird.cnt})`;
const colorCombo = await getNextColorCombo();
const newState = { ...animalWidgetJson };
const birdName = bird.en.replace(" ", "+");
newState.data.content_url =
`https://api.val.town/express/@crsven.embedBirdSound?args=${birdName},${bird.file},${photoUrl}`;
newState.layouts.hello_small.styles.colors.background.color = colorCombo.background;
newState.layouts.hello_small.styles.colors.text.color = colorCombo.text;
newState.layouts.hello_small.layers[2].rows[0].cells[0].text.string = message;
newState.layouts.hello_small.layers[0].rows[0].cells[0].image.url = photoUrl;
await set("animalWidgetJson", newState);
};