Created
December 30, 2022 15:26
-
-
Save keeth/03ce6290599636a6ae403d09ed1ae224 to your computer and use it in GitHub Desktop.
Scrape weather conditions from whitewater website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JSDOM } from 'jsdom'; | |
import fetch from "node-fetch"; | |
export type WhitewaterConditions = { | |
weather: string; | |
overnightSnowCm: number; | |
baseTempDegC: number; | |
} | |
const WEB_URL = 'https://skiwhitewater.com/conditions/'; | |
function select(document: Document, selector: string): string { | |
const el = document.querySelector(selector); | |
if (!el) { | |
throw new Error(`Element not found at ${selector}`); | |
} | |
const text = el.textContent; | |
if (!text) { | |
throw new Error(`No textContent at ${selector}`); | |
} | |
return text.trim(); | |
} | |
export async function getWhitewaterConditions(): Promise<WhitewaterConditions> { | |
const res = await fetch(WEB_URL) | |
const text = await res.text(); | |
const dom = new JSDOM(text); | |
const document = dom.window.document; | |
return { | |
weather: select(document, '.snow-summary__item--weather .snow-summary__desc'), | |
overnightSnowCm: parseInt(select(document, '.snow-summary__item--overnight .snow-summary__desc .value')), | |
baseTempDegC: parseInt(select(document, '.snow-summary__item--base .snow-summary__desc .value')), | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: