Skip to content

Commit f16df56

Browse files
committed
use io module to save and load poplutation data backups
1 parent 6995938 commit f16df56

File tree

2 files changed

+270
-6
lines changed

2 files changed

+270
-6
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
{
2+
"AD": 84000,
3+
"AE": 4975593,
4+
"AF": 29121286,
5+
"AG": 86754,
6+
"AI": 13254,
7+
"AL": 2986952,
8+
"AM": 2968000,
9+
"AO": 13068161,
10+
"AQ": null,
11+
"AR": 41343201,
12+
"AS": 57881,
13+
"AT": 8205000,
14+
"AU": 21515754,
15+
"AW": 71566,
16+
"AX": 26711,
17+
"AZ": 8303512,
18+
"BA": 4590000,
19+
"BB": 285653,
20+
"BD": 156118464,
21+
"BE": 10403000,
22+
"BF": 16241811,
23+
"BG": 7000039,
24+
"BH": 738004,
25+
"BI": 9863117,
26+
"BJ": 9056010,
27+
"BL": 8450,
28+
"BM": 65365,
29+
"BN": 395027,
30+
"BO": 9947418,
31+
"BQ": 18012,
32+
"BR": 201103330,
33+
"BS": 301790,
34+
"BT": 699847,
35+
"BV": null,
36+
"BW": 2029307,
37+
"BY": 9685000,
38+
"BZ": 314522,
39+
"CA": 33679000,
40+
"CC": 628,
41+
"CD": 70916439,
42+
"CF": 4844927,
43+
"CG": 3039126,
44+
"CH": 8484100,
45+
"CI": 21058798,
46+
"CK": 21388,
47+
"CL": 16746491,
48+
"CM": 19294149,
49+
"CN": 1330044000,
50+
"CO": 47790000,
51+
"CR": 4516220,
52+
"CU": 11423000,
53+
"CV": 508659,
54+
"CW": 141766,
55+
"CX": 1500,
56+
"CY": 1102677,
57+
"CZ": 10476000,
58+
"DE": 81802257,
59+
"DJ": 740528,
60+
"DK": 5484000,
61+
"DM": 72813,
62+
"DO": 9823821,
63+
"DZ": 34586184,
64+
"EC": 14790608,
65+
"EE": 1291170,
66+
"EG": 80471869,
67+
"EH": 273008,
68+
"ER": 5792984,
69+
"ES": 46505963,
70+
"ET": 88013491,
71+
"FI": 5244000,
72+
"FJ": 875983,
73+
"FK": 2638,
74+
"FM": 107708,
75+
"FO": 48228,
76+
"FR": 64768389,
77+
"GA": 1545255,
78+
"GB": 62348447,
79+
"GD": 107818,
80+
"GE": 4630000,
81+
"GF": 195506,
82+
"GG": 65228,
83+
"GH": 24339838,
84+
"GI": 27884,
85+
"GL": 56375,
86+
"GM": 1593256,
87+
"GN": 10324025,
88+
"GP": 443000,
89+
"GQ": 1014999,
90+
"GR": 11000000,
91+
"GS": 30,
92+
"GT": 13550440,
93+
"GU": 159358,
94+
"GW": 1565126,
95+
"GY": 748486,
96+
"HK": 6898686,
97+
"HM": null,
98+
"HN": 7989415,
99+
"HR": 4284889,
100+
"HT": 9648924,
101+
"HU": 9982000,
102+
"ID": 242968342,
103+
"IE": 4622917,
104+
"IL": 7353985,
105+
"IM": 75049,
106+
"IN": 1173108018,
107+
"IO": 4000,
108+
"IQ": 29671605,
109+
"IR": 76923300,
110+
"IS": 308910,
111+
"IT": 60340328,
112+
"JE": 90812,
113+
"JM": 2847232,
114+
"JO": 6407085,
115+
"JP": 127288000,
116+
"KE": 40046566,
117+
"KG": 5776500,
118+
"KH": 14453680,
119+
"KI": 92533,
120+
"KM": 773407,
121+
"KN": 51134,
122+
"KP": 22912177,
123+
"KR": 48422644,
124+
"KW": 2789132,
125+
"KY": 44270,
126+
"KZ": 15340000,
127+
"LA": 6368162,
128+
"LB": 4125247,
129+
"LC": 160922,
130+
"LI": 35000,
131+
"LK": 21513990,
132+
"LR": 3685076,
133+
"LS": 1919552,
134+
"LT": 2944459,
135+
"LU": 497538,
136+
"LV": 2217969,
137+
"LY": 6461454,
138+
"MA": 33848242,
139+
"MC": 32965,
140+
"MD": 4324000,
141+
"ME": 666730,
142+
"MF": 35925,
143+
"MG": 21281844,
144+
"MH": 65859,
145+
"MK": 2062294,
146+
"ML": 13796354,
147+
"MM": 53414374,
148+
"MN": 3086918,
149+
"MO": 449198,
150+
"MP": 53883,
151+
"MQ": 432900,
152+
"MR": 3205060,
153+
"MS": 9341,
154+
"MT": 403000,
155+
"MU": 1294104,
156+
"MV": 395650,
157+
"MW": 17563749,
158+
"MX": 112468855,
159+
"MY": 28274729,
160+
"MZ": 22061451,
161+
"NA": 2128471,
162+
"NC": 216494,
163+
"NE": 15878271,
164+
"NF": 1828,
165+
"NG": 154000000,
166+
"NI": 5995928,
167+
"NL": 16645000,
168+
"NO": 5009150,
169+
"NP": 28951852,
170+
"NR": 10065,
171+
"NU": 2166,
172+
"NZ": 4252277,
173+
"OM": 2967717,
174+
"PA": 3410676,
175+
"PE": 29907003,
176+
"PF": 270485,
177+
"PG": 6064515,
178+
"PH": 99900177,
179+
"PK": 184404791,
180+
"PL": 38500000,
181+
"PM": 7012,
182+
"PN": 46,
183+
"PR": 3916632,
184+
"PS": 3800000,
185+
"PT": 10676000,
186+
"PW": 19907,
187+
"PY": 6375830,
188+
"QA": 840926,
189+
"RE": 776948,
190+
"RO": 21959278,
191+
"RS": 7344847,
192+
"RU": 140702000,
193+
"RW": 11055976,
194+
"SA": 25731776,
195+
"SB": 559198,
196+
"SC": 88340,
197+
"SD": 35000000,
198+
"SE": 9828655,
199+
"SG": 4701069,
200+
"SH": 7460,
201+
"SI": 2007000,
202+
"SJ": 2550,
203+
"SK": 5455000,
204+
"SL": 5245695,
205+
"SM": 31477,
206+
"SN": 12323252,
207+
"SO": 10112453,
208+
"SR": 492829,
209+
"SS": 8260490,
210+
"ST": 197700,
211+
"SV": 6052064,
212+
"SX": 37429,
213+
"SY": 22198110,
214+
"SZ": 1354051,
215+
"TC": 20556,
216+
"TD": 10543464,
217+
"TF": 140,
218+
"TG": 6587239,
219+
"TH": 67089500,
220+
"TJ": 7487489,
221+
"TK": 1466,
222+
"TL": 1154625,
223+
"TM": 4940916,
224+
"TN": 10589025,
225+
"TO": 122580,
226+
"TR": 77804122,
227+
"TT": 1328019,
228+
"TV": 10472,
229+
"TW": 22894384,
230+
"TZ": 41892895,
231+
"UA": 45415596,
232+
"UG": 33398682,
233+
"UM": null,
234+
"US": 310232863,
235+
"UY": 3477000,
236+
"UZ": 27865738,
237+
"VA": 921,
238+
"VC": 104217,
239+
"VE": 27223228,
240+
"VG": 21730,
241+
"VI": 108708,
242+
"VN": 89571130,
243+
"VU": 221552,
244+
"WF": 16025,
245+
"WS": 192001,
246+
"XK": 1800000,
247+
"YE": 23495361,
248+
"YT": 159042,
249+
"ZA": 49000000,
250+
"ZM": 13460305,
251+
"ZW": 13061000
252+
}

