"""Geo-related utilities.""" from async_lru import alru_cache from .. import logger from ..connectors.scrapers import CSVScraper from ..typings import CountryCode2, CountryName logger = logger.new_logger(__name__) @alru_cache(maxsize=1) async def get_iso_3166_territories() -> dict[CountryCode2, CountryName]: """Get ISO 3166 territories. They are scraped from a CSV file hosted on GitHub, which belongs to one of the most popular repositories for ISO 3166 data. It cannot be scraped from the official ISO website due to the lack of a direct link to the CSV file and because their API is paid. This function is cached to avoid unnecessary requests to the GitHub repository and because the ISO 3166 data is not expected to change frequently. """ scraper = CSVScraper() # Use CSV and not JSON for performance reasons and lighter payload url = ( "https://raw.githubusercontent.com/lukes/" "ISO-3166-Countries-with-Regional-Codes/master/all/all.csv" ) logger.info("Fetching ISO 3166 territories from source...") csv_rows = await scraper.get(url) territories_dict = {row["alpha-2"]: row["name"] for row in csv_rows} return territories_dict