Skip to content

Instantly share code, notes, and snippets.

@JonathanGarro
Last active January 17, 2024 19:31
Show Gist options
  • Save JonathanGarro/039963f87b0c99616ee3a3c89dfda149 to your computer and use it in GitHub Desktop.
Save JonathanGarro/039963f87b0c99616ee3a3c89dfda149 to your computer and use it in GitHub Desktop.
Save IFRC GO Country List to CSV
import math
import csv
# script requires requests library
# prompt user in console to auto pip install it
try:
import requests
except ImportError as e:
response = input("The required module 'requests' is not installed. Would you like to install it now? (yes/no): ")
if response.lower() in ['yes', 'y']:
import sys
import subprocess
# install the module using pip
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
# try importing the module again after installation
import requests
else:
print("The module 'requests' is required to run this script.")
sys.exit(1)
url = 'https://goadmin.ifrc.org/api/v2/country/'
r = requests.get(url).json()
current_page = 1
page_count = int(math.ceil(r['count'] / 50))
print(f"THE PAGE COUNT TOTAL IS: {page_count}")
output = []
while current_page <= page_count:
print(f'Getting data from: {current_page}')
for result in r['results']:
temp_dict = {}
temp_dict['country_name'] = result['name']
temp_dict['iso2'] = result['iso']
temp_dict['iso3'] = result['iso3']
temp_dict['go_id'] = result['id']
temp_dict['society_name'] = result['society_name']
temp_dict['society_url'] = result['society_url']
temp_dict['fdrs_url'] = result['url_ifrc']
output.append(temp_dict)
if r['next']:
next_page = requests.get(r['next']).json()
r = next_page
current_page += 1
else:
break
keys = output[0].keys()
a_file = open("output.csv", "w")
dict_writer = csv.DictWriter(a_file, keys)
dict_writer.writeheader()
dict_writer.writerows(output)
a_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment