add weather class
All checks were successful
NPM Audit Check / Check NPM audit (push) Successful in -2m17s
All checks were successful
NPM Audit Check / Check NPM audit (push) Successful in -2m17s
This commit is contained in:
parent
3b562116fd
commit
7f45387dac
@ -114,5 +114,11 @@
|
|||||||
"directory": "audio/voice/cory/",
|
"directory": "audio/voice/cory/",
|
||||||
"extension": "flac"
|
"extension": "flac"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"weather": {
|
||||||
|
// Provide an OpenWeatherMap API key
|
||||||
|
// https://openweathermap.org/price
|
||||||
|
"key": "not0a0real0key00281f631aef6ad3a1",
|
||||||
|
"city": "chicago"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,15 @@ import json5 from 'json5';
|
|||||||
import Sequencer from './sequencer.js';
|
import Sequencer from './sequencer.js';
|
||||||
import type {Programs, Segments, Sequences} from './sequencer.js';
|
import type {Programs, Segments, Sequences} from './sequencer.js';
|
||||||
import type { Voices } from './voice.js';
|
import type { Voices } from './voice.js';
|
||||||
|
import type { WeatherConfig } from './weather.js';
|
||||||
|
|
||||||
|
|
||||||
interface Config {
|
interface Config {
|
||||||
programs: Programs,
|
programs: Programs,
|
||||||
segments: Segments,
|
segments: Segments,
|
||||||
sequences: Sequences,
|
sequences: Sequences,
|
||||||
voices: Voices
|
voices: Voices,
|
||||||
|
weather: WeatherConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('morning-report\nCory Sanin 2025\n');
|
console.log('morning-report\nCory Sanin 2025\n');
|
||||||
|
101
src/weather.ts
101
src/weather.ts
@ -0,0 +1,101 @@
|
|||||||
|
import OpenWeatherMap from 'openweathermap-ts';
|
||||||
|
import type { ThreeHourResponse, CurrentResponse, CountryCode } from 'openweathermap-ts/dist/types/index.js';
|
||||||
|
|
||||||
|
interface CityName {
|
||||||
|
cityName: string;
|
||||||
|
state: string;
|
||||||
|
countryCode: CountryCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface WeatherConfig {
|
||||||
|
key: string;
|
||||||
|
lang?: string;
|
||||||
|
coordinates?: number[] | string;
|
||||||
|
zip?: number;
|
||||||
|
country?: CountryCode;
|
||||||
|
cityid?: number;
|
||||||
|
city?: CityName;
|
||||||
|
}
|
||||||
|
|
||||||
|
type LocationType = 'coordinates' | 'zip' | 'cityid' | 'city' | null;
|
||||||
|
|
||||||
|
function parseCoords(coords: number[] | string): number[] {
|
||||||
|
if (typeof coords == 'string') {
|
||||||
|
return coords.replace(/\s/g, '').split(',').map(Number.parseFloat);
|
||||||
|
}
|
||||||
|
return coords;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Weather {
|
||||||
|
private openWeather: OpenWeatherMap.default;
|
||||||
|
private locationType: LocationType;
|
||||||
|
private current: CurrentResponse;
|
||||||
|
private threeDay: ThreeHourResponse;
|
||||||
|
|
||||||
|
constructor(options: WeatherConfig) {
|
||||||
|
this.locationType = this.current = this.threeDay = null;
|
||||||
|
this.openWeather = new OpenWeatherMap.default({
|
||||||
|
apiKey: options.key
|
||||||
|
});
|
||||||
|
if ('city' in options && 'cityName' in options.city && 'state' in options.city && 'countryCode' in options.city) {
|
||||||
|
this.openWeather.setCityName(options.city);
|
||||||
|
this.locationType = 'city';
|
||||||
|
}
|
||||||
|
if ('cityid' in options) {
|
||||||
|
this.openWeather.setCityId(options.cityid);
|
||||||
|
this.locationType = 'cityid';
|
||||||
|
}
|
||||||
|
if ('zip' in options && 'country' in options) {
|
||||||
|
this.openWeather.setZipCode(options.zip, options.country)
|
||||||
|
this.locationType = 'zip';
|
||||||
|
}
|
||||||
|
if ('coordinates' in options) {
|
||||||
|
const coords = parseCoords(options.coordinates);
|
||||||
|
if (coords.length >= 2) {
|
||||||
|
this.openWeather.setGeoCoordinates(coords[0], coords[1]);
|
||||||
|
this.locationType = 'coordinates';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getCurrentWeather(): Promise<CurrentResponse> {
|
||||||
|
if (this.current) {
|
||||||
|
return this.current;
|
||||||
|
}
|
||||||
|
switch (this.locationType) {
|
||||||
|
case 'city':
|
||||||
|
return this.current = await this.openWeather.getCurrentWeatherByCityName();
|
||||||
|
case 'cityid':
|
||||||
|
return this.current = await this.openWeather.getCurrentWeatherByCityId();
|
||||||
|
case 'zip':
|
||||||
|
return this.current = await this.openWeather.getCurrentWeatherByZipcode();
|
||||||
|
case 'coordinates':
|
||||||
|
return this.current = await this.openWeather.getCurrentWeatherByGeoCoordinates();
|
||||||
|
default:
|
||||||
|
throw new Error(`Can't fetch weather for location type '${this.locationType}'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getThreeHourForecast(): Promise<ThreeHourResponse> {
|
||||||
|
if (this.threeDay) {
|
||||||
|
return this.threeDay;
|
||||||
|
}
|
||||||
|
switch (this.locationType) {
|
||||||
|
case 'city':
|
||||||
|
return this.threeDay = await this.openWeather.getThreeHourForecastByCityName();
|
||||||
|
case 'cityid':
|
||||||
|
return this.threeDay = await this.openWeather.getThreeHourForecastByCityId();
|
||||||
|
case 'zip':
|
||||||
|
return this.threeDay = await this.openWeather.getThreeHourForecastByZipcode();
|
||||||
|
case 'coordinates':
|
||||||
|
return this.threeDay = await this.openWeather.getThreeHourForecastByGeoCoordinates();
|
||||||
|
default:
|
||||||
|
throw new Error(`Can't fetch weather for location type '${this.locationType}'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Weather;
|
||||||
|
export { Weather };
|
||||||
|
export type { WeatherConfig, CityName };
|
Loading…
x
Reference in New Issue
Block a user