Skip to content

Commit

Permalink
Get geolocation list first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
victrme committed Nov 16, 2024
1 parent b041db6 commit 2c1f9f6
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 31 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
This service cleverly transforms weather web pages into a usable (and free!) rest API. It uses [accuweather](https://accuweather.com) and/or
[foreca](https://foreca.com) under the hood.

- Sturdy: Uses other providers as fallback to guarentee a response if a provider becomes invalid
- Flexible: Easy to update with strong typing and htmlparser2 as basic parser
- Compatible: Pure typescript using deno and small dependencies means you can install it almost anywhere
- Sturdy: Uses other providers as fallback to guarentee a response if a provider becomes invalid
- Flexible: Easy to update with strong typing and htmlparser2 as basic parser
- Compatible: Pure typescript using deno and small dependencies means you can install it almost anywhere

## Install

Expand Down Expand Up @@ -58,6 +58,7 @@ Define a weather provider to start using the API.
| Parameter | Type | Required | Description |
| --------- | ----------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| provider | accuweather, foreca, ~~weathercom~~, auto | required | Choose the weather provider. "Auto" returns "simple" data, specific providers returns all, see "data". |
| geo | any | optional | Geo returns a list of found locations. |
| query, q | string | optional | Matches a location based on your query. Best to use as "City,CountryCode". Adding "query" overrides "lat" & "lon" parameters. |
| lat | string | optional | Location latitude. |
| lon | string | optional | Location longitude. |
Expand Down Expand Up @@ -109,8 +110,8 @@ Define a weather provider to start using the API.

## Simple icon equivalences

- Accuweather: https://developer.accuweather.com/weather-icons
- Foreca: https://developer.foreca.com/resources
- Accuweather: https://developer.accuweather.com/weather-icons
- Foreca: https://developer.foreca.com/resources

As a union:

Expand Down Expand Up @@ -169,10 +170,10 @@ Equivalence between other providers:

Language codes are following the ISO-639 standard. A wrong language throws an error. Sanitized so that:

- `lang` is case insensitive
- `-` or `_` works
- `pt` resolves to `pt-pt`
- localization (-XX) is removed with `foreca`
- `lang` is case insensitive
- `-` or `_` works
- `pt` resolves to `pt-pt`
- localization (-XX) is removed with `foreca`

| code | name | foreca | accuweather |
| ----- | ----------------------- | ------ | ----------- |
Expand All @@ -199,7 +200,7 @@ Language codes are following the ISO-639 standard. A wrong language throws an er
| pl | Polski | true | true |
| ca | Català | | true |
| pt-br | Português (Brazil) | | true |
| hi | हिन्दी | | true |
| hi | हिन्दी | | true |
| ru | русский | true | true |
| ar | عربي | | true |
| el | Ελληνικά | true | true |
Expand All @@ -225,18 +226,18 @@ Language codes are following the ISO-639 standard. A wrong language throws an er
| th | ไทย | | true |
| vi | Tiếng Việt | | true |
| fa | فارسی | | true |
| bn | বাংলা | | true |
| bn | বাংলা | | true |
| bs | bosanski | | true |
| is | íslenska | | true |
| sw | Kiswahili | | true |
| ur | اُردُو | | true |
| ur | اُردُو | | true |
| sr-me | Crnogorski | | true |
| uz | Oʻzbekcha | | true |
| az | Azərbaycanca | | true |
| ta | தமிழ் | | true |
| gu | ગુજરાતી | | true |
| kn | ಕನ್ನಡ | | true |
| te | తెలుగు | | true |
| ta | தமிழ் | | true |
| gu | ગુજરાતી | | true |
| kn | ಕನ್ನಡ | | true |
| te | తెలుగు | | true |
| mr | मराठी | | true |
| pa | ਪੰਜਾਬੀ | | true |
| my | မြန်မာဘာသာ | | true |
| pa | ਪੰਜਾਬੀ | | true |
| my | မြန်မာဘာသာ | | true |
48 changes: 39 additions & 9 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
<h1>Racle météo</h1>

<p>
This service cleverly transforms weather web pages into a usable (and free!) rest API. It uses
This service cleverly transforms weather web pages into a usable (and free!) rest API.
It uses
<a href="https://accuweather.com">accuweather</a>
and/or
<a href="https://foreca.com">foreca</a> under the hood.
Expand Down Expand Up @@ -108,6 +109,19 @@ <h2>Test</h2>
</label>
</p>

<p>
Geo <br />
<label>
<input type="radio" name="geo" value="" checked />
Off
</label>
<br />
<label>
<input type="radio" name="geo" value="true" />
On
</label>
</p>

<p>
Data <br />
<label>
Expand Down Expand Up @@ -191,15 +205,24 @@ <h2>Parameters</h2>
<th scope="row">provider</th>
<td>accuweather, foreca</td>
<td>required</td>
<td>Choose the weather provider. By default returns all available data, see "data".</td>
<td>
Choose the weather provider. By default returns all available data, see
"data".
</td>
</tr>
<tr>
<th scope="row">geo</th>
<td>any</td>
<td>optional</td>
<td>Geo returns a list of found locations.</td>
</tr>
<tr>
<th scope="row">query</th>
<td>string</td>
<td>optional</td>
<td>
Matches a location based on your query. Best to use as "City,CountryCode". Adding "query" overrides "lat" & "lon"
parameters.
Matches a location based on your query. Best to use as "City,CountryCode".
Adding "query" overrides "lat" & "lon" parameters.
</td>
</tr>
<tr>
Expand All @@ -219,7 +242,8 @@ <h2>Parameters</h2>
<td>string</td>
<td>optional</td>
<td>
English by default. Some languages are only available on accuweather, see language list below. Incorrect
English by default. Some languages are only available on accuweather, see
language list below. Incorrect
<code>lang</code>
does not fallback to english.
</td>
Expand All @@ -228,22 +252,28 @@ <h2>Parameters</h2>
<th scope="row">unit</th>
<td>C, F</td>
<td>optional</td>
<td>Useful for accuweather or when using "simple" data. Foreca always returns celsius and farenheit.</td>
<td>
Useful for accuweather or when using "simple" data. Foreca always returns
celsius and farenheit.
</td>
</tr>
<tr>
<th scope="row">data</th>
<td>all, simple</td>
<td>optional</td>
<td>
Select "all" to retrieve all the data from the provider's webpage. "simple" returns only data available for all
providers. "all" by default.
Select "all" to retrieve all the data from the provider's webpage. "simple"
returns only data available for all providers. "all" by default.
</td>
</tr>
<tr>
<th scope="row">debug</th>
<td>nodes, content, geo</td>
<td>optional</td>
<td>Debugging "nodes" returns a list of found html tags. "content" shows strings collected before being manipulated.</td>
<td>
Debugging "nodes" returns a list of found html tags. "content" shows strings
collected before being manipulated.
</td>
</tr>
</tbody>
</table>
Expand Down
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as accuweather from './providers/accuweather.ts'
import toSimpleWeather from './providers/simple.ts'
import { isAccuweather, isForeca } from './types.ts'

import type { AccuWeather, Foreca, QueryParams } from './types.ts'
import type { AccuWeather, Foreca, QueryParams, SimpleLocations } from './types.ts'

/**
* Racle-météo can be called like a Cloudflare Worker, using fetch().
Expand All @@ -31,6 +31,7 @@ async function main(request: Request) {
const cf_longitude = (request as CFRequest)?.cf?.longitude

const url = new URL(request.url)
const geo = url.searchParams.get('geo') ?? ''
const debug = url.searchParams.get('debug') ?? ''
const unit = url.searchParams.get('unit') ?? 'C'
const lang = url.searchParams.get('lang') ?? 'en'
Expand All @@ -40,7 +41,7 @@ async function main(request: Request) {
const lon = url.searchParams.get('lon') ?? cf_longitude ?? '0'
const provider = url.searchParams.get('provider') ?? ''

const params = sanitizeParams({ lat, lon, lang, unit, provider, data, query, debug })
const params = sanitizeParams({ lat, lon, lang, unit, provider, data, query, debug, geo })

let body = ''
let status = 200
Expand Down Expand Up @@ -84,6 +85,28 @@ async function main(request: Request) {
// }
}

if (params.geo && params.provider) {
let list: SimpleLocations = []

if (params.provider === 'accuweather') {
list = await accuweather.geo(params)
}

if (params.provider === 'foreca') {
list = await foreca.geo(params)
}

return new Response(JSON.stringify(list), {
status,
headers: {
'access-control-allow-methods': 'GET',
'access-control-allow-origin': '*',
'content-type': contentType,
'cache-control': cacheControl,
},
})
}

try {
if (url.pathname !== '/' && url.pathname !== '/weather') {
status = 404
Expand Down Expand Up @@ -146,6 +169,7 @@ function sanitizeParams(params: Record<string, string>): QueryParams {
let debug: QueryParams['debug'] = ''
let lat: QueryParams['lat'] = params.lat
let lon: QueryParams['lon'] = params.lon
let geo: QueryParams['geo'] = params.geo || undefined

if (params.provider === 'auto') {
params.data = 'simple'
Expand Down Expand Up @@ -188,6 +212,7 @@ function sanitizeParams(params: Record<string, string>): QueryParams {
debug: debug,
lat: lat,
lon: lon,
geo: geo,
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/providers/accuweather.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parser, { find, findAll, getAll } from '../parser.ts'

import type { FlatNode } from '../parser.ts'
import type { AccuWeather, AccuweatherContent, AccuWeatherGeolocation, QueryParams } from '../types.ts'
import type { AccuWeather, AccuweatherContent, AccuWeatherGeolocation, QueryParams, SimpleLocations } from '../types.ts'

const ACCUWEATHER_LANGS =
'en_us, es, fr, da, pt_pt, nl, no, it, de, sv, fi, zh_hk, zh_cn, zh_tw, es_ar, es_mx, sk, ro, cs, hu, pl, ca, pt_br, hi, ru, ar, el, en_gb, ja, ko, tr, fr_ca, hr, sl, uk, id, bg, et, kk, lt, lv, mk, ms, tl, sr, th, vi, fa, bn, bs, is, sw, ur, sr_me, uz, az, ta, gu, kn, te, mr, pa, my'
Expand All @@ -16,6 +16,10 @@ export default async function accuweather(params: QueryParams): Promise<AccuWeat
return api
}

export async function geo(params: QueryParams): Promise<SimpleLocations> {
return await geolocationFromQuery(params.query)
}

export async function debugContent(params: QueryParams): Promise<AccuweatherContent> {
const html = await fetchPageContent(params)
await parser(html)
Expand Down
12 changes: 11 additions & 1 deletion src/providers/foreca.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parser, { find, findAll, getAll, next, prev, prevAll } from '../parser.ts'

import type { FlatNode } from '../parser.ts'
import type { Foreca, ForecaContent, ForecaGeo, QueryParams } from '../types.ts'
import type { Foreca, ForecaContent, ForecaGeo, QueryParams, SimpleLocations } from '../types.ts'

const FORECA_LANGS = 'en, bg, cs, da, de, et, el, es, fr, hr, it, lv, hu, nl, pl, pt, ro, ru, sk, sv, uk'

Expand All @@ -19,6 +19,16 @@ export default async function foreca(params: QueryParams): Promise<Foreca> {
return api
}

export async function geo(params: QueryParams): Promise<SimpleLocations> {
const res = await getForecaLocation({
lat: params.lat,
lon: params.lon,
query: params.query,
})

return res
}

export async function debugContent(params: QueryParams): Promise<ForecaContent> {
const html = await fetchPageContent(params)
await parser(html)
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface QueryParams {
lang: string
lat?: string
lon?: string
geo?: unknown
}

/*************
Expand Down Expand Up @@ -50,6 +51,11 @@ export interface SimpleWeather {
}[]
}

export type SimpleLocations = {
name: string
longName: string
}[]

/*****************
Accuweather
******************/
Expand Down

0 comments on commit 2c1f9f6

Please sign in to comment.