Add country functions to utils and try to convert a country name to ISO country name

This commit is contained in:
Mizaki 2023-05-23 00:02:56 +01:00
parent 97e64fa918
commit 8e2411a086
2 changed files with 50 additions and 4 deletions

View File

@ -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 = ""

View File

@ -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