python-apims 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_apims-0.1.0/PKG-INFO +41 -0
- python_apims-0.1.0/README.md +14 -0
- python_apims-0.1.0/pyproject.toml +50 -0
- python_apims-0.1.0/setup.cfg +4 -0
- python_apims-0.1.0/src/apims/__init__.py +31 -0
- python_apims-0.1.0/src/apims/client.py +78 -0
- python_apims-0.1.0/src/apims/const.py +23 -0
- python_apims-0.1.0/src/apims/exceptions.py +10 -0
- python_apims-0.1.0/src/apims/models.py +126 -0
- python_apims-0.1.0/src/python_apims.egg-info/PKG-INFO +41 -0
- python_apims-0.1.0/src/python_apims.egg-info/SOURCES.txt +13 -0
- python_apims-0.1.0/src/python_apims.egg-info/dependency_links.txt +1 -0
- python_apims-0.1.0/src/python_apims.egg-info/requires.txt +6 -0
- python_apims-0.1.0/src/python_apims.egg-info/top_level.txt +1 -0
- python_apims-0.1.0/tests/test_apims.py +61 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-apims
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Asynchronous Python client for Malaysia DOE APIMS (Air Pollutant Index Management System)
|
|
5
|
+
Author: Zubir Jamal
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/zubirjamal/python-apims
|
|
8
|
+
Project-URL: Repository, https://github.com/zubirjamal/python-apims
|
|
9
|
+
Project-URL: Issues, https://github.com/zubirjamal/python-apims/issues
|
|
10
|
+
Keywords: apims,air-quality,ipu,api,malaysia,home-assistant
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Topic :: Home Automation
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
Requires-Dist: aiohttp>=3.9.0
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Requires-Dist: pytest>=8.0.0; extra == "test"
|
|
25
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == "test"
|
|
26
|
+
Requires-Dist: aresponses>=3.0.0; extra == "test"
|
|
27
|
+
|
|
28
|
+
# python-apims
|
|
29
|
+
|
|
30
|
+
Asynchronous Python client for Malaysia Department of Environment (DOE) APIMS (Air Pollutant Index Management System).
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install python-apims
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
- Fetches real-time Air Pollutant Index (API / IPU) from all 68 nationwide monitoring stations in Malaysia.
|
|
40
|
+
- Provides PM2.5 and PM10 concentrations, station locations, categories, and timestamps.
|
|
41
|
+
- Spatial helpers for locating nearest air quality monitoring stations.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# python-apims
|
|
2
|
+
|
|
3
|
+
Asynchronous Python client for Malaysia Department of Environment (DOE) APIMS (Air Pollutant Index Management System).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install python-apims
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
- Fetches real-time Air Pollutant Index (API / IPU) from all 68 nationwide monitoring stations in Malaysia.
|
|
13
|
+
- Provides PM2.5 and PM10 concentrations, station locations, categories, and timestamps.
|
|
14
|
+
- Spatial helpers for locating nearest air quality monitoring stations.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "python-apims"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Asynchronous Python client for Malaysia DOE APIMS (Air Pollutant Index Management System)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Zubir Jamal" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["apims", "air-quality", "ipu", "api", "malaysia", "home-assistant"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Topic :: Home Automation",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
"Intended Audience :: Developers",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"aiohttp>=3.9.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
test = [
|
|
33
|
+
"pytest>=8.0.0",
|
|
34
|
+
"pytest-asyncio>=0.23.0",
|
|
35
|
+
"aresponses>=3.0.0",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Homepage = "https://github.com/zubirjamal/python-apims"
|
|
40
|
+
Repository = "https://github.com/zubirjamal/python-apims"
|
|
41
|
+
Issues = "https://github.com/zubirjamal/python-apims/issues"
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.packages.find]
|
|
44
|
+
where = ["src"]
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
asyncio_mode = "auto"
|
|
48
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
49
|
+
pythonpath = ["src"]
|
|
50
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""APIMS Python client."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from .client import ApimsClient
|
|
5
|
+
from .const import API_PARAMS, API_URL, MYT, REQUEST_TIMEOUT, USER_AGENT
|
|
6
|
+
from .exceptions import ApimsConnectionError, ApimsError
|
|
7
|
+
from .models import (
|
|
8
|
+
Station,
|
|
9
|
+
distance_km,
|
|
10
|
+
find_nearest_station,
|
|
11
|
+
parse_concentration,
|
|
12
|
+
parse_station_attributes,
|
|
13
|
+
parse_time,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"API_PARAMS",
|
|
18
|
+
"API_URL",
|
|
19
|
+
"ApimsClient",
|
|
20
|
+
"ApimsConnectionError",
|
|
21
|
+
"ApimsError",
|
|
22
|
+
"MYT",
|
|
23
|
+
"REQUEST_TIMEOUT",
|
|
24
|
+
"Station",
|
|
25
|
+
"USER_AGENT",
|
|
26
|
+
"distance_km",
|
|
27
|
+
"find_nearest_station",
|
|
28
|
+
"parse_concentration",
|
|
29
|
+
"parse_station_attributes",
|
|
30
|
+
"parse_time",
|
|
31
|
+
]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Asynchronous client for Malaysia DOE APIMS."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import asyncio
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import aiohttp
|
|
8
|
+
|
|
9
|
+
from .const import API_PARAMS, API_URL, REQUEST_TIMEOUT, USER_AGENT
|
|
10
|
+
from .exceptions import ApimsConnectionError, ApimsError
|
|
11
|
+
from .models import (
|
|
12
|
+
Station,
|
|
13
|
+
find_nearest_station,
|
|
14
|
+
parse_station_attributes,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ApimsClient:
|
|
19
|
+
"""Client for fetching data from Malaysia DOE APIMS portal."""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
session: aiohttp.ClientSession | None = None,
|
|
24
|
+
timeout: int = REQUEST_TIMEOUT,
|
|
25
|
+
user_agent: str = USER_AGENT,
|
|
26
|
+
) -> None:
|
|
27
|
+
"""Initialize ApimsClient."""
|
|
28
|
+
self._session = session
|
|
29
|
+
self._timeout = timeout
|
|
30
|
+
self._user_agent = user_agent
|
|
31
|
+
|
|
32
|
+
async def _get_session(self) -> aiohttp.ClientSession:
|
|
33
|
+
if self._session is None or self._session.closed:
|
|
34
|
+
self._session = aiohttp.ClientSession()
|
|
35
|
+
return self._session
|
|
36
|
+
|
|
37
|
+
async def async_get_stations(self) -> dict[str, Station]:
|
|
38
|
+
"""Fetch all stations with their latest readings in a single request."""
|
|
39
|
+
session = await self._get_session()
|
|
40
|
+
try:
|
|
41
|
+
async with asyncio.timeout(self._timeout):
|
|
42
|
+
resp = await session.get(
|
|
43
|
+
API_URL,
|
|
44
|
+
params=API_PARAMS,
|
|
45
|
+
headers={"User-Agent": self._user_agent},
|
|
46
|
+
)
|
|
47
|
+
resp.raise_for_status()
|
|
48
|
+
payload = await resp.json(content_type=None)
|
|
49
|
+
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
|
50
|
+
raise ApimsConnectionError(f"APIMS request failed: {err}") from err
|
|
51
|
+
except ValueError as err:
|
|
52
|
+
raise ApimsError(f"APIMS returned invalid JSON: {err}") from err
|
|
53
|
+
|
|
54
|
+
if not isinstance(payload, dict):
|
|
55
|
+
raise ApimsError("APIMS returned unexpected non-dictionary payload")
|
|
56
|
+
if "error" in payload:
|
|
57
|
+
raise ApimsError(f"APIMS returned error: {payload['error']}")
|
|
58
|
+
|
|
59
|
+
stations: dict[str, Station] = {}
|
|
60
|
+
for feat in payload.get("features", []):
|
|
61
|
+
st = parse_station_attributes(feat.get("attributes") or {})
|
|
62
|
+
if st and st.station_id:
|
|
63
|
+
stations[st.station_id] = st
|
|
64
|
+
|
|
65
|
+
if not stations:
|
|
66
|
+
raise ApimsError("APIMS returned 0 monitoring stations")
|
|
67
|
+
return stations
|
|
68
|
+
|
|
69
|
+
async def async_get_nearest_station(
|
|
70
|
+
self, latitude: float, longitude: float
|
|
71
|
+
) -> tuple[Station, float] | tuple[None, None]:
|
|
72
|
+
"""Fetch all stations and return the nearest one and its distance in km."""
|
|
73
|
+
stations = await self.async_get_stations()
|
|
74
|
+
st = find_nearest_station(stations, latitude, longitude)
|
|
75
|
+
if not st:
|
|
76
|
+
return None, None
|
|
77
|
+
from .models import distance_km
|
|
78
|
+
return st, round(distance_km(latitude, longitude, st.latitude, st.longitude), 2)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Constants for APIMS client."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from datetime import timedelta, timezone
|
|
5
|
+
from typing import Final
|
|
6
|
+
|
|
7
|
+
MYT: Final = timezone(timedelta(hours=8))
|
|
8
|
+
API_URL: Final = (
|
|
9
|
+
"https://eqms.doe.gov.my/api3/publicmapproxy/PUBLIC_DISPLAY/"
|
|
10
|
+
"0f898ad95c524e4d924083f21b764b88/MapServer/0/query"
|
|
11
|
+
)
|
|
12
|
+
API_PARAMS: Final = {
|
|
13
|
+
"f": "json",
|
|
14
|
+
"where": "1=1",
|
|
15
|
+
"outFields": (
|
|
16
|
+
"STATION_ID,STATION_LOCATION,PLACE,STATE_NAME,REGION_NAME,"
|
|
17
|
+
"STATION_CATEGORY,LATITUDE,LONGITUDE,API,PARAM_SELECTED,CLASS,DATETIME,"
|
|
18
|
+
"PM25_CONC,PM10_CONC"
|
|
19
|
+
),
|
|
20
|
+
"returnGeometry": "false",
|
|
21
|
+
}
|
|
22
|
+
USER_AGENT: Final = "python-apims/0.1.0 (+https://github.com/zubirjamal/python-apims)"
|
|
23
|
+
REQUEST_TIMEOUT: Final = 30
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Exceptions for APIMS client."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ApimsError(Exception):
|
|
6
|
+
"""Raised when APIMS data cannot be read or parsed."""
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ApimsConnectionError(ApimsError):
|
|
10
|
+
"""Raised on network timeout or connection failure."""
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Data models and spatial utilities for APIMS."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
from math import asin, cos, radians, sin, sqrt
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from .const import MYT
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class Station:
|
|
14
|
+
"""An air quality monitoring station in APIMS."""
|
|
15
|
+
|
|
16
|
+
station_id: str
|
|
17
|
+
name: str
|
|
18
|
+
place: str | None
|
|
19
|
+
state: str | None
|
|
20
|
+
region: str | None
|
|
21
|
+
category: str | None # Urban / Sub Urban / Rural / Industry / Mobile
|
|
22
|
+
latitude: float
|
|
23
|
+
longitude: float
|
|
24
|
+
api: float | None
|
|
25
|
+
pollutant: str | None
|
|
26
|
+
api_class: str | None
|
|
27
|
+
updated: datetime | None
|
|
28
|
+
pm25: float | None = None # µg/m³, current hourly concentration
|
|
29
|
+
pm10: float | None = None
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def mobile(self) -> bool:
|
|
33
|
+
"""True if this is a mobile continuous air quality monitoring station."""
|
|
34
|
+
return self.station_id.startswith("MCAQM")
|
|
35
|
+
|
|
36
|
+
def to_dict(self) -> dict[str, Any]:
|
|
37
|
+
"""Convert to dictionary representation."""
|
|
38
|
+
return {
|
|
39
|
+
"station_id": self.station_id,
|
|
40
|
+
"name": self.name,
|
|
41
|
+
"place": self.place,
|
|
42
|
+
"state": self.state,
|
|
43
|
+
"region": self.region,
|
|
44
|
+
"category": self.category,
|
|
45
|
+
"latitude": self.latitude,
|
|
46
|
+
"longitude": self.longitude,
|
|
47
|
+
"api": self.api,
|
|
48
|
+
"pollutant": self.pollutant,
|
|
49
|
+
"api_class": self.api_class,
|
|
50
|
+
"updated": self.updated.isoformat() if self.updated else None,
|
|
51
|
+
"pm25": self.pm25,
|
|
52
|
+
"pm10": self.pm10,
|
|
53
|
+
"mobile": self.mobile,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def parse_time(ms: int | None) -> datetime | None:
|
|
58
|
+
"""DATETIME is MYT wall-clock stored as a UTC epoch: reinterpret it."""
|
|
59
|
+
if ms is None:
|
|
60
|
+
return None
|
|
61
|
+
naive = datetime.fromtimestamp(ms / 1000, timezone.utc).replace(tzinfo=None)
|
|
62
|
+
return naive.replace(tzinfo=MYT)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def parse_concentration(value: Any) -> float | None:
|
|
66
|
+
"""Concentration in µg/m³; APIMS uses negatives/None for no data."""
|
|
67
|
+
try:
|
|
68
|
+
v = float(value)
|
|
69
|
+
except (TypeError, ValueError):
|
|
70
|
+
return None
|
|
71
|
+
return round(v, 2) if v >= 0 else None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def parse_station_attributes(attrs: dict[str, Any]) -> Station | None:
|
|
75
|
+
"""Parse raw feature attributes into a Station object."""
|
|
76
|
+
try:
|
|
77
|
+
lat = float(attrs["LATITUDE"])
|
|
78
|
+
lon = float(attrs["LONGITUDE"])
|
|
79
|
+
except (KeyError, TypeError, ValueError):
|
|
80
|
+
return None
|
|
81
|
+
api_val = attrs.get("API")
|
|
82
|
+
api: float | None = None
|
|
83
|
+
if api_val is not None:
|
|
84
|
+
try:
|
|
85
|
+
f = float(api_val)
|
|
86
|
+
if f >= 0:
|
|
87
|
+
api = f
|
|
88
|
+
except (ValueError, TypeError):
|
|
89
|
+
pass
|
|
90
|
+
|
|
91
|
+
return Station(
|
|
92
|
+
station_id=str(attrs.get("STATION_ID", "")).strip(),
|
|
93
|
+
name=(attrs.get("STATION_LOCATION") or "").strip(),
|
|
94
|
+
place=(attrs.get("PLACE") or "").replace("\u00a0", " ").strip() or None,
|
|
95
|
+
state=attrs.get("STATE_NAME"),
|
|
96
|
+
region=attrs.get("REGION_NAME"),
|
|
97
|
+
category=(attrs.get("STATION_CATEGORY") or "").strip() or None,
|
|
98
|
+
latitude=lat,
|
|
99
|
+
longitude=lon,
|
|
100
|
+
api=api,
|
|
101
|
+
pollutant=attrs.get("PARAM_SELECTED"),
|
|
102
|
+
api_class=attrs.get("CLASS"),
|
|
103
|
+
updated=parse_time(attrs.get("DATETIME")),
|
|
104
|
+
pm25=parse_concentration(attrs.get("PM25_CONC")),
|
|
105
|
+
pm10=parse_concentration(attrs.get("PM10_CONC")),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def distance_km(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
|
110
|
+
"""Great-circle distance in kilometers."""
|
|
111
|
+
lat1, lon1, lat2, lon2 = map(radians, (lat1, lon1, lat2, lon2))
|
|
112
|
+
a = sin((lat2 - lat1) / 2) ** 2 + cos(lat1) * cos(lat2) * sin((lon2 - lon1) / 2) ** 2
|
|
113
|
+
return 6371.0 * 2 * asin(sqrt(a))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def find_nearest_station(
|
|
117
|
+
stations: dict[str, Station] | list[Station], lat: float, lon: float
|
|
118
|
+
) -> Station | None:
|
|
119
|
+
"""Find the nearest station to the given coordinates."""
|
|
120
|
+
station_list = list(stations.values()) if isinstance(stations, dict) else stations
|
|
121
|
+
if not station_list:
|
|
122
|
+
return None
|
|
123
|
+
return min(
|
|
124
|
+
station_list,
|
|
125
|
+
key=lambda s: distance_km(lat, lon, s.latitude, s.longitude),
|
|
126
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-apims
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Asynchronous Python client for Malaysia DOE APIMS (Air Pollutant Index Management System)
|
|
5
|
+
Author: Zubir Jamal
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/zubirjamal/python-apims
|
|
8
|
+
Project-URL: Repository, https://github.com/zubirjamal/python-apims
|
|
9
|
+
Project-URL: Issues, https://github.com/zubirjamal/python-apims/issues
|
|
10
|
+
Keywords: apims,air-quality,ipu,api,malaysia,home-assistant
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Topic :: Home Automation
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
Requires-Dist: aiohttp>=3.9.0
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Requires-Dist: pytest>=8.0.0; extra == "test"
|
|
25
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == "test"
|
|
26
|
+
Requires-Dist: aresponses>=3.0.0; extra == "test"
|
|
27
|
+
|
|
28
|
+
# python-apims
|
|
29
|
+
|
|
30
|
+
Asynchronous Python client for Malaysia Department of Environment (DOE) APIMS (Air Pollutant Index Management System).
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install python-apims
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
- Fetches real-time Air Pollutant Index (API / IPU) from all 68 nationwide monitoring stations in Malaysia.
|
|
40
|
+
- Provides PM2.5 and PM10 concentrations, station locations, categories, and timestamps.
|
|
41
|
+
- Spatial helpers for locating nearest air quality monitoring stations.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/apims/__init__.py
|
|
4
|
+
src/apims/client.py
|
|
5
|
+
src/apims/const.py
|
|
6
|
+
src/apims/exceptions.py
|
|
7
|
+
src/apims/models.py
|
|
8
|
+
src/python_apims.egg-info/PKG-INFO
|
|
9
|
+
src/python_apims.egg-info/SOURCES.txt
|
|
10
|
+
src/python_apims.egg-info/dependency_links.txt
|
|
11
|
+
src/python_apims.egg-info/requires.txt
|
|
12
|
+
src/python_apims.egg-info/top_level.txt
|
|
13
|
+
tests/test_apims.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
apims
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Tests for python-apims."""
|
|
2
|
+
import pytest
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from apims import (
|
|
5
|
+
Station,
|
|
6
|
+
parse_time,
|
|
7
|
+
parse_concentration,
|
|
8
|
+
parse_station_attributes,
|
|
9
|
+
distance_km,
|
|
10
|
+
find_nearest_station,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
def test_parse_concentration():
|
|
14
|
+
assert parse_concentration("23.45") == 23.45
|
|
15
|
+
assert parse_concentration(-1) is None
|
|
16
|
+
assert parse_concentration(None) is None
|
|
17
|
+
assert parse_concentration("invalid") is None
|
|
18
|
+
|
|
19
|
+
def test_parse_station_attributes():
|
|
20
|
+
raw = {
|
|
21
|
+
"STATION_ID": "CA001",
|
|
22
|
+
"STATION_LOCATION": "Cheras, Kuala Lumpur",
|
|
23
|
+
"PLACE": "Cheras",
|
|
24
|
+
"STATE_NAME": "W.P. Kuala Lumpur",
|
|
25
|
+
"REGION_NAME": "Klang Valley",
|
|
26
|
+
"STATION_CATEGORY": "Urban",
|
|
27
|
+
"LATITUDE": "3.106",
|
|
28
|
+
"LONGITUDE": "101.728",
|
|
29
|
+
"API": "55",
|
|
30
|
+
"PARAM_SELECTED": "PM2.5",
|
|
31
|
+
"CLASS": "Moderate",
|
|
32
|
+
"DATETIME": 1700000000000,
|
|
33
|
+
"PM25_CONC": "14.2",
|
|
34
|
+
"PM10_CONC": "25.0",
|
|
35
|
+
}
|
|
36
|
+
st = parse_station_attributes(raw)
|
|
37
|
+
assert st is not None
|
|
38
|
+
assert st.station_id == "CA001"
|
|
39
|
+
assert st.name == "Cheras, Kuala Lumpur"
|
|
40
|
+
assert st.api == 55.0
|
|
41
|
+
assert st.pollutant == "PM2.5"
|
|
42
|
+
assert st.pm25 == 14.2
|
|
43
|
+
assert not st.mobile
|
|
44
|
+
|
|
45
|
+
def test_mobile_station():
|
|
46
|
+
raw = {
|
|
47
|
+
"STATION_ID": "MCAQM01",
|
|
48
|
+
"STATION_LOCATION": "Mobile Unit 1",
|
|
49
|
+
"LATITUDE": "3.0",
|
|
50
|
+
"LONGITUDE": "101.5",
|
|
51
|
+
}
|
|
52
|
+
st = parse_station_attributes(raw)
|
|
53
|
+
assert st is not None
|
|
54
|
+
assert st.mobile
|
|
55
|
+
|
|
56
|
+
def test_find_nearest_station():
|
|
57
|
+
st1 = Station("S1", "Kajang", None, None, None, None, 2.99, 101.79, 50, None, None, None)
|
|
58
|
+
st2 = Station("S2", "Shah Alam", None, None, None, None, 3.07, 101.51, 60, None, None, None)
|
|
59
|
+
stations = {"S1": st1, "S2": st2}
|
|
60
|
+
nearest = find_nearest_station(stations, 3.00, 101.78)
|
|
61
|
+
assert nearest.station_id == "S1"
|