Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Last active March 7, 2024 11:41
Show Gist options
  • Save coderbyheart/b9719f162b004698a7ace98c8b30a8c1 to your computer and use it in GitHub Desktop.
Save coderbyheart/b9719f162b004698a7ace98c8b30a8c1 to your computer and use it in GitHub Desktop.
Kartverket API for vasstandsdata
import { xml2json } from "xml-js";
const station = async ({ latitude, longitude }) => {
const req = `https://api.sehavniva.no/tideapi.php?${new URLSearchParams({
lat: latitude,
lon: longitude,
datatype: "obs",
refcode: "cd",
interval: 60,
dst: 0,
tzone: 0,
place: "",
file: "",
lang: "nn",
tide_request: "locationdata",
fromtime: new Date(Date.now() - 2 * 60 * 60 * 1000)
.toISOString()
.slice(0, 16),
totime: new Date().toISOString(-60 * 60 * 1000).slice(0, 16),
})}`;
const measure = JSON.parse(xml2json(await (await fetch(req)).text()));
return measure.elements[0].elements[0].elements[2].elements.map(
({ attributes }) => attributes
);
};
/**
* {
name: 'Sirevåg',
code: 'SIE',
latitude: '58.505200',
longitude: '5.791602',
type: 'PERM'
}[]
*/
const stations = async () => {
const stationsXML = await (
await fetch(
"https://api.sehavniva.no/tideapi.php?type=perm&tide_request=stationlist"
)
).text();
const stationsJSON = JSON.parse(
xml2json(stationsXML, { compact: true, spaces: 4 })
);
/**
*
*/
const stations = stationsJSON.tide.stationinfo.location.map(
({ _attributes }) => _attributes
);
return stations;
};
for (const s of await stations()) {
console.log(s);
console.log(await station(s));
break;
}
ANX
BGO
BOO
BRJ
HFT
HAR
HEI
HRO
HVG
KAB
KSU
LEH
MSU
MAY
NVK
NYA
OSC
OSL
RVK
SBG
SIE
SOY
SVG
TRG
TOS
TRD
TAZ
VAW
VIK
AES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment