diff --git a/eco_project/backend/app.py b/eco_project/backend/app.py index c9f71f5..d589924 100644 --- a/eco_project/backend/app.py +++ b/eco_project/backend/app.py @@ -465,6 +465,45 @@ def livestock(): ] return jsonify(mock_data) +@app.route('/api/air_quality') +def air_quality(): + openaq_api_key = os.environ.get('OPENAQ_API_KEY') + if not openaq_api_key: + return jsonify({"error": "OpenAQ API key is not configured."}), 500 + + cities = ["New York", "Los Angeles", "Chicago", "Houston"] + url = "https://api.openaq.org/v3/latest" + headers = {"X-API-Key": openaq_api_key} + params = {"city": cities, "limit": 100} + + try: + response = requests.get(url, headers=headers, params=params) + response.raise_for_status() + data = response.json() + + formatted_data = [] + if data and data.get('results'): + processed_cities = set() + for result in data['results']: + city_name = result.get('city') + if city_name and city_name in cities and city_name not in processed_cities: + measurement = next((m for m in result.get('measurements', []) if m.get('parameter') == 'pm25'), None) + if not measurement and result.get('measurements'): + measurement = result['measurements'][0] + + if measurement: + formatted_data.append({ + "city": city_name, + "aqi": measurement.get('value'), + "pollutant": measurement.get('parameter') + }) + processed_cities.add(city_name) + + return jsonify(formatted_data) + + except requests.exceptions.RequestException as e: + return jsonify({"error": str(e)}), 500 + import json import bleach from filelock import FileLock diff --git a/eco_project/backend/static/air_quality.css b/eco_project/backend/static/air_quality.css new file mode 100644 index 0000000..3dc8547 --- /dev/null +++ b/eco_project/backend/static/air_quality.css @@ -0,0 +1,11 @@ +#data-container { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); + gap: 20px; +} + +.data-item { + border: 1px solid #ccc; + padding: 15px; + border-radius: 5px; +} diff --git a/eco_project/backend/static/air_quality.html b/eco_project/backend/static/air_quality.html new file mode 100644 index 0000000..03b3980 --- /dev/null +++ b/eco_project/backend/static/air_quality.html @@ -0,0 +1,30 @@ + + +
+ + +No air quality data found.
'; + return; + } + data.forEach(item => { + const itemElement = document.createElement('div'); + itemElement.classList.add('data-item'); + itemElement.innerHTML = ` +AQI: ${item.aqi}
+Pollutant: ${item.pollutant}
+ `; + dataContainer.appendChild(itemElement); + }); + }) + .catch(error => { + console.error('There has been a problem with your fetch operation:', error); + dataContainer.innerHTML = 'Could not fetch air quality data. Please try again later.
'; + }); +}); diff --git a/eco_project/backend/static/index.html b/eco_project/backend/static/index.html index b188942..6fa77b4 100644 --- a/eco_project/backend/static/index.html +++ b/eco_project/backend/static/index.html @@ -57,6 +57,7 @@