2021-04-10 14:21:38 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
def get_args():
|
|
|
|
""" Get command line arguments """
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('file', help="The countries.json file to generate the output from", type=str)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
""" Entry point for script """
|
|
|
|
with open(args.file) as fp:
|
|
|
|
countries = json.load(fp)
|
|
|
|
|
2022-01-19 20:35:33 +00:00
|
|
|
country_list = []
|
2021-04-10 14:21:38 +00:00
|
|
|
|
|
|
|
for country in countries:
|
2021-04-11 14:04:52 +00:00
|
|
|
if len(country['capital']) < 1 or country['capital'][0] == "" or not country['independent']:
|
2021-04-10 14:21:38 +00:00
|
|
|
continue
|
2022-01-19 20:35:33 +00:00
|
|
|
country_list.append({
|
2022-01-14 01:49:34 +00:00
|
|
|
'capital': country['capital'][0],
|
|
|
|
'countryname': country['name']['common'],
|
2021-04-11 14:35:16 +00:00
|
|
|
'region': country['region'],
|
2021-04-11 14:04:52 +00:00
|
|
|
'subregion': country['subregion'],
|
2022-01-14 01:49:34 +00:00
|
|
|
'languages': country['languages'],
|
|
|
|
'code': country['cca3'].lower()
|
2022-01-19 20:35:33 +00:00
|
|
|
})
|
2021-04-11 14:04:52 +00:00
|
|
|
|
|
|
|
print('countries = ', end='')
|
2022-01-13 20:10:47 +00:00
|
|
|
print(json.dumps(country_list), end=';')
|
2021-04-10 14:21:38 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
sys.exit(main(get_args()))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|