| import os |
| import requests |
|
|
| def download_resource_ckan(iso3_code, dataset_prefix="cod-ab", download_dir="downloads"): |
| iso3 = iso3_code.lower() |
| dataset_id = f"{dataset_prefix}-{iso3}" |
| api_url = f"https://data.humdata.org/api/3/action/package_show?id={dataset_id}" |
| headers = {'User-Agent': 'Mozilla/5.0'} |
|
|
| |
| if dataset_prefix == "cod-ps": |
| keyword_match = lambda name: name.startswith(f"{iso3}_admpop_adm") and name.endswith(".csv") |
| else: |
| keyword_match = lambda name: "_SHP.zip" in name |
|
|
| try: |
| response = requests.get(api_url, headers=headers) |
| if response.status_code != 200: |
| print(f"[{iso3.upper()}] Dataset not found (HTTP {response.status_code}): {dataset_id}") |
| return [] |
|
|
| data = response.json() |
| if not data.get("success") or "result" not in data: |
| print(f"[{iso3.upper()}] API returned malformed response.") |
| return [] |
|
|
| |
| os.makedirs(download_dir, exist_ok=True) |
| downloaded_files = [] |
|
|
| for resource in data["result"].get("resources", []): |
| name = resource.get("name", "") |
| if keyword_match(name): |
| url = resource.get("url") |
| if not url: |
| continue |
|
|
| filepath = os.path.join(download_dir, name) |
| try: |
| with requests.get(url, stream=True) as r: |
| r.raise_for_status() |
| with open(filepath, 'wb') as f: |
| for chunk in r.iter_content(chunk_size=8192): |
| f.write(chunk) |
| print(f"[{iso3.upper()}] Downloaded: {filepath}") |
| downloaded_files.append(filepath) |
| except Exception as e: |
| print(f"[{iso3.upper()}] Failed to download {name}: {e}") |
|
|
| if not downloaded_files: |
| print(f"[{iso3.upper()}] No matching files found in dataset '{dataset_id}'.") |
|
|
| return downloaded_files |
|
|
| except Exception as e: |
| print(f"[{iso3.upper()}] Error: {e}") |
| return [] |