Skip to content

src/getPollen.ts

46 lines · 1.3 KB · TypeScript
Raw
import { getSnapshot, type GetSnapshotInput } from './getSnapshot'
import type { AirQualitySnapshot, PollenSnapshot } from './types'

export type GetPollenResult = {
	place: AirQualitySnapshot['place']
	source: AirQualitySnapshot['source']
	pollen: PollenSnapshot
	summary: string
	attribution: string
	warnings: string[]
	coverageNote: string
}

/**
 * Fetch pollen + allergy risk for a place via air-quality.com scrape (with fallbacks).
 * Use for allergy pulse; outside pollen season values are often null.
 *
 * @param input - Same as getSnapshot
 * @returns Pollen slice with source/coverage notes
 *
 * @example
 * import { getPollen } from 'kody:@reng/air-quality/getPollen'
 *
 * const bj = await getPollen({ location: '北京' })
 * // { pollen: { raw, birch, grass, ... }, source }
 */
export async function getPollen(input: GetSnapshotInput = {}): Promise<GetPollenResult> {
	const full = await getSnapshot(input)
	const p = full.pollen
	const summary =
		p.raw != null
			? `${full.place.name} 花粉 ${p.raw}` +
				(p.allergyRisk != null ? ` · 过敏风险 ${p.allergyRisk}` : '')
			: `${full.place.name}: no pollen values (off-season or uncovered)`
	return {
		place: full.place,
		source: full.source,
		pollen: full.pollen,
		summary,
		attribution: full.attribution,
		warnings: full.warnings,
		coverageNote: full.coverageNote,
	}
}

export default getPollen