Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Aggregation 2
  • Loading branch information
Vimarsh Patel committed Jul 25, 2021
commit b0deb2403735fffe1b00fde1d4486fccb3e99d05
105 changes: 57 additions & 48 deletions app/utils/populations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,60 @@
GEONAMES_URL = "http://api.geonames.org/countryInfoJSON"
GEONAMES_BACKUP_PATH = "geonames_population_mappings.json"

# Fetching of the populations.
def fetch_populations(save=False):
"""
Returns a dictionary containing the population of each country fetched from the GeoNames.
https://www.geonames.org/

TODO: only skip writing to the filesystem when deployed with gunicorn, or handle concurent access, or use DB.

:returns: The mapping of populations.
:rtype: dict
"""
LOGGER.info("Fetching populations...")

# Mapping of populations
mappings = {}

# Fetch the countries.
try:
countries = requests.get(GEONAMES_URL, params={"username": "dperic"}, timeout=1.25).json()[
"geonames"
]
# Go through all the countries and perform the mapping.
for country in countries:
mappings.update({country["countryCode"]: int(country["population"]) or None})

if mappings and save:
LOGGER.info(f"Saving population data to {app.io.save(GEONAMES_BACKUP_PATH, mappings)}")
except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err:
LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}")
mappings = app.io.load(GEONAMES_BACKUP_PATH)
LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}")
# Finally, return the mappings.
LOGGER.info("Fetched populations")
return mappings


# Mapping of alpha-2 codes country codes to population.
POPULATIONS = fetch_populations()

# Retrieving.
def country_population(country_code, default=None):
"""
Fetches the population of the country with the provided country code.

:returns: The population.
:rtype: int
"""
return POPULATIONS.get(country_code, default)

class POPULATION:
# Fetching of the populations.
POPULATION = fetch_population()

def __init__(self):
self.population = POPULATION

def fetch_populations(save=False):
"""
Returns a dictionary containing the population of each country fetched from the GeoNames.
https://www.geonames.org/

TODO: only skip writing to the filesystem when deployed with gunicorn, or handle concurent access, or use DB.

:returns: The mapping of populations.
:rtype: dict
"""
LOGGER.info("Fetching populations...")

# Mapping of populations
mappings = {}

# Fetch the countries.
try:
countries = requests.get(GEONAMES_URL, params={"username": "dperic"}, timeout=1.25).json()[
"geonames"
]
# Go through all the countries and perform the mapping.
for country in countries:
mappings.update({country["countryCode"]: int(country["population"]) or None})

if mappings and save:
LOGGER.info(f"Saving population data to {app.io.save(GEONAMES_BACKUP_PATH, mappings)}")
except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err:
LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}")
mappings = app.io.load(GEONAMES_BACKUP_PATH)
LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}")
# Finally, return the mappings.
LOGGER.info("Fetched populations")
return mappings


# Mapping of alpha-2 codes country codes to population.
POPULATIONS = fetch_populations()

# Retrieving.
def get_country_population(country_code, default=None):
"""
Fetches the population of the country with the provided country code.

:returns: The population.
:rtype: int
"""
return self.population.get(country_code, default)