-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherapp.py
51 lines (40 loc) · 1.71 KB
/
weatherapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import asyncio
import datetime
import requests
from brilliant import Monocle
async def fetch_weather():
try:
# Step 1: Get the forecast URL
points_url = "https://api.weather.gov/points/39.7456,-97.0892"
response = requests.get(points_url)
response.raise_for_status()
forecast_url = response.json()['properties']['forecast']
# Step 2: Fetch the weather forecast
forecast_response = requests.get(forecast_url)
forecast_response.raise_for_status()
forecast_data = forecast_response.json()
current_weather = forecast_data['properties']['periods'][0]
description = current_weather['shortForecast']
temperature = current_weather['temperature']
temperature_unit = current_weather['temperatureUnit']
return f"{description}, {temperature} {temperature_unit}"
except requests.RequestException as e:
print(f"Failed to fetch weather data: {e}")
return "Failed to connect to weather service"
def generate_display_script(weather, current_time):
return f'''
import display
def update_display():
text = display.Text("Weather: {weather}", 100, 0, display.WHITE, justify=display.TOP_LEFT)
text2 = display.Text("Time: {current_time}", 100, 100, display.BLUE, justify=display.TOP_LEFT)
display.show(text, text2)
update_display()
'''
async def main():
async with Monocle() as monocle:
weather = await fetch_weather()
current_time = datetime.datetime.now().strftime("%H:%M")
remote_script = generate_display_script(weather, current_time)
await monocle.send_command(remote_script)
print("Monocle is now displaying weather information and time.")
asyncio.run(main())