File size: 2,183 Bytes
a6ec964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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'}

    # Match logic
    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 []

        # Find and download all matching resources
        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 []