|
10 | 10 | GEONAMES_URL = "http://api.geonames.org/countryInfoJSON" |
11 | 11 | GEONAMES_BACKUP_PATH = "geonames_population_mappings.json" |
12 | 12 |
|
13 | | -# Fetching of the populations. |
14 | | -def fetch_populations(save=False): |
15 | | - """ |
16 | | - Returns a dictionary containing the population of each country fetched from the GeoNames. |
17 | | - https://www.geonames.org/ |
18 | | -
|
19 | | - TODO: only skip writing to the filesystem when deployed with gunicorn, or handle concurent access, or use DB. |
20 | | -
|
21 | | - :returns: The mapping of populations. |
22 | | - :rtype: dict |
23 | | - """ |
24 | | - LOGGER.info("Fetching populations...") |
25 | | - |
26 | | - # Mapping of populations |
27 | | - mappings = {} |
28 | | - |
29 | | - # Fetch the countries. |
30 | | - try: |
31 | | - countries = requests.get(GEONAMES_URL, params={"username": "dperic"}, timeout=1.25).json()[ |
32 | | - "geonames" |
33 | | - ] |
34 | | - # Go through all the countries and perform the mapping. |
35 | | - for country in countries: |
36 | | - mappings.update({country["countryCode"]: int(country["population"]) or None}) |
37 | | - |
38 | | - if mappings and save: |
39 | | - LOGGER.info(f"Saving population data to {app.io.save(GEONAMES_BACKUP_PATH, mappings)}") |
40 | | - except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err: |
41 | | - LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}") |
42 | | - mappings = app.io.load(GEONAMES_BACKUP_PATH) |
43 | | - LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}") |
44 | | - # Finally, return the mappings. |
45 | | - LOGGER.info("Fetched populations") |
46 | | - return mappings |
47 | | - |
48 | | - |
49 | | -# Mapping of alpha-2 codes country codes to population. |
50 | | -POPULATIONS = fetch_populations() |
51 | | - |
52 | | -# Retrieving. |
53 | | -def country_population(country_code, default=None): |
54 | | - """ |
55 | | - Fetches the population of the country with the provided country code. |
56 | | -
|
57 | | - :returns: The population. |
58 | | - :rtype: int |
59 | | - """ |
60 | | - return POPULATIONS.get(country_code, default) |
| 13 | + |
| 14 | +class POPULATION: |
| 15 | + # Fetching of the populations. |
| 16 | + POPULATION = fetch_population() |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + self.population = POPULATION |
| 20 | + |
| 21 | + def fetch_populations(save=False): |
| 22 | + """ |
| 23 | + Returns a dictionary containing the population of each country fetched from the GeoNames. |
| 24 | + https://www.geonames.org/ |
| 25 | + |
| 26 | + TODO: only skip writing to the filesystem when deployed with gunicorn, or handle concurent access, or use DB. |
| 27 | + |
| 28 | + :returns: The mapping of populations. |
| 29 | + :rtype: dict |
| 30 | + """ |
| 31 | + LOGGER.info("Fetching populations...") |
| 32 | + |
| 33 | + # Mapping of populations |
| 34 | + mappings = {} |
| 35 | + |
| 36 | + # Fetch the countries. |
| 37 | + try: |
| 38 | + countries = requests.get(GEONAMES_URL, params={"username": "dperic"}, timeout=1.25).json()[ |
| 39 | + "geonames" |
| 40 | + ] |
| 41 | + # Go through all the countries and perform the mapping. |
| 42 | + for country in countries: |
| 43 | + mappings.update({country["countryCode"]: int(country["population"]) or None}) |
| 44 | + |
| 45 | + if mappings and save: |
| 46 | + LOGGER.info(f"Saving population data to {app.io.save(GEONAMES_BACKUP_PATH, mappings)}") |
| 47 | + except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err: |
| 48 | + LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}") |
| 49 | + mappings = app.io.load(GEONAMES_BACKUP_PATH) |
| 50 | + LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}") |
| 51 | + # Finally, return the mappings. |
| 52 | + LOGGER.info("Fetched populations") |
| 53 | + return mappings |
| 54 | + |
| 55 | + |
| 56 | + # Mapping of alpha-2 codes country codes to population. |
| 57 | + POPULATIONS = fetch_populations() |
| 58 | + |
| 59 | + # Retrieving. |
| 60 | + def get_country_population(country_code, default=None): |
| 61 | + """ |
| 62 | + Fetches the population of the country with the provided country code. |
| 63 | + |
| 64 | + :returns: The population. |
| 65 | + :rtype: int |
| 66 | + """ |
| 67 | + return self.population.get(country_code, default) |
| 68 | + |
| 69 | + |
0 commit comments