Spaces:
Sleeping
Sleeping
Create public_data_ingest.py
Browse files- public_data_ingest.py +34 -0
public_data_ingest.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from public_data_sources import WORLD_BANK_BASE, INDICATORS
|
| 3 |
+
|
| 4 |
+
def fetch_world_bank_indicator(
|
| 5 |
+
country="WLD",
|
| 6 |
+
indicator="NY.GDP.MKTP.CD",
|
| 7 |
+
year="2022"
|
| 8 |
+
):
|
| 9 |
+
url = (
|
| 10 |
+
f"{WORLD_BANK_BASE}/country/{country}/indicator/"
|
| 11 |
+
f"{indicator}?format=json&per_page=1&date={year}"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get(url, timeout=10)
|
| 16 |
+
data = response.json()
|
| 17 |
+
value = data[1][0]["value"]
|
| 18 |
+
return value
|
| 19 |
+
except Exception:
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def fetch_macro_anchor():
|
| 24 |
+
return {
|
| 25 |
+
"global_gdp": fetch_world_bank_indicator(
|
| 26 |
+
indicator=INDICATORS["GDP"]
|
| 27 |
+
),
|
| 28 |
+
"global_inflation": fetch_world_bank_indicator(
|
| 29 |
+
indicator=INDICATORS["INFLATION"]
|
| 30 |
+
),
|
| 31 |
+
"population": fetch_world_bank_indicator(
|
| 32 |
+
indicator=INDICATORS["POPULATION"]
|
| 33 |
+
),
|
| 34 |
+
}
|