Lobal API and Get The Result in Different Format (CSV, XLSX, and JSON)
Lobal API and Get The Result in Different Format (CSV, XLSX, and JSON)
requests
pandas
Usage
Configuration
1. API URL: The API URL is set to fetch Admin 0 level data by default. Change the URL in the script for
different administrative levels:
Admin 0: https://dtmapi.iom.int/api/idpAdmin0Data/GetAdmin0Datav2
Admin 1: https://dtmapi.iom.int/api/IdpAdmin1Data/GetAdmin1Datav2
Admin 2: https://dtmapi.iom.int/api/IdpAdmin2Data/GetAdmin2Datav2
2. Parameters: Adjust the parameters in the params dictionary to filter data by country code,
administrative level, reporting dates, and other optional fields.
Example params :
params = {
'CountryName': '',
'Admin0Pcode': 'AFG', # Example for Afghanistan
'Operation': '',
'FromReportingDate': '',
'ToReportingDate': '',
'Admin1Name': '',
'Admin1Pcode': '',
'Admin2Name': '',
'Admin2Pcode': '',
'FromRoundNumber': '',
'ToRoundNumber': ''
}
3. Output Format: Set the format_type variable to specify the output format ( 'json' , 'csv' , or 'xlsx' ).
4. Output Location: By default, files are saved to a folder named api_data on your desktop. Change the
output_dir variable to a different path if desired.
python script_name.py
https://github.com/Aschalew/GlobalIDPAPI/tree/main 2/4
11/27/24, 8:15 PM Aschalew/GlobalIDPAPI: This Python code allows you to access the Global API and get the result in different format(csv, xlsx, and …
Example
To fetch and save data as a CSV file:
params = {
'Admin0Pcode': 'AFG' # Specify the country or any other required parameters
}
format_type = 'csv'
Functions
fetch_data(api_url, params)
Description: Sends a GET request to the specified API with the given parameters.
Parameters:
api_url : API endpoint URL.
Error Handling
If a required parameter (such as CountryName , Admin0Pcode , or Operation ) is missing, the script will raise a
ValueError . Additionally, any issues during the API request or file saving process will be caught and printed.
Releases
No releases published
Create a new release
https://github.com/Aschalew/GlobalIDPAPI/tree/main 3/4