Readme

Send a weekly email digest of good times to go for a bike ride.

Runs every 7 days
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Send an email of good times to go for a bike ride in the next week. Email looks like
*
* bike times 🚲
* Fri 06:00 AM to 08:00 AM
* Sat 06:00 AM to 02:00 PM
* Sun 06:00 AM to 11:00 AM
* Mon 06:00 AM to 06:00 PM
* Tue 06:00 AM to 02:00 PM
*
* Change the NOAA url constant to point to wherever you are.
*/
import { email } from "https://esm.town/v/std/email";
// Takoma Park, MD weather forecast
// https://weather-gov.github.io/api/gridpoints
const NOAA_URL_TKPK = "https://api.weather.gov/gridpoints/LWX/97,75/forecast/hourly";
interface Period {
number: number;
isDaytime: boolean;
probabilityOfPrecipitation: { value: number };
temperature: number;
windSpeed: string;
startTime: string;
endTime: string;
}
// Format an ISO datetime like Sun 06:00 AM
function fmtDate(d: string): string {
return new Date(d).toLocaleString("en-US", { weekday: "short", hour: "numeric", minute: "numeric", hour12: true });
}
// Format ISO datetime like 06:00 AM
function fmtTime(d: string): string {
return new Date(d).toLocaleString("en-US", { hour: "numeric", minute: "numeric", hour12: true });
}
// Retry a function n times with exponential backoff.
async function retry<T>(fn: () => Promise<T>, n: number): Promise<T> {
for (let i = 0; i < n; i++) {
try {
return await fn();
} catch (e) {
console.error(e);
const backoff = Math.floor(Math.random() * Math.pow(2, i));
await new Promise((resolve) => setTimeout(resolve, backoff * 1000));
}
}
throw new Error("Retry limit exceeded");
}
// Sleep a random amount of seconds (to respect NOAA's API).
async function sleepRand(seconds: number) {
const s = Math.random() * seconds * 1000;
await new Promise(r => setTimeout(r, s));
}
// Find some weather intervals!
async function run(): Promise<string> {
try {
await sleepRand(15);
const response = await fetch(NOAA_URL_TKPK, {
headers: { "User-Agent": "https://www.val.town/v/kingishb/blackLobster" },
});
const jsonData = await response.json();
// temperate time intervals
const temperate = jsonData.properties.periods.filter((p: Period) =>
p.isDaytime
&& p.probabilityOfPrecipitation.value < 25
&& p.temperature >= 50
&& p.temperature <= 80
&& parseInt(p.windSpeed.split(" ")[0]) < 13
);
if (temperate.length === 0) {
console.log("sadly, no good days 😒");
return;
}
// merge them together into time blocks
const blocks: Period[] = [];
let num = temperate.length > 0 ? temperate[0].number : 0;
for (const period of temperate) {
if (period.number === num + 1 && blocks.length > 0) {
const last = blocks.pop()!;
last.endTime = period.endTime;
blocks.push(last);
} else {
blocks.push(period);
}
num = period.number;
}
// format the message
const schedule = blocks.map((b) => `${fmtDate(b.startTime)} to ${fmtTime(b.endTime)}`).join("\n");
const msg = `bike times 🚲\n${schedule}`;
return msg;
} catch (error) {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 10, 2024