Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/morning-briefing

src/sources/weather.ts

35 lines · 1.2 KB · TypeScript
import { skippedSection } from '../format.ts'
import { fetchWeather } from '../open-meteo.ts'
import type { BriefingInput, BriefingSection } from '../types.ts'

function round(value: number | null): string {
	return value === null ? '—' : String(Math.round(value))
}

export async function weatherSection(input: BriefingInput): Promise<BriefingSection> {
	const location = typeof input.location === 'string' ? input.location.trim() : ''
	if (!location) {
		return skippedSection(
			'weather',
			'Pass location (city or place name). Weather uses Open-Meteo and needs no OAuth.',
		)
	}

	const weather = await fetchWeather(location)
	const high = round(weather.daily.highC)
	const low = round(weather.daily.lowC)
	const current = round(weather.current.temperatureC)
	return {
		id: 'weather',
		title: 'Weather',
		status: 'ok',
		summary: `${weather.place} · ${weather.daily.weatherLabel} · ${high}° / ${low}°C`,
		items: [
			{
				title: `${weather.daily.weatherLabel}, ${high}° / ${low}°C`,
				detail: `Current ${current}°C, precip ${weather.daily.precipPct ?? 0}%, wind ${round(weather.current.windSpeedKmh)} km/h, humidity ${round(weather.current.humidityPct)}%. ${weather.attribution}`,
				label: 'today',
			},
		],
	}
}