|
2 | 2 | Utilities used in handlers. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import copy |
5 | 6 | import json |
6 | 7 | import os |
7 | 8 | import sys |
8 | | -import copy |
9 | 9 |
|
10 | 10 | from .version import SDK_VERSION |
11 | 11 |
|
@@ -68,21 +68,26 @@ def format_details( |
68 | 68 | """ |
69 | 69 | Format details given a countries object. |
70 | 70 | """ |
71 | | - details["country_name"] = countries.get(details.get("country")) |
72 | | - details["isEU"] = details.get("country") in eu_countries |
73 | | - details["country_flag_url"] = ( |
74 | | - COUNTRY_FLAGS_URL + (details.get("country") or "") + ".svg" |
75 | | - ) |
76 | | - details["country_flag"] = copy.deepcopy( |
77 | | - countries_flags.get(details.get("country")) |
78 | | - ) |
79 | | - details["country_currency"] = copy.deepcopy( |
80 | | - countries_currencies.get(details.get("country")) |
81 | | - ) |
82 | | - details["continent"] = copy.deepcopy( |
83 | | - continents.get(details.get("country")) |
84 | | - ) |
85 | | - details["latitude"], details["longitude"] = read_coords(details.get("loc")) |
| 71 | + country_code = "" |
| 72 | + # Core and Lite API return the country_code in differently named fields |
| 73 | + if "country_code" in details: |
| 74 | + country_code = details.get("country_code") |
| 75 | + elif "country" in details: |
| 76 | + country_code = details.get("country") |
| 77 | + |
| 78 | + # country_code = details.get("country") |
| 79 | + if country_name := countries.get(country_code): |
| 80 | + details["country_name"] = country_name |
| 81 | + details["isEU"] = country_code in eu_countries |
| 82 | + details["country_flag_url"] = COUNTRY_FLAGS_URL + country_code + ".svg" |
| 83 | + if flag := countries_flags.get(country_code): |
| 84 | + details["country_flag"] = copy.deepcopy(flag) |
| 85 | + if currency := countries_currencies.get(country_code): |
| 86 | + details["country_currency"] = copy.deepcopy(currency) |
| 87 | + if continent := continents.get(country_code): |
| 88 | + details["continent"] = copy.deepcopy(continent) |
| 89 | + if location := details.get("loc"): |
| 90 | + details["latitude"], details["longitude"] = read_coords(location) |
86 | 91 |
|
87 | 92 |
|
88 | 93 | def read_coords(location): |
|
0 commit comments