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
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
import { msDay } from "https://esm.town/v/stevekrouse/msDay";
export let openAQLocation = async ({ lat, lon }: {
lat: number;
lon: number;
}) => {
const { results } = await fetchJSON(
"https://api.openaq.org/v2/locations?"
+ new URLSearchParams({
coordinates: lat.toPrecision(8) + "," + lon.toPrecision(8),
order_by: "distance",
sort: "asc",
radius: "25000",
dumpRaw: "false",
// entity: "government",
parameter_id: "2",
limit: "10",
page: "1",
offset: "0",
}),
);
if (!results?.length) throw new Error("No AQI locations found");
// find first (closest, given sort order) location with active pm25 sensor
return results.find((location) => {
const lastUpdated = new Date(
location.parameters.find((p) => p.parameter === "pm25")?.lastUpdated,
);
return (lastUpdated
&& new Date().getTime() - lastUpdated.getTime()
< msDay);
});
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
1
Glench avatar

This isn't working for me anymore. Got results is undefined for 02093

June 5, 2024