python-easy/openweather.py

51 lines
1.6 KiB
Python
Raw Normal View History

2023-09-29 11:38:03 +02:00
#!/usr/bin/env python3
import requests
from rich import print
from rich.panel import Panel
from rich.prompt import Prompt
from rich.columns import Columns
api_key = Prompt.ask("Enter your Api Key", password=True)
2023-09-29 11:38:03 +02:00
city_name = Prompt.ask("Enter your City", default="Göttingen")
base_url = "https://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
2023-09-29 13:25:31 +02:00
data = response.json()
2023-09-29 11:38:03 +02:00
2023-09-29 13:37:47 +02:00
print()
match response.status_code:
case 200:
info_main = data["main"]
info_weather = data["weather"]
current_temperature = info_main["temp"]
current_pressure = info_main["pressure"]
current_humidity = info_main["humidity"]
weather_description = info_weather[0]["description"]
print(Columns([
Panel(f"[blue]{round(current_temperature - 273.15, 2)}°C",
2023-09-29 13:43:43 +02:00
title="[bold]Temperature"),
2023-09-29 13:37:47 +02:00
Panel(f"[blue]{current_pressure}hPa",
2023-09-29 13:43:43 +02:00
title="[bold]Atmospheric Pressure"),
2023-09-29 13:37:47 +02:00
Panel(f"[blue]{current_humidity}%",
2023-09-29 13:43:43 +02:00
title="[bold]Humidity"),
2023-09-29 13:37:47 +02:00
Panel(f"[blue]{weather_description}",
2023-09-29 13:43:43 +02:00
title="[bold]Description"),
2023-09-29 13:37:47 +02:00
]))
case _:
if "message" in data:
print(Panel(data["message"],
title=f"[bold red]Error: {response.status_code}",
expand=False))
else:
print(Panel("unknown error",
title=f"[bold red]Error: {response.status_code}",
expand=False))