diff --git a/comicapi/utils.py b/comicapi/utils.py index 9cb579c..9b2c2f5 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -239,6 +239,7 @@ def get_language_from_iso(iso: str | None) -> str | None: def get_language_iso(string: str | None) -> str | None: if string is None: return None + # Why return this if ISO failed? lang = string.casefold() try: @@ -248,6 +249,39 @@ def get_language_iso(string: str | None) -> str | None: return lang +def get_country_from_iso(iso: str | None) -> str | None: + return countries[iso] + + +def get_country_iso(string: str | None) -> str | None: + if string is None: + return None + + try: + country = pycountry.countries.lookup(string) + if country: + # Assume first entry is correct + return country[0].alpha_2 + except LookupError: + pass + return None + + +def get_country_iso_name(string: str | None) -> str | None: + """Return country's ISO name from "other" name""" + if string is None: + return None + + try: + country = pycountry.countries.search_fuzzy(string) + if country: + # Assume first entry is correct + return country[0].name + except LookupError: + pass + return None + + def get_publisher(publisher: str) -> tuple[str, str]: imprint = "" diff --git a/comictalker/talker_utils.py b/comictalker/talker_utils.py index b5c3094..4767e1c 100644 --- a/comictalker/talker_utils.py +++ b/comictalker/talker_utils.py @@ -99,13 +99,25 @@ def map_comic_issue_to_metadata( if issue_results.critical_rating: metadata.critical_rating = utils.xlate_float(issue_results.critical_rating) + # 2-letter country code if issue_results.language: - # 2-letter code # TODO Run check against pycountry? - metadata.language = issue_results.language + if utils.get_language_from_iso(issue_results.language): + metadata.language = issue_results.language + else: + code = utils.get_language_iso(issue_results.language) + if code: + metadata.language = code + # ISO country name if issue_results.country: - # 2-letter code # TODO Run check against pycountry? - metadata.language = issue_results.country + # Only CBI supports country currently in CT and is a string name + # TODO Use https://codeberg.org/plotski/countryguess as pycountry seems stale? + # Return current entry if conversion fails + metadata.country = issue_results.country + if not utils.get_country_iso(issue_results.country): + code = utils.get_country_iso_name(issue_results.country) + if code: + metadata.country = code return metadata