forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_info.py
30 lines (22 loc) · 1.14 KB
/
weather_info.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
import os
import requests
API_KEY = os.getenv('OPENWEATHER_API_KEY')
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
def get_weather(city_name, units="metric"):
if not city_name.strip(): return "Please provide a valid city name."
if not API_KEY: return "API key not found."
url = f"{BASE_URL}q={city_name}&appid={API_KEY}&units={units}"
try:
data = requests.get(url).json()
if data["cod"] != 200:
return {"404": "City not found.", "401": "Invalid API key.", "429": "Too many requests."}.get(str(data["cod"]), "Error occurred.")
temp = data["main"]["temp"]
description = data["weather"][0]["description"]
unit_symbol = {"metric": "°C", "imperial": "°F"}.get(units, "K")
return f"Temperature: {temp:.2f}{unit_symbol}\nDescription: {description}"
except requests.RequestException as e:
return f"Error retrieving data: {e}"
if __name__ == "__main__":
city = input("Enter city name: ")
unit = input("Units (metric/imperial/leave empty for Kelvin): ").strip() or None
print(get_weather(city, unit))