From 916b628bb0d5195c41ff9c6ca2e462d3cd9b747a Mon Sep 17 00:00:00 2001 From: pamfilos Date: Fri, 23 Aug 2024 13:23:47 +0200 Subject: [PATCH] common: updates pycountry to country_converter package Signed-off-by: pamfilos --- dags/common/utils.py | 13 ++++++++++--- requirements.txt | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dags/common/utils.py b/dags/common/utils.py index 2399165c..76f36c2c 100644 --- a/dags/common/utils.py +++ b/dags/common/utils.py @@ -12,7 +12,7 @@ from inspire_utils.record import get_value import backoff -import pycountry +import country_converter as coco import requests from airflow.models.dagrun import DagRun from airflow.utils.state import DagRunState @@ -33,7 +33,7 @@ from structlog import get_logger logger = get_logger() - +cc = coco.CountryConverter() def set_harvesting_interval(repo, **kwargs): if ( @@ -308,7 +308,14 @@ def parse_country_from_value(affiliation_value): return val try: - mapped_countries = pycountry.countries.search_fuzzy(country) + country_code = cc.convert(country, to="iso2") + mapped_countries = [] + if country_code != "not found": + mapped_countries = [{ + "code": country_code, + "name": cc.convert(country, to="name_short"), + }] + if len(mapped_countries) > 1 or len(mapped_countries) == 0: raise FoundMoreThanOneMatchOrNone(affiliation_value) return mapped_countries[0].name diff --git a/requirements.txt b/requirements.txt index 3ee8faa6..82aaf8da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,5 +9,5 @@ busypie==0.4.5 pydantic jsonschema plyvel -pycountry +country_converter inspire-utils