From e9e90a1858340bc98959a79759600b323ed04252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Tue, 14 May 2024 16:57:45 +0200 Subject: [PATCH] refactor: Use nominatim module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim HĂ„rek Andreassen --- deno.lock | 6 +++- deps.ts | 4 +++ mod.ts | 4 +-- src/nominatim.ts | 84 ------------------------------------------------ src/util.ts | 8 ++--- src/yr.ts | 13 ++++---- 6 files changed, 21 insertions(+), 98 deletions(-) delete mode 100644 src/nominatim.ts diff --git a/deno.lock b/deno.lock index ceebb36..636ee93 100644 --- a/deno.lock +++ b/deno.lock @@ -12,7 +12,8 @@ "jsr:@std/fmt@0.215": "jsr:@std/fmt@0.215.0", "jsr:@std/fmt@0.221": "jsr:@std/fmt@0.221.0", "jsr:@std/fmt@^0.218.2": "jsr:@std/fmt@0.218.2", - "jsr:@std/text@0.221": "jsr:@std/text@0.221.0" + "jsr:@std/text@0.221": "jsr:@std/text@0.221.0", + "jsr:@timharek/nominatim@1.0.0": "jsr:@timharek/nominatim@1.0.0" }, "jsr": { "@cliffy/command@1.0.0-rc.4": { @@ -53,6 +54,9 @@ "dependencies": [ "jsr:@std/assert@^0.221.0" ] + }, + "@timharek/nominatim@1.0.0": { + "integrity": "75064090f20353e6b999a501bf3ad88b2052b2f90c4d7e2b50a3dcb54702d08c" } } }, diff --git a/deps.ts b/deps.ts index fc8f559..e7e32c1 100644 --- a/deps.ts +++ b/deps.ts @@ -1,3 +1,7 @@ export { format } from 'jsr:@std/datetime@^0.218.2'; export * as Colors from 'jsr:@std/fmt@^0.218.2/colors'; export { Command, CompletionsCommand } from 'jsr:@cliffy/command@1.0.0-rc.4'; +export { + coordinatesFromLocationName, + locationFromCoordinates, +} from 'jsr:@timharek/nominatim@1.0.0'; diff --git a/mod.ts b/mod.ts index a169fb0..a8b8456 100644 --- a/mod.ts +++ b/mod.ts @@ -1,6 +1,5 @@ /** - * Access Yr's weather API and Nominatim's names API for getting weather details - * about a specific location. + * Access Yr's weather API for getting weather forecast about a specific location. * * ## Example for current weather * @@ -25,6 +24,5 @@ * @module */ -export { Nominatim } from './src/nominatim.ts'; export { Yr } from './src/yr.ts'; export { getCurrent, getForecast } from './src/util.ts'; diff --git a/src/nominatim.ts b/src/nominatim.ts deleted file mode 100644 index 5d81ed7..0000000 --- a/src/nominatim.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { _fetch } from './util.ts'; - -const API_URL = new URL('https://nominatim.openstreetmap.org'); - -// TODO: Not a complete interface -interface SearchResult { - place_id: number; - licence: string; - osm_type: string; - osm_id: number; - lat: string; - lon: string; - class: string; - type: string; - place_rank: number; - importance: number; - addresstype: string; - name: string; - display_name: string; - boundingbox: string[]; - address?: { - village: string; - city: string; - }; -} - -/** - * Get coordinates based on name. - * - * @param name Query name - * @returns Coordinates assosiated with `name` - */ -async function getCoordinatesFromName( - name: string, -): Promise<{ lat: number; lng: number }> { - API_URL.pathname = '/search'; - API_URL.searchParams.set('format', 'json'); - API_URL.searchParams.set('q', name); - - try { - const result = await _fetch(API_URL); - - return { - lat: Number(result[0].lat), - lng: Number(result[0].lon), - }; - } catch (_e) { - console.error( - `Coordinates lookup failed. Check if "${name}" is spelled correct.`, - ); - Deno.exit(1); - } -} - -/** - * Get name based on coordinates. - * - * @param lat Latitude - * @param lng Longitude - * @returns Name assosiated with coordinates - */ -async function getNameFromCoordinates( - coordinates: Coordinates, -): Promise { - const { lat, lng } = coordinates; - API_URL.pathname = '/reverse'; - API_URL.searchParams.set('format', 'json'); - API_URL.searchParams.set('lat', String(lat)); - API_URL.searchParams.set('lon', String(lng)); - - try { - const result = await _fetch(API_URL); - - return result.address?.village || result.address?.city || 'null'; - } catch (_e) { - console.error('Name lookup failed'); - Deno.exit(1); - } -} - -export const Nominatim = { - getCoordinatesFromName, - getNameFromCoordinates, -}; diff --git a/src/util.ts b/src/util.ts index 14eaafb..38b4a73 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,4 @@ -import { Nominatim } from './nominatim.ts'; +import { coordinatesFromLocationName } from '../deps.ts'; import { Forecast, TimeseriesMinified, Yr } from './yr.ts'; /** @@ -32,7 +32,7 @@ export async function _fetch(url: string | URL): Promise { export async function getCurrent( locationName: string, ): Promise { - const { lat, lng } = await Nominatim.getCoordinatesFromName(locationName); + const { lat, lng } = await coordinatesFromLocationName(locationName); const url = Yr.getUrl(lat, lng); const yrResponse = await _fetch(url); @@ -52,7 +52,7 @@ export async function getForecast( locationName: string, interval = 1, ): Promise { - const { lat, lng } = await Nominatim.getCoordinatesFromName(locationName); + const { lat, lng } = await coordinatesFromLocationName(locationName); const url = Yr.getUrl(lat, lng); const yrResponse = await _fetch(url); @@ -70,7 +70,7 @@ export async function getForecast( export async function getTomorrow( locationName: string, ): Promise { - const { lat, lng } = await Nominatim.getCoordinatesFromName(locationName); + const { lat, lng } = await coordinatesFromLocationName(locationName); const url = Yr.getUrl(lat, lng); const yrResponse: Yr.IWeather = await _fetch(url); diff --git a/src/yr.ts b/src/yr.ts index 152b654..5d778c3 100644 --- a/src/yr.ts +++ b/src/yr.ts @@ -1,5 +1,4 @@ -import { format as formatDate } from '../deps.ts'; -import { Nominatim } from './nominatim.ts'; +import { format as formatDate, locationFromCoordinates } from '../deps.ts'; import { cleanForecast, getEarliestTimeseries, getUrl } from './util.ts'; export interface TimeseriesMinified { @@ -26,10 +25,11 @@ async function getCurrent( weatherData, ); const earliestTimeseries: Yr.ITimeseries = getEarliestTimeseries(timeseries); - const location_name = await Nominatim.getNameFromCoordinates(coordinates); + const location = await locationFromCoordinates(coordinates); + const locationName = location?.city ?? location?.village; return { - location_name, + location_name: locationName, ...getSimpleTimeseries(earliestTimeseries, units), } as TimeseriesMinified; } @@ -77,7 +77,8 @@ async function getForecast( weatherData, ); const { time: closestTime } = getEarliestTimeseries(timeseries); - const location_name = await Nominatim.getNameFromCoordinates(coordinates); + const location = await locationFromCoordinates(coordinates); + const locationName = location?.city ?? location?.village; const resultArray = weatherData.properties.timeseries.map((entry) => { const { data: { next_1_hours: nextHour }, time } = entry; @@ -91,7 +92,7 @@ async function getForecast( : cleanForecast(resultArray); return { - location_name, + location_name: locationName ?? '', array, }; } -- 2.45.2