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
// Thx gpt4o!
interface GeoLocation {
srs: string;
latitude: string | number;
longitude: string | number;
}
interface CurrentValue {
status: string;
datetime: number;
date: string;
value: string | number;
volume?: number; // Make volume optional
}
interface CurrentValueCorrected extends CurrentValue {
value: number;
}
interface Location {
station_no?: string; // Make station_no optional
name: string;
short_name: string;
watershed: string;
subwatershed?: string;
type: string;
unitname: string;
unitsymbol: string;
category: string;
url: string;
geoLocation: GeoLocation;
recent_values?: Array<{
status: string;
datetime: number;
date: string;
value: string | number;
volume?: number; // Make volume optional
}>;
current_value?: CurrentValue;
}
interface Data {
unitTypes: Array<{
type: string;
label: string;
description: string;
unitCode: string;
}>;
locations: {
[key: string]: Location;
};
}
function getCurrentValue(jsonData: Data, locationKey: string): CurrentValueCorrected | undefined {
const location = jsonData.locations[locationKey];
const currentLocation = location ? location.current_value : undefined;
if (currentLocation) {
currentLocation.value = Number(currentLocation.value);
}
return currentLocation;
}
async function getData() {
const url = "https://minnehahacreek.org/data-json/request/";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data; // Store the JSON data in a variable
} catch (error) {
console.error("Error fetching data:", error);
}
}
export async function getFlowLevel() {
const data = await getData();
const graysBayDamCurrentValue = getCurrentValue(data, "mcwdgraysdischarge");
const hiawathaCurrentValue = getCurrentValue(data, "usgshiawatha");
return { graysBayDamCurrentValue, hiawathaCurrentValue };
}
const test = (async () => {
const results = await getFlowLevel();
console.log(results);
})();
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!
August 1, 2024