all repos — iot-project @ 0627d8a0865451db9600af9ebcb04e083c391149

backend_iot/api.py (view raw)

 1from dotenv import load_dotenv
 2from os import getenv
 3import requests
 4load_dotenv()
 5
 6OWM_API_KEY = getenv("OWM_API_KEY", None)
 7
 8assert OWM_API_KEY is not None
 9api_url = "http://api.openweathermap.org/data/2.5/air_pollution?appid=" + OWM_API_KEY + "&lon={}&lat={}"
10
11
12def get_aqi_owm(latitude: float, longitude: float):
13    try:
14        response = requests.get(api_url.format(longitude, latitude))
15        weather_data = response.json()
16        return weather_data['list'][0]['main']['aqi']
17    except Exception as e:
18        print("Exception:", e)
19    return None