-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path50_Weather.py
More file actions
39 lines (33 loc) · 1.16 KB
/
50_Weather.py
File metadata and controls
39 lines (33 loc) · 1.16 KB
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
import requests
from plyer import notification
# 1. Get coordinates from city name
city = input("Enter city name: ")
geo_url = "https://geocoding-api.open-meteo.com/v1/search"
geo_params = {"name": city, "count": 1}
geo_res = requests.get(geo_url, params=geo_params).json()
if "results" in geo_res:
lat = geo_res["results"][0]["latitude"]
lon = geo_res["results"][0]["longitude"]
# 2. Get weather data
weather_url = "https://api.open-meteo.com/v1/forecast"
weather_params = {
"latitude": lat,
"longitude": lon,
"current_weather": True
}
weather_res = requests.get(weather_url, params=weather_params).json()
if "current_weather" in weather_res:
temp = weather_res["current_weather"]["temperature"]
wind = weather_res["current_weather"]["windspeed"]
weather_info = f"{city}: {temp}°C, Wind {wind} km/h"
print("Weather:", weather_info)
# 3. Cross-platform notification
notification.notify(
title="Weather Update",
message=weather_info,
timeout=5
)
else:
print("Weather data not found")
else:
print("City not found")