forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulations.py
More file actions
44 lines (32 loc) · 1.14 KB
/
populations.py
File metadata and controls
44 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""app.utils.populations.py"""
import logging
import requests
LOGGER = logging.getLogger(__name__)
# Fetching of the populations.
def fetch_populations():
"""
Returns a dictionary containing the population of each country fetched from the GeoNames.
https://www.geonames.org/
:returns: The mapping of populations.
:rtype: dict
"""
LOGGER.info("Fetching populations...")
# Mapping of populations
mappings = {}
# Fetch the countries.
countries = requests.get("http://api.geonames.org/countryInfoJSON?username=dperic").json()["geonames"]
# Go through all the countries and perform the mapping.
for country in countries:
mappings.update({country["countryCode"]: int(country["population"]) or None})
# Finally, return the mappings.
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)