///
The `air-quality` repository leverages external APIs to retrieve and process air quality data. Two distinct approaches are present in the codebase: one utilizing the AirVisual API for real-time AQI up
114 views
~114 views from guests
Guest views are estimated from total page views. These include anonymous visitors and users who weren't logged in when they viewed the page.
The air-quality repository leverages external APIs to retrieve and process air quality data. Two distinct approaches are present in the codebase: one utilizing the AirVisual API for real-time AQI updates for the Telegram bot, and another exploring the OpenWeatherMap API for a more granular pollutant-based AQI calculation.
The primary method for fetching air quality data for the Telegram bot relies on the AirVisual API. This integration is demonstrated in airvisual.py and implemented within the bot.py script.
airvisual.pyThe airvisual.py script serves as a standalone example of how to query the AirVisual API. It fetches the current Air Quality Index (AQI) for Phnom Penh, Cambodia.
The API call targets a specific city, state, and country, requiring an API_KEY. The relevant data points extracted are the aqius (Air Quality Index for the US system) and mainus (main pollutant for the US system).
bot.pyThe Main class in bot.py incorporates this functionality through the get_phnom_penh_aq method. This method makes an identical API request to AirVisual, handling potential network issues with a retry mechanism.
The aqius value returned by this method is then used by the bot to determine the air quality category, generate advice, and format the messages sent to Telegram.
The openweather.py script explores an alternative method for air quality assessment using the OpenWeatherMap API. Unlike the AirVisual integration, the data fetched and processed by openweather.py is not currently consumed by bot.py for its Telegram updates. This script represents a separate exploration of air quality data and AQI calculation.
The fetch_air_quality function in openweather.py retrieves air pollution data based on geographical coordinates (latitude and longitude).
A crucial component of this script is the breakpoint dictionary, which defines the concentration ranges for various pollutants (PM2.5, PM10, O3, NO2, SO2, CO, NH3) corresponding to different AQI categories (good, moderate, unhealthy for sensitive groups, etc.). It also includes a conversion_factor to normalize units.
The index_calulator function takes the pollutant components from the OpenWeatherMap API response and calculates a sub-AQI for each pollutant. It identifies the category each pollutant falls into based on its concentration and the breakpoint ranges, then uses a linear interpolation formula to derive a corresponding AQI value.
The aqi_calculation function combines these steps to provide an overall AQI for a given location by fetching data, processing components, and then returning the maximum sub-AQI calculated across all pollutants, representing the most impactful pollutant.
This openweather.py script serves as an example of an alternative method for fetching and calculating AQI, demonstrating a more detailed, pollutant-by-pollutant approach compared to the AirVisual API's direct AQI reporting. However, its output is currently only printed to the console and not integrated into the [Overview] of the Telegram bot's operational workflow.