Skip to content

Commit 56a7443

Browse files
authored
Merge pull request ExpDev07#15 from Bost/master
Add two letter country codes to `:locations` according to ISO_3166-1
2 parents 10bc7ea + 83507c6 commit 56a7443

File tree

2 files changed

+297
-9
lines changed

2 files changed

+297
-9
lines changed

app/data/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from cachetools import cached, TTLCache
44
from app.utils import date as date_util
55

6+
from . import countrycodes as cc
7+
68
"""
79
Base URL for fetching data.
810
"""
@@ -16,8 +18,8 @@ def get_data(category):
1618

1719
# Adhere to category naming standard.
1820
category = category.lower().capitalize();
19-
20-
# Request the data
21+
22+
# Request the data
2123
request = requests.get(base_url % category)
2224
text = request.text
2325

@@ -31,10 +33,13 @@ def get_data(category):
3133
# Filter out all the dates.
3234
history = dict(filter(lambda element: date_util.is_date(element[0]), item.items()))
3335

36+
country = item['Country/Region']
37+
3438
# Normalize the item and append to locations.
3539
locations.append({
3640
# General info.
37-
'country': item['Country/Region'],
41+
'country': country,
42+
'country_code': cc.country_code(country),
3843
'province': item['Province/State'],
3944

4045
# Coordinates.
@@ -58,9 +63,3 @@ def get_data(category):
5863
'locations': locations,
5964
'latest': latest
6065
}
61-
62-
63-
64-
65-
66-

app/data/countrycodes.py

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
default_country_name = "XX"
2+
3+
is_3166_1 = {
4+
"Afghanistan" : "AF",
5+
"Åland Islands" : "AX",
6+
"Albania" : "AL",
7+
"Algeria" : "DZ",
8+
"American Samoa" : "AS",
9+
"Andorra" : "AD",
10+
"Angola" : "AO",
11+
"Anguilla" : "AI",
12+
"Antarctica" : "AQ",
13+
"Antigua and Barbuda" : "AG",
14+
"Argentina" : "AR",
15+
"Armenia" : "AM",
16+
"Aruba" : "AW",
17+
"Australia" : "AU",
18+
"Austria" : "AT",
19+
"Azerbaijan" : "AZ",
20+
"Bahamas" : "BS",
21+
"Bahrain" : "BH",
22+
"Bangladesh" : "BD",
23+
"Barbados" : "BB",
24+
"Belarus" : "BY",
25+
"Belgium" : "BE",
26+
"Belize" : "BZ",
27+
"Benin" : "BJ",
28+
"Bermuda" : "BM",
29+
"Bhutan" : "BT",
30+
"Bolivia, Plurinational State of" : "BO",
31+
"Bonaire, Sint Eustatius and Saba" : "BQ",
32+
"Bosnia and Herzegovina" : "BA",
33+
"Botswana" : "BW",
34+
"Bouvet Island" : "BV",
35+
"Brazil" : "BR",
36+
"British Indian Ocean Territory" : "IO",
37+
"Brunei Darussalam" : "BN",
38+
"Bulgaria" : "BG",
39+
"Burkina Faso" : "BF",
40+
"Burundi" : "BI",
41+
"Cambodia" : "KH",
42+
"Cameroon" : "CM",
43+
"Canada" : "CA",
44+
"Cape Verde" : "CV",
45+
"Cayman Islands" : "KY",
46+
"Central African Republic" : "CF",
47+
"Chad" : "TD",
48+
"Chile" : "CL",
49+
"China" : "CN",
50+
"Christmas Island" : "CX",
51+
"Cocos (Keeling) Islands" : "CC",
52+
"Colombia" : "CO",
53+
"Comoros" : "KM",
54+
"Congo" : "CG",
55+
"Congo, the Democratic Republic of the" : "CD",
56+
"Cook Islands" : "CK",
57+
"Costa Rica" : "CR",
58+
"Côte d'Ivoire" : "CI",
59+
"Croatia" : "HR",
60+
"Cuba" : "CU",
61+
"Curaçao" : "CW",
62+
"Cyprus" : "CY",
63+
"Czech Republic" : "CZ",
64+
"Denmark" : "DK",
65+
"Djibouti" : "DJ",
66+
"Dominica" : "DM",
67+
"Dominican Republic" : "DO",
68+
"Ecuador" : "EC",
69+
"Egypt" : "EG",
70+
"El Salvador" : "SV",
71+
"Equatorial Guinea" : "GQ",
72+
"Eritrea" : "ER",
73+
"Estonia" : "EE",
74+
"Ethiopia" : "ET",
75+
"Falkland Islands (Malvinas)" : "FK",
76+
"Faroe Islands" : "FO",
77+
"Fiji" : "FJ",
78+
"Finland" : "FI",
79+
"France" : "FR",
80+
"French Guiana" : "GF",
81+
"French Polynesia" : "PF",
82+
"French Southern Territories" : "TF",
83+
"Gabon" : "GA",
84+
"Gambia" : "GM",
85+
"Georgia" : "GE",
86+
"Germany" : "DE",
87+
"Ghana" : "GH",
88+
"Gibraltar" : "GI",
89+
"Greece" : "GR",
90+
"Greenland" : "GL",
91+
"Grenada" : "GD",
92+
"Guadeloupe" : "GP",
93+
"Guam" : "GU",
94+
"Guatemala" : "GT",
95+
"Guernsey" : "GG",
96+
"Guinea" : "GN",
97+
"Guinea-Bissau" : "GW",
98+
"Guyana" : "GY",
99+
"Haiti" : "HT",
100+
"Heard Island and McDonald Islands" : "HM",
101+
"Holy See (Vatican City State)" : "VA",
102+
"Honduras" : "HN",
103+
"Hong Kong" : "HK",
104+
"Hungary" : "HU",
105+
"Iceland" : "IS",
106+
"India" : "IN",
107+
"Indonesia" : "ID",
108+
"Iran, Islamic Republic of" : "IR",
109+
"Iraq" : "IQ",
110+
"Ireland" : "IE",
111+
"Isle of Man" : "IM",
112+
"Israel" : "IL",
113+
"Italy" : "IT",
114+
"Jamaica" : "JM",
115+
"Japan" : "JP",
116+
"Jersey" : "JE",
117+
"Jordan" : "JO",
118+
"Kazakhstan" : "KZ",
119+
"Kenya" : "KE",
120+
"Kiribati" : "KI",
121+
"Korea, Democratic People's Republic of" : "KP",
122+
"Korea, Republic of" : "KR",
123+
"Kuwait" : "KW",
124+
"Kyrgyzstan" : "KG",
125+
"Lao People's Democratic Republic" : "LA",
126+
"Latvia" : "LV",
127+
"Lebanon" : "LB",
128+
"Lesotho" : "LS",
129+
"Liberia" : "LR",
130+
"Libya" : "LY",
131+
"Liechtenstein" : "LI",
132+
"Lithuania" : "LT",
133+
"Luxembourg" : "LU",
134+
"Macao" : "MO",
135+
"North Macedonia" : "MK",
136+
"Madagascar" : "MG",
137+
"Malawi" : "MW",
138+
"Malaysia" : "MY",
139+
"Maldives" : "MV",
140+
"Mali" : "ML",
141+
"Malta" : "MT",
142+
"Marshall Islands" : "MH",
143+
"Martinique" : "MQ",
144+
"Mauritania" : "MR",
145+
"Mauritius" : "MU",
146+
"Mayotte" : "YT",
147+
"Mexico" : "MX",
148+
"Micronesia, Federated States of" : "FM",
149+
"Moldova, Republic of" : "MD",
150+
"Monaco" : "MC",
151+
"Mongolia" : "MN",
152+
"Montenegro" : "ME",
153+
"Montserrat" : "MS",
154+
"Morocco" : "MA",
155+
"Mozambique" : "MZ",
156+
"Myanmar" : "MM",
157+
"Namibia" : "NA",
158+
"Nauru" : "NR",
159+
"Nepal" : "NP",
160+
"Netherlands" : "NL",
161+
"New Caledonia" : "NC",
162+
"New Zealand" : "NZ",
163+
"Nicaragua" : "NI",
164+
"Niger" : "NE",
165+
"Nigeria" : "NG",
166+
"Niue" : "NU",
167+
"Norfolk Island" : "NF",
168+
"Northern Mariana Islands" : "MP",
169+
"Norway" : "NO",
170+
"Oman" : "OM",
171+
"Pakistan" : "PK",
172+
"Palau" : "PW",
173+
"Palestine, State of" : "PS",
174+
"Panama" : "PA",
175+
"Papua New Guinea" : "PG",
176+
"Paraguay" : "PY",
177+
"Peru" : "PE",
178+
"Philippines" : "PH",
179+
"Pitcairn" : "PN",
180+
"Poland" : "PL",
181+
"Portugal" : "PT",
182+
"Puerto Rico" : "PR",
183+
"Qatar" : "QA",
184+
"Réunion" : "RE",
185+
"Romania" : "RO",
186+
"Russian Federation" : "RU",
187+
"Rwanda" : "RW",
188+
"Saint Barthélemy" : "BL",
189+
"Saint Helena, Ascension and Tristan da Cunha" : "SH",
190+
"Saint Kitts and Nevis" : "KN",
191+
"Saint Lucia" : "LC",
192+
"Saint Martin (French part)" : "MF",
193+
"Saint Pierre and Miquelon" : "PM",
194+
"Saint Vincent and the Grenadines" : "VC",
195+
"Samoa" : "WS",
196+
"San Marino" : "SM",
197+
"Sao Tome and Principe" : "ST",
198+
"Saudi Arabia" : "SA",
199+
"Senegal" : "SN",
200+
"Serbia" : "RS",
201+
"Seychelles" : "SC",
202+
"Sierra Leone" : "SL",
203+
"Singapore" : "SG",
204+
"Sint Maarten (Dutch part)" : "SX",
205+
"Slovakia" : "SK",
206+
"Slovenia" : "SI",
207+
"Solomon Islands" : "SB",
208+
"Somalia" : "SO",
209+
"South Africa" : "ZA",
210+
"South Georgia and the South Sandwich Islands" : "GS",
211+
"South Sudan" : "SS",
212+
"Spain" : "ES",
213+
"Sri Lanka" : "LK",
214+
"Sudan" : "SD",
215+
"Suriname" : "SR",
216+
"Svalbard and Jan Mayen" : "SJ",
217+
"Swaziland" : "SZ",
218+
"Sweden" : "SE",
219+
"Switzerland" : "CH",
220+
"Syrian Arab Republic" : "SY",
221+
"Taiwan, Province of China" : "TW",
222+
"Tajikistan" : "TJ",
223+
"Tanzania, United Republic of" : "TZ",
224+
"Thailand" : "TH",
225+
"Timor-Leste" : "TL",
226+
"Togo" : "TG",
227+
"Tokelau" : "TK",
228+
"Tonga" : "TO",
229+
"Trinidad and Tobago" : "TT",
230+
"Tunisia" : "TN",
231+
"Turkey" : "TR",
232+
"Turkmenistan" : "TM",
233+
"Turks and Caicos Islands" : "TC",
234+
"Tuvalu" : "TV",
235+
"Uganda" : "UG",
236+
"Ukraine" : "UA",
237+
"United Arab Emirates" : "AE",
238+
"United Kingdom" : "GB",
239+
"United States" : "US",
240+
"United States Minor Outlying Islands" : "UM",
241+
"Uruguay" : "UY",
242+
"Uzbekistan" : "UZ",
243+
"Vanuatu" : "VU",
244+
"Venezuela, Bolivarian Republic of" : "VE",
245+
"Viet Nam" : "VN",
246+
"Virgin Islands, British" : "VG",
247+
"Virgin Islands, U.S." : "VI",
248+
"Wallis and Futuna" : "WF",
249+
"Western Sahara" : "EH",
250+
"Yemen" : "YE",
251+
"Zambia" : "ZM",
252+
"Zimbabwe" : "ZW",
253+
}
254+
255+
# Mapping of alternative names, spelling, typos to the names of countries used
256+
# by the ISO 3166-1 norm
257+
country_name_synonyms = {
258+
"Mainland China" : "China",
259+
"Czechia" : "Czech Republic",
260+
"South Korea" : "Korea, Republic of",
261+
"Taiwan" : "Taiwan, Province of China",
262+
"US" : "United States",
263+
# TODO Macau is probably a typo. Report it to CSSEGISandData/COVID-19
264+
"Macau" : "Macao",
265+
"Vietnam" : "Viet Nam",
266+
"UK" : "United Kingdom",
267+
"Russia" : "Russian Federation",
268+
"Iran" : "Iran, Islamic Republic of",
269+
"Saint Barthelemy" : "Saint Barthélemy",
270+
"Palestine" : "Palestine, State of",
271+
# "Others" has no mapping
272+
}
273+
274+
def country_code(country):
275+
"""
276+
Return two letter country code (Alpha-2) according to
277+
https://en.wikipedia.org/wiki/ISO_3166-1
278+
Defaults to "XX".
279+
"""
280+
if country in is_3166_1:
281+
return is_3166_1[country]
282+
else:
283+
if country in country_name_synonyms:
284+
synonym = country_name_synonyms[country]
285+
return is_3166_1[synonym]
286+
else:
287+
print ("No country_code found for '" + country + "'. Using '"
288+
+default_country_name+"'")
289+
return default_country_name

0 commit comments

Comments
 (0)