Created
October 8, 2024 13:49
-
-
Save avdata99/0b861889637686c223986dc9fed4d14e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
File to push NDX countries list into Kobo but not including pcodes (yet) | |
""" | |
import csv | |
import json | |
import requests | |
from io import StringIO | |
def get_ndx_countries(ndx_countries_dataset_url, ndx_api_token): | |
""" Get NDX countries data | |
Returns a dictionary with ISO3 as key and country name as value | |
""" | |
headers = {'Authorization': ndx_api_token} | |
response = requests.request("GET", ndx_countries_dataset_url, headers=headers) | |
""" data sample: | |
Countries and Territories,Official Name,ISO3,ISO2,M49 | |
Aruba,Aruba,ABW,AW,533 | |
Afghanistan,The Islamic Republic of Afghanistan,AFG,AF,4 | |
""" | |
# Transform this to JSON replacing field names from | |
# "Countries and Territories" to "country" and "ISO3" to "code" | |
csv_data = StringIO(response.text) | |
data = csv.DictReader(csv_data) | |
countries = {} | |
for row in data: | |
countries[row['ISO3']] = row['Countries and Territories'] | |
return countries | |
def get_kobo_collection(kobo_base_url, kobo_api_token, kobo_collection_id): | |
""" Get Kobo collection data """ | |
kobo_collection_url = f'{kobo_base_url}/api/v2/assets/{kobo_collection_id}/' | |
headers = {'Authorization': f'Token {kobo_api_token}'} | |
response = requests.request("GET", kobo_collection_url, headers=headers) | |
return response.json() | |
def main(): | |
config = get_config() | |
ndx_countries_dataset_url = config['ndx_countries_dataset_url'] | |
ndx_api_token = config['ndx_api_token'] | |
print(f'Getting NDX countries from {ndx_countries_dataset_url} ...') | |
country_code_map = get_ndx_countries(ndx_countries_dataset_url, ndx_api_token) | |
total_countries = len(country_code_map) | |
print(f'{total_countries} countries found:') | |
for code, country in country_code_map.items(): | |
print(f' - {code}: {country}') | |
""" | |
Getting NDX countries from XXX ... | |
246 countries found: | |
- ABW: Aruba | |
- AFG: Afghanistan | |
- AGO: Angola | |
... | |
""" | |
# Analyze currect collection in Kobo | |
kobo_collection = get_kobo_collection( | |
config['kobo_base_url'], | |
config['kobo_api_token'], | |
config['kobo_collection_id'], | |
) | |
print(f'Current Kobo collection: {kobo_collection}') | |
""" | |
Sample | |
""" | |
def get_config(): | |
""" | |
Get config from config.json file | |
To be filled with the following structure: | |
{ | |
"ndx_countries_dataset_url": "", | |
"ndx_api_token": "", | |
"kobo_base_url": "", | |
"kobo_api_token": "", | |
"kobo_collection_id": "" | |
} | |
""" | |
config = json.load(open('config.json')) | |
return config | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment