-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_alt_to_flag.py
More file actions
40 lines (39 loc) · 1.64 KB
/
add_alt_to_flag.py
File metadata and controls
40 lines (39 loc) · 1.64 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
import json
country_data = []
#sovereigns = []
#sovereigns_count = 0
try:
flag_fh = open('flag-descriptions.json')
flag_descriptions = json.load(flag_fh)
with open('src/main/resources/countriesV3.1.json') as fh:
data = json.load(fh)
for country_count, country in enumerate(data):
flag_data = None
# Query country's common name.
# For countries that share common names e.g. Guinea,
# query country's official name
if country['name']['common'] in flag_descriptions:
flag_country_name = country['name']['common']
elif country['name']['official'] in flag_descriptions:
flag_country_name = country['name']['official']
try:
if country['independent'] == True:
#sovereigns.append(country['name']['common'])
#sovereigns_count += 1
flag_data = {'svg': country['flags']['flagImages']['svg'],
'png': country['flags']['flagImages']['png'],
'alt': flag_descriptions[flag_country_name]}
except KeyError:
# Don't alter data of non-independent countries
pass
finally:
country_data.append(country)
if flag_data is not None:
country_data[country_count]['flags'] = flag_data
finally:
flag_fh.close()
with open('src/main/resources/countriesV3.1.json', 'w', encoding='utf-8') as write_fh:
json.dump(country_data, write_fh, indent=2, separators=(", ", ": "))
#print(len(country_data))
#print(sovereigns_count)
#print(sorted(sovereigns))