Skip to content

Instantly share code, notes, and snippets.

@keeth
Created December 30, 2022 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeth/03ce6290599636a6ae403d09ed1ae224 to your computer and use it in GitHub Desktop.
Save keeth/03ce6290599636a6ae403d09ed1ae224 to your computer and use it in GitHub Desktop.
Scrape weather conditions from whitewater website
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')),
}
}
@keeth
Copy link
Author

keeth commented Dec 30, 2022

Example output:

{ weather: 'Snowing', overnightSnowCm: 2, baseTempDegC: -7 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment