-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
49 lines (38 loc) · 1.47 KB
/
Copy pathweather.py
File metadata and controls
49 lines (38 loc) · 1.47 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
40
41
42
43
44
45
46
47
48
49
import requests
import json
from decouple import config
WEATHER_API_KEY = config('WEATHER')
BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
class City:
def __init__(self, name, lon, lat):
self.name = name # name of the city
self.lon = lon # longitude
self.lat = lat # latitude
# Example city instance
agadir = City("Agadir", "-9.598107", "30.427755")
def construct_url(city, api_key):
return (f"{BASE_URL}lat={city.lat}&lon={city.lon}"
f"&lang=en&units=metric&appid={api_key}")
URL = construct_url(agadir, WEATHER_API_KEY)
def getCurrentWeather():
try:
response = requests.get(URL)
response.raise_for_status() # Will raise an HTTPError for bad responses
data = response.json()
main = data.get('main', {})
weather = data.get('weather', [{}])[0]
temperature = main.get('temp', 'N/A')
humidity = main.get('humidity', 'N/A')
pressure = main.get('pressure', 'N/A')
description = weather.get('description', 'N/A')
return (f"\n{agadir.name:-^30}\n"
f"Temperature: {temperature} °C\n"
f"Humidity: {humidity} %\n"
f"Pressure: {pressure} hPa\n"
f"Weather Report: {description}\n")
except requests.exceptions.RequestException as e:
print(f"HTTP request failed: {e}")
return "Failed to get weather data."
# Example usage
if __name__ == "__main__":
print(getCurrentWeather())