-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpollution_data.py
More file actions
35 lines (28 loc) · 900 Bytes
/
pollution_data.py
File metadata and controls
35 lines (28 loc) · 900 Bytes
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
import requests
def fetch_air_pollution_data(lat, lon, api_key):
"""
Fetch air pollution data from the OpenWeatherMap API.
Parameters:
lat (float): Latitude of the location.
lon (float): Longitude of the location.
api_key (str): Your OpenWeatherMap API key.
"""
url = f"http://api.openweathermap.org/data/2.5/air_pollution"
params = {
"lat": lat,
"lon": lon,
"appid": api_key
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
return extract_info_from_response(response.json())
except requests.exceptions.RequestException as e:
return None
def extract_info_from_response(json):
data = json["list"][0]
return {
"timestamp": data['dt'],
"air_quality_index": data['main']['aqi'],
"components": data['components'],
}