app/utils/populations.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""app.utils.populations.py"""
2+
import json
23
import logging
34

45
import requests
56

7+
import app.io
8+
69
LOGGER = logging.getLogger(__name__)
10+
GEONAMES_BACKUP_PATH = "geonames_population_mappings.json"
711

812
# Fetching of the populations.
913
def fetch_populations():
@@ -20,12 +24,20 @@ def fetch_populations():
2024
mappings = {}
2125

2226
# Fetch the countries.
23-
countries = requests.get("http://api.geonames.org/countryInfoJSON?username=dperic").json()["geonames"]
24-
25-
# Go through all the countries and perform the mapping.
26-
for country in countries:
27-
mappings.update({country["countryCode"]: int(country["population"]) or None})
28-
27+
try:
28+
countries = requests.get(
29+
"http://api.geonames.org/countryInfoJSON", params={"username": "dperic"}, timeout=2
30+
).json()["geonames"]
31+
# Go through all the countries and perform the mapping.
32+
for country in countries:
33+
mappings.update({country["countryCode"]: int(country["population"]) or None})
34+
35+
if mappings:
36+
app.io.save(GEONAMES_BACKUP_PATH, mappings)
37+
except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err:
38+
LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}")
39+
mappings = app.io.load(GEONAMES_BACKUP_PATH)
40+
LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}")
2941
# Finally, return the mappings.
3042
return mappings
3143

0 commit comments

Comments
 (0)