Compare commits

...

6 Commits

Author SHA1 Message Date
Marius Alwan Meyer
55b63764e8 Add license 2023-09-29 13:48:07 +02:00
Marius Alwan Meyer
02782a6ce3 Removed unnescessary style elements 2023-09-29 13:43:43 +02:00
Marius Alwan Meyer
69c0703e17 Handle errors when there was no json response 2023-09-29 13:41:35 +02:00
Marius Alwan Meyer
f1ce9fb455 Overhaul error handling 2023-09-29 13:37:47 +02:00
Marius Alwan Meyer
20a153e857 Read http status code from response object 2023-09-29 13:29:06 +02:00
Marius Alwan Meyer
55fdfc2c8d Rename bad variable names 2023-09-29 13:25:31 +02:00
2 changed files with 54 additions and 23 deletions

9
LICENSE Normal file
View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023 Marius Alwan Meyer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,11 +1,26 @@
#!/usr/bin/env python3
"""
MIT License
Copyright (c) 2023 Marius Alwan Meyer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
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)
city_name = Prompt.ask("Enter your City", default="Göttingen")
@@ -13,31 +28,38 @@ 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"]
data = response.json()
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",
title="[bold]Temperature[/bold]"),
title="[bold]Temperature"),
Panel(f"[blue]{current_pressure}hPa",
title="[bold]Atmospheric Pressure[/bold]"),
title="[bold]Atmospheric Pressure"),
Panel(f"[blue]{current_humidity}%",
title="[bold]Humidity[/bold]"),
title="[bold]Humidity"),
Panel(f"[blue]{weather_description}",
title="[bold]Description[/bold]"),
title="[bold]Description"),
]))
case _:
if "message" in data:
print(Panel(data["message"],
title=f"[bold red]Error: {response.status_code}",
expand=False))
else:
print(" City Not Found ")
print(Panel("unknown error",
title=f"[bold red]Error: {response.status_code}",
expand=False))