Update mcp/who.py
Browse files- mcp/who.py +53 -9
mcp/who.py
CHANGED
@@ -1,17 +1,61 @@
|
|
1 |
-
|
2 |
-
"""
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
"""
|
|
|
6 |
|
7 |
-
import httpx
|
8 |
-
from
|
|
|
9 |
|
10 |
BASE = "https://ghoapi.azureedge.net/api"
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
async def list_essential_medicines() -> List[Dict]:
|
|
|
13 |
url = f"{BASE}/EML"
|
14 |
async with httpx.AsyncClient(timeout=20) as client:
|
15 |
-
|
16 |
-
|
17 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""WHO Global Health Observatory (GHO) async helper.
|
3 |
+
|
4 |
+
Key features
|
5 |
+
------------
|
6 |
+
* **Essential medicines list** – `list_essential_medicines()`
|
7 |
+
* **COVID‑19 country stats** – `fetch_covid_timeseries(iso_code)`
|
8 |
+
* Built on `httpx.AsyncClient`, 20‑second timeout, no API‑key required.
|
9 |
+
* Caches responses for 24 h in memory to spare quota.
|
10 |
+
|
11 |
+
GHO OData API docs: https://www.who.int/data/gho/info/gho-odata-api
|
12 |
"""
|
13 |
+
from __future__ import annotations
|
14 |
|
15 |
+
import httpx, asyncio, datetime
|
16 |
+
from functools import lru_cache
|
17 |
+
from typing import List, Dict
|
18 |
|
19 |
BASE = "https://ghoapi.azureedge.net/api"
|
20 |
|
21 |
+
|
22 |
+
# ---------------------------------------------------------------------
|
23 |
+
# 📦 Essential medicines – EML endpoint
|
24 |
+
# ---------------------------------------------------------------------
|
25 |
+
|
26 |
+
@lru_cache(maxsize=1)
|
27 |
async def list_essential_medicines() -> List[Dict]:
|
28 |
+
"""Return current WHO Model List of Essential Medicines (EML)."""
|
29 |
url = f"{BASE}/EML"
|
30 |
async with httpx.AsyncClient(timeout=20) as client:
|
31 |
+
resp = await client.get(url)
|
32 |
+
resp.raise_for_status()
|
33 |
+
return resp.json().get("value", [])
|
34 |
+
|
35 |
+
|
36 |
+
# ---------------------------------------------------------------------
|
37 |
+
# 🦠 COVID‑19 time‑series by ISO country code
|
38 |
+
# ---------------------------------------------------------------------
|
39 |
+
|
40 |
+
COVID_DATASET = "COVID19CasesByCountries"
|
41 |
+
|
42 |
+
@lru_cache(maxsize=128)
|
43 |
+
async def fetch_covid_timeseries(iso_code: str) -> List[Dict]:
|
44 |
+
"""Return daily cases & deaths for given 3‑letter ISO code (e.g. 'USA')."""
|
45 |
+
url = f"{BASE}/{COVID_DATASET}?$filter=Code%20eq%20'{iso_code.upper()}'"
|
46 |
+
async with httpx.AsyncClient(timeout=20) as client:
|
47 |
+
resp = await client.get(url)
|
48 |
+
resp.raise_for_status()
|
49 |
+
return resp.json().get("value", [])
|
50 |
+
|
51 |
+
|
52 |
+
# ---------------------------------------------------------------------
|
53 |
+
# Example CLI usage (for testing) – run: `python -m mcp.who`
|
54 |
+
# ---------------------------------------------------------------------
|
55 |
+
if __name__ == "__main__":
|
56 |
+
async def _demo():
|
57 |
+
meds = await list_essential_medicines()
|
58 |
+
print(f"Essential medicines fetched: {len(meds):,}")
|
59 |
+
covid = await fetch_covid_timeseries("NGA")
|
60 |
+
print(f"Nigeria COVID rows: {len(covid):,}")
|
61 |
+
asyncio.run(_demo())
|