python-easy/openweather.py

35 lines
1.1 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
city_name = Prompt.ask("Enter your City", default="Göttingen")
api_key = Prompt.ask("Enter your Api Key")
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)
x = response.json()
if x["cod"] != "404":
y = x["main"]
current_temperature = y["temp"]
current_pressure = y["pressure"]
current_humidity = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
print(Columns([
Panel(f"[bold]Temperature[/bold]\n[blue]{current_temperature} K", expand=True),
Panel(f"[bold]Atmospheric Pressure[/bold]\n[blue]{current_pressure} hPa", expand=True),
Panel(f"[bold]Humidity[/bold]\n[blue]{current_humidity} %", expand=True),
Panel(f"[bold]Description[/bold]\n[blue]{weather_description}", expand=True)
]))
else:
print(" City Not Found ")