Skip to content

Commit

Permalink
Force simple data on "auto"
Browse files Browse the repository at this point in the history
  • Loading branch information
victrme committed Oct 26, 2024
1 parent a1a2795 commit 4f11561
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ async function main(request: Request) {
console.error(err)
}

if (data === 'all' && json) {
if (params.data === 'all' && json) {
body = JSON.stringify(json)
}

if (data === 'simple' && json) {
if (params.data === 'simple' && json) {
if (isAccuweather(json) || isForeca(json)) {
body = JSON.stringify(toSimpleWeather(json, params))
}
Expand Down Expand Up @@ -139,6 +139,7 @@ function sanitizeParams(params: Record<string, string>): QueryParams {

async function tryNoCatch<Result>(fn: (_: QueryParams) => Promise<Result>, args: QueryParams): Promise<Result | undefined> {
try {
console.log(args)
return await fn(args)
} catch (_) {
return undefined
Expand Down
19 changes: 7 additions & 12 deletions src/providers/simple.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { AccuWeather, Foreca, QueryParams, SimpleWeather } from '../types.ts'
import { isAccuweather, isForeca } from '../types.ts'

export default function toSimpleWeather(
json: AccuWeather | Foreca,
params: QueryParams,
): SimpleWeather {
export default function toSimpleWeather(json: AccuWeather | Foreca, params: QueryParams): SimpleWeather {
const { provider, unit } = params

console.log(json)

const simple: SimpleWeather = {
meta: { ...json.meta },
geo: { ...json.geo },
Expand All @@ -23,7 +22,7 @@ export default function toSimpleWeather(
daily: [] as SimpleWeather['daily'],
}

if (provider === 'foreca' && isForeca(json)) {
if ((provider === 'auto' || provider === 'foreca') && isForeca(json)) {
const degrees = unit === 'F' ? 'f' : 'c'

simple.now.icon = transformToSimpleIcon(simple.now.icon, 'foreca')
Expand All @@ -41,7 +40,7 @@ export default function toSimpleWeather(
}
}

if (provider === 'accuweather' && isAccuweather(json)) {
if ((provider === 'auto' || provider === 'accuweather') && isAccuweather(json)) {
simple.now.icon = transformToSimpleIcon(simple.now.icon, 'accuweather')
simple.now.temp = json.now.temp
simple.now.feels = json.now.feels
Expand All @@ -60,10 +59,7 @@ export default function toSimpleWeather(
return simple
}

function transformToSimpleIcon(
id: string,
provider: 'accuweather' | 'foreca',
): string {
function transformToSimpleIcon(id: string, provider: 'accuweather' | 'foreca'): string {
for (const [simpleId, providerIds] of Object.entries(SIMPLE_ICONS)) {
const list = providerIds[provider]

Expand Down Expand Up @@ -123,8 +119,7 @@ export const SIMPLE_ICONS = Object.freeze({
},
snow: {
accuweather: '20, 21, 22, 23, 24, 25, 26, 43, 44',
foreca:
'd221, d311, d411, d221, d321, d431, d212, d312, d412, d222, d322, d422, d432, n221, n311, n411, n221, n321, n431, n212, n312, n412, n222, n322, n422, n432',
foreca: 'd221, d311, d411, d221, d321, d431, d212, d312, d412, d222, d322, d422, d432, n221, n311, n411, n221, n321, n431, n212, n312, n412, n222, n322, n422, n432',
},
mist: {
accuweather: '11',
Expand Down

0 comments on commit 4f11561

Please sign in to comment.