Created
April 30, 2023 22:41
-
-
Save Hypercubed/3b05ad028ea1cbc56ccb696600b50566 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Name: What to Wear | |
import "@johnlindquist/kit" | |
const gps = await db({ lat: 0, long: 0 }); | |
if (gps.data.lat === 0 && gps.data.long === 0) { | |
gps.data.lat = await arg("Enter your lat"); | |
gps.data.long = await arg("Enter your long"); | |
await gps.write(); | |
} | |
const { lat, long } = gps.data; | |
const w = await widget(` | |
{{location}} {{f}} {{description}} <img :src="top" /> | |
<p>{{ date }}</p> | |
`, { width: 800, height: 200 }); | |
refresh(); | |
setInterval(refresh, 60000); | |
async function refresh() { | |
const weather = await getWeather({ lat, long }); | |
const date = new Date().toLocaleTimeString(); | |
const f = (weather.celsius * 1.8 + 32).toFixed() + ' ºF'; | |
const top = getTop(weather.celsius); | |
w.setState({ ...weather, top, date, f }); | |
} | |
interface Weather { | |
celsius: number; | |
location: string; | |
description: string; | |
main: string; | |
} | |
async function getWeather(gps: ({ lat: number, long: number })): Promise<Weather> { | |
const { lat, long } = gps; | |
const api = `https://fcc-weather-api.glitch.me/api/current?lat=${lat}&lon=${long}`; | |
const response = await fetch(api); | |
const jsonData = await response.json(); | |
return { | |
celsius: jsonData.main.temp, | |
location: jsonData.name, | |
description: jsonData.weather[0].description, | |
main: jsonData.weather[0].main | |
}; | |
} | |
function getTop(celsius: number) { | |
if (celsius < 20) { | |
return 'https://hypercubed.github.io/wtw/assets/jacket.png'; | |
} else if (celsius < 30) { | |
return 'https://hypercubed.github.io/wtw/assets/hoodie.png'; | |
} else { | |
return 'https://hypercubed.github.io/wtw/assets/t-shirt.png'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment