Skip to content

Commit

Permalink
Merge branch 'release-1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Milind220 committed Mar 11, 2022
2 parents 84f0427 + b5ade4f commit 3b48f73
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ozon3
version = 1.2.2
version = 1.3.0
author = Milind Sharma
author_email = [email protected]
description = A package to get air quality data using the WAQI API
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
description="A package to get air quality data using the WAQI API",
license="GPLv3+",
url="https://github.com/Milind220/Ozone",
version="1.2.2",
download_url="https://github.com/Milind220/Ozone/archive/refs/tags/v1.1.0.tar.gz",
version="1.3.0",
download_url="https://github.com/Milind220/Ozone/archive/refs/tags/v1.3.0.tar.gz",
packages=setuptools.find_packages(),
install_requires=[
"numpy; python_version>='3'",
Expand Down
51 changes: 45 additions & 6 deletions src/ozone/ozone.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@

class Ozone:
"""Primary class for Ozone API
This class contains all the methods used for live data collection.
This class should be instantiated, and methods should be called from the
instance.
instance.
Attributes:
token (str): The private API token for the WAQI API service.
"""

_search_aqi_url: str = URLs.search_aqi_url
_find_stations_url: str = URLs.find_stations_url
_default_params: List[str] = [
Expand All @@ -55,7 +56,7 @@ class Ozone:

def __init__(self, token: str = ""):
"""Initialises the class instance and sets the API token value
Args:
token (str): The users private API token for the WAQI API.
"""
Expand Down Expand Up @@ -146,10 +147,12 @@ def _parse_data(
"""Parse the data from the API response
Args:
data_obj (dict): The data from the API response.
data_obj (JSON object returned by json.loads): The data from the API response.
city (str): The city name.
params (List[str]): The parameters to parse.
Returns:
list: A list of dictionaries containing the data.
list: A list containing a single dictionary with the data.
"""
# A single row of data for the dataframe.
row: Dict[str, Union[str, float]] = {}
Expand Down Expand Up @@ -177,6 +180,9 @@ def _parse_data(
except KeyError:
# Gets triggered if the parameter is not provided by station.
row[param] = numpy.nan

# Return a list containing the dictionary so that it can be used with
# pandas.concat method later.
return [row]

def _AQI_meaning(self, aqi: float) -> Tuple[str, str]:
Expand Down Expand Up @@ -335,6 +341,39 @@ def get_multiple_city_air(
df.reset_index(inplace=True, drop=True)
return self._format_output(data_format, df)

def get_specific_parameter(
self,
city: str,
air_param: str = "",
) -> float:
"""Get specific parameter as a float
Args:
city (string): A city to get the data for
air_param (string): A string containing the specified air quality parameter.
Gets all parameters by default.
Returns:
float: Value of the specified parameter for the given city.
"""
result: float = 0.0
try:
r = self._make_api_request(
f"{self._search_aqi_url}/{city}/?token={self.token}"
)
if self._check_status_code(r):
data_obj = json.loads(r.content)["data"]
row = self._parse_data(data_obj, city, [air_param])[0]
result = float(row[air_param])

except KeyError:
print(
"Missing air quality parameter!\n"
+ "Try: get_specific_parameter(`city name`, `aqi` or `no2` or `co`)"
)

return result


if __name__ == "__main__":
pass
5 changes: 3 additions & 2 deletions src/ozone/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
@dataclass(frozen=True)
class URLs:
"""Class that contains the endpoint urls for the WAQI API
This class should not be instantiated. It only contains class level attributes,
and no methods at all. It is a static dataclass.
Attributes:
search_aqi_url (str): The endpoint used for retrieving air quality data.
find_stations_url (str): The endpoint used for retrieving a collection of air quality measuring stations.
"""

# Base API endpoint.
_base_url: str = "https://api.waqi.info/"

Expand Down

0 comments on commit 3b48f73

Please sign in to comment.