Created
October 17, 2023 19:53
-
-
Save keyboardcrunch/910dd634bcbaf2e5a6dfd754678fafd2 to your computer and use it in GitHub Desktop.
Weather service backend for 3rd party clients to present bike-nobike weather conditions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const kv = await Deno.openKv(); | |
export interface BikeStatus { | |
bike: boolean, | |
temp: number, | |
uv: number, | |
rain: number, | |
sunset: string, | |
sunrise: string | |
}; | |
export async function update(day: string, status: BikeStatus) { | |
await kv.set(['bikeNobike', day], status); | |
} | |
export async function query(day: string) { | |
const result = kv.get(["bikeNobike", day]); | |
return (await result).value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S deno run -A --unstable --allow-net | |
import { format } from "https://deno.land/std@0.193.0/datetime/mod.ts"; | |
import { Cron } from "https://deno.land/x/croner@7.0.3/dist/croner.js"; | |
import { update, query, BikeStatus} from "./db.ts"; | |
const zipcode = "REPLACE ME"; | |
const apikey = "REPLACE ME"; | |
const uri = `https://api.weatherapi.com/v1/forecast.json?key=${apikey}&q=${zipcode}&days=1&aqi=yes&alerts=no`; | |
const max_temp_f = 103; | |
const min_temp_f = 50; | |
const max_uv = 6; // 3-5 Low, 6-7 moderate, 8-10 High | |
function toBike(fc): boolean { | |
return ( | |
( fc.day.maxtemp_f <= max_temp_f ) && | |
( fc.day.mintemp_f >= min_temp_f ) && | |
( fc.day.uv <= max_uv ) && | |
( fc.day.daily_will_it_rain == 0 ) | |
); | |
} | |
// update the database every day at 3 AM | |
const job = new Cron("0 3 * * *", {}, async () => { | |
//import weather_data from "./example.json" assert { type: "json"}; | |
const day = format(new Date, "yyyy-MM-dd"); | |
const data = await fetch(uri); | |
const weather_data = await data.json(); | |
const forecast = weather_data.forecast.forecastday[0]; | |
const status: BikeStatus = { | |
temp: forecast.day.maxtemp_f, | |
uv: forecast.day.uv, | |
rain: forecast.day.daily_will_it_rain, | |
sunset: forecast.astro.sunset, | |
sunrise: forecast.astro.sunrise, | |
bike: toBike(forecast) | |
}; | |
update(day, status) | |
console.log("Database updated!"); | |
}); | |
// otherwise host an async http server to return current status from query() | |
async function handler(_req: Request): Response { | |
const day = format(new Date, "yyyy-MM-dd"); | |
const data = await query(day); | |
const jdata = JSON.stringify(data); | |
const resp = new Response(jdata); | |
resp.headers.set("Content-Type", "application/json") | |
return resp; | |
} | |
// returns json BikeStatus for 3rd party clients, localhost:8000/ | |
Deno.serve(handler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment