Avatar

@kognise

10 public vals
Joined August 10, 2023
1
2
3
export function googleSheetsDate(date: Date): number {
return date.getTime() / 1000 / 60 / 60 / 24 + 25569
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { mail } from "https://esm.town/v/kognise/mail";
import { addValenceDatapoint } from "https://esm.town/v/kognise/addValenceDatapoint";
export async function onValenceReply(email) {
const firstNumber = parseInt(email.text.trim(), 10)
try {
await addValenceDatapoint(firstNumber)
await mail({
to: "Lexi Mattick <lexi.mattick@gmail.com>",
from: "Valence Tracker <kognise.onValenceReply@valtown.email>",
subject: `Re: ${email.subject}`,
text: 'received and stored! thanks.'
})
} catch (error) {
await mail({
to: "Lexi Mattick <lexi.mattick@gmail.com>",
from: "Valence Tracker <kognise.onValenceReply@valtown.email>",
subject: `Re: ${email.subject}`,
text: 'something broke!\n\n' + error.toString()
})
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { mail } from "https://esm.town/v/kognise/mail";
export async function sendDailyValenceEmail() {
await mail({
to: "Lexi Mattick <lexi.mattick@gmail.com>",
from: "Valence Tracker <kognise.onValenceReply@valtown.email>",
subject: "daily happiness check-in",
text: `
hello, me! this is your daily reminder to rate your happiness. you can do this any time today.
1 - terrible
2 - pretty bad
3 - meh
4 - pretty good
5 - fantastic
as always, just reply with a relevant number.
`.trim(),
});
}
1
2
3
4
5
6
7
8
export const stopIds = {
// Toward Shelburne:
fayette: "GMTVT:14599",
tracyHouse: "GMTVT:14613",
// Toward Burlington:
shoppingPark: "GMTVT:14625",
macintosh: "GMTVT:14642", // Shelburne Road / Macintosh Drive
};
1
export const transitCacheExpiry = 60000;
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
import { stopDeparturesSingleCountdown } from "https://esm.town/v/kognise/stopDeparturesSingleCountdown";
import { stopIds } from "https://esm.town/v/kognise/stopIds";
export async function workBusesGui(
req: express.Request,
res: express.Response,
) {
const currentKey = req.path.slice(1);
let departures;
if (stopIds[currentKey]) {
departures = await stopDeparturesSingleCountdown(
stopIds[currentKey],
);
}
return res.send(`
<!DOCTYPE html>
<html>
<head>
<title>bus departures</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/light.css">
</head>
<body>
<nav>
${
Object.keys(stopIds).map((key) => `
${
currentKey === key
? `<strong>${key}</strong>`
: `<a href="/${key}">${key}</a>`
}
`).join(" &middot; ")
}
</nav>
${
departures
? `
<h1>bus departures from ${currentKey}</h1>
<ul>
${
departures.map((departure) => `
<li>
${(departure.countdown / 1000 / 60).toFixed(0)} min
${departure.is_real_time ? "(live)" : "(scheduled)"}
</li>
`).join("")
}
</ul>
`
: `
Select a location!
1
2
3
4
5
6
7
8
9
import { stopDepartures } from "https://esm.town/v/kognise/stopDepartures";
export async function stopDeparturesSingleCountdown(globalStopId: string) {
const res = await stopDepartures(globalStopId);
return res[0].itineraries[0].schedule_items.map((bus) => ({
...bus,
countdown: bus.departure_time * 1000 - Date.now(),
}));
}
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 process from "node:process";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import { transitCacheExpiry } from "https://esm.town/v/kognise/transitCacheExpiry";
import { stopDeparturesCache } from "https://esm.town/v/kognise/stopDeparturesCache";
export async function stopDepartures(globalStopId: string) {
const cached = stopDeparturesCache[globalStopId];
if (cached && Date.now() - cached.time < transitCacheExpiry) {
return cached.data;
}
const url =
`https://external.transitapp.com/v3/public/stop_departures?global_stop_id=${
encodeURIComponent(globalStopId)
}`;
const json = await fetchJSON(url, {
headers: {
apiKey: process.env.transit,
},
});
const data = json.route_departures;
stopDeparturesCache[globalStopId] = {
time: Date.now(),
data,
};
await set("stopDeparturesCache", stopDeparturesCache)
return data;
}
1
2
3
import { exportedKeys } from "https://esm.town/v/kognise/exportedKeys";
export const publicKey = () => exportedKeys.publicKey;
1
export const jsonOkExample = () => Response.json({ ok: true });
Next