diff options
author | ache <ache@ache.one> | 2023-12-30 14:29:50 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2023-12-30 14:29:50 +0100 |
commit | 934b691bd8b6eb752832a425c915a61a16b13253 (patch) | |
tree | 60ca2588f85894078f4933178ba59a50eb5cf59b | |
parent | Tournures de phrases (diff) |
Sort every key of the dictionnary to get reproductible results
-rw-r--r-- | get_country.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/get_country.py b/get_country.py index ee31970..c43a4d5 100644 --- a/get_country.py +++ b/get_country.py @@ -22,7 +22,7 @@ REQUEST_HEADERS = { REQUEST_DATA = { "searchTerm": "*", "searchType": 0, - "searchLanguages": ["ar", "en", "fr", "ru", "zh"], + "searchLanguages": ["ar", "es", "en", "fr", "ru", "zh"], "languagesDisplay": ["en", "fr"], "datasets": ["UNHQ"], "bodies": [], @@ -39,11 +39,11 @@ REQUEST_DATA = { } languagesIDToLanguage = { - 'ca7853cf-0f94-4aa6-be0a-ea586361c19e': 'french', - '496bcad8-7cd0-4f46-85c7-23b91e7a4ad2': 'russian', - '77ebac02-6a97-4722-859a-de5896a4d34e': 'english', '5a504ded-6ba9-49c9-98aa-c8418cbb1ce9': 'arabic', '676ccfad-a7b3-4e36-8d49-b21a9b5e0d7d': 'chinese', + '77ebac02-6a97-4722-859a-de5896a4d34e': 'english', + 'ca7853cf-0f94-4aa6-be0a-ea586361c19e': 'french', + '496bcad8-7cd0-4f46-85c7-23b91e7a4ad2': 'russian', 'af320177-7bdb-40df-9521-6d2e0478f129': 'spanish', } @@ -57,6 +57,9 @@ def main(): infoLang = getInfoCountry(records) countries[infoLang['iso_a2']] = infoLang + # Sort the dictionnary to get the same result every times. + countries = {i2: countries[i2] for i2 in sorted(list(countries.keys()))} + with open(OUTPUT_FILE_NAME, 'w') as f: json.dump(countries, f) @@ -107,8 +110,8 @@ def getRecordsID(): def getInfoCountry(recordID): """ - This function extract the information of a recordID. - Each record should match a country so this function extract the names of a country in the 6 offical UN languages. + This function extract the information of a recordID. + Each record should match a country so this function extract the names of a country in the 6 offical UN languages. """ def removeExtraInfo(countryName): if '(' in countryName: @@ -124,24 +127,25 @@ def getInfoCountry(recordID): data = res.json() countryInfo = {} - for lang in data['languages']: + for lang in sorted(data['languages']): lang = lang.lower() - countryInfo[lang] = {} + countryInfo[lang] = {'short': '', 'full': ''} + for i in range(len(data[lang]['terms'])): countryInfo[lang][data[lang]['terms'][i]['termType']] = data[lang]['terms'][i]['term'] + + # The UK doesn't have official short name in russian. ¯\_(ツ)_/¯ + if not countryInfo[lang]['short'] and countryInfo[lang]['full']: + countryInfo[lang]['short'] = countryInfo[lang]['full'] + try: countryInfo[lang]['map_usable'] = removeExtraInfo(countryInfo[lang]['short']) except KeyError: - if 'full' in countryInfo[lang]: - # The Russian federation doesn't seems to have a official short translation in some language - countryInfo[lang]['short'] = countryInfo[lang]['full'] - countryInfo[lang]['map_usable'] = removeExtraInfo(countryInfo[lang]['short']) - else: - print(f"What the fuck {lang}?!") - print(f"Missing name for this country in {lang}", countryInfo) + print(f"What the fuck {lang}?!") + print(f"Missing name for this country in {lang}", countryInfo) - for field in data['specialFields']: + for field in sorted(data['specialFields'], key=lambda item: item['name']): match field['name']: case "ISO Country alpha-2-code": countryInfo['iso_a2'] = field['value'] @@ -178,6 +182,7 @@ def getInfoCountry(recordID): if field['languageId'] in languagesIDToLanguage: lang = languagesIDToLanguage[field['languageId']] countryInfo[lang]['adjective'] = field['value'] + case 'Date of Entry in UN': countryInfo['un_entry_date'] = field['value'] |