Skip to content

Commit 8cf38d5

Browse files
authored
Merge pull request #70 from ipinfo/umar/continent
Added Continent
2 parents b7cae65 + 892a80d commit 8cf38d5

File tree

8 files changed

+294
-2
lines changed

8 files changed

+294
-2
lines changed

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
include ipinfo/countries.json
22
include ipinfo/eu.json
3+
include ipinfo/flags.json
4+
include ipinfo/currency.json
5+
include ipinfo/continent.json

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Request behavior can be modified by setting the `request_options` keyword argume
232232

233233
### Internationalization
234234

235-
When looking up an IP address, the response object includes a `details.country_name`, `details.isEU`, `details.country_flag` and `details.country_currency` attributes which includes the country based on American English. It is possible to return the country name in other languages by setting the `countries_file`, remove or add EU countries by setting the keyword argument `eu_countries_file`, change the country flag emoji or unicode by setting the keyword argument `countries_flags_file` or change country's currency code or currency symbol by setting the `countries_currencies` when creating the `IPinfo` object.
235+
When looking up an IP address, the response object includes a `details.country_name`, `details.isEU`, `details.country_flag` and `details.country_currency` attributes which includes the country based on American English. It is possible to return the country name in other languages by setting the `countries_file`, remove or add EU countries by setting the keyword argument `eu_countries_file`, change the country flag emoji or unicode by setting the keyword argument `countries_flags_file` or change country's currency code or currency symbol by setting the `countries_currencies` when creating the `IPinfo` object. Moreover the response object includes a `details.continent` which includes continent code and name of IP. The default file can be changed by setting the `continent_file` while creating the `IPinfo` object.
236236

237237
The file must be a `.json` file with the following structure:
238238

ipinfo/continent.json

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
{
2+
"BD": {"code": "AS", "name": "Asia"},
3+
"BE": {"code": "EU", "name": "Europe"},
4+
"BF": {"code": "AF", "name": "Africa"},
5+
"BG": {"code": "EU", "name": "Europe"},
6+
"BA": {"code": "EU", "name": "Europe"},
7+
"BB": {"code": "NA", "name": "North America"},
8+
"WF": {"code": "OC", "name": "Oceania"},
9+
"BL": {"code": "NA", "name": "North America"},
10+
"BM": {"code": "NA", "name": "North America"},
11+
"BN": {"code": "AS", "name": "Asia"},
12+
"BO": {"code": "SA", "name": "South America"},
13+
"BH": {"code": "AS", "name": "Asia"},
14+
"BI": {"code": "AF", "name": "Africa"},
15+
"BJ": {"code": "AF", "name": "Africa"},
16+
"BT": {"code": "AS", "name": "Asia"},
17+
"JM": {"code": "NA", "name": "North America"},
18+
"BV": {"code": "AN", "name": "Antarctica"},
19+
"BW": {"code": "AF", "name": "Africa"},
20+
"WS": {"code": "OC", "name": "Oceania"},
21+
"BQ": {"code": "NA", "name": "North America"},
22+
"BR": {"code": "SA", "name": "South America"},
23+
"BS": {"code": "NA", "name": "North America"},
24+
"JE": {"code": "EU", "name": "Europe"},
25+
"BY": {"code": "EU", "name": "Europe"},
26+
"BZ": {"code": "NA", "name": "North America"},
27+
"RU": {"code": "EU", "name": "Europe"},
28+
"RW": {"code": "AF", "name": "Africa"},
29+
"RS": {"code": "EU", "name": "Europe"},
30+
"TL": {"code": "OC", "name": "Oceania"},
31+
"RE": {"code": "AF", "name": "Africa"},
32+
"TM": {"code": "AS", "name": "Asia"},
33+
"TJ": {"code": "AS", "name": "Asia"},
34+
"RO": {"code": "EU", "name": "Europe"},
35+
"TK": {"code": "OC", "name": "Oceania"},
36+
"GW": {"code": "AF", "name": "Africa"},
37+
"GU": {"code": "OC", "name": "Oceania"},
38+
"GT": {"code": "NA", "name": "North America"},
39+
"GS": {"code": "AN", "name": "Antarctica"},
40+
"GR": {"code": "EU", "name": "Europe"},
41+
"GQ": {"code": "AF", "name": "Africa"},
42+
"GP": {"code": "NA", "name": "North America"},
43+
"JP": {"code": "AS", "name": "Asia"},
44+
"GY": {"code": "SA", "name": "South America"},
45+
"GG": {"code": "EU", "name": "Europe"},
46+
"GF": {"code": "SA", "name": "South America"},
47+
"GE": {"code": "AS", "name": "Asia"},
48+
"GD": {"code": "NA", "name": "North America"},
49+
"GB": {"code": "EU", "name": "Europe"},
50+
"GA": {"code": "AF", "name": "Africa"},
51+
"SV": {"code": "NA", "name": "North America"},
52+
"GN": {"code": "AF", "name": "Africa"},
53+
"GM": {"code": "AF", "name": "Africa"},
54+
"GL": {"code": "NA", "name": "North America"},
55+
"GI": {"code": "EU", "name": "Europe"},
56+
"GH": {"code": "AF", "name": "Africa"},
57+
"OM": {"code": "AS", "name": "Asia"},
58+
"TN": {"code": "AF", "name": "Africa"},
59+
"JO": {"code": "AS", "name": "Asia"},
60+
"HR": {"code": "EU", "name": "Europe"},
61+
"HT": {"code": "NA", "name": "North America"},
62+
"HU": {"code": "EU", "name": "Europe"},
63+
"HK": {"code": "AS", "name": "Asia"},
64+
"HN": {"code": "NA", "name": "North America"},
65+
"HM": {"code": "AN", "name": "Antarctica"},
66+
"VE": {"code": "SA", "name": "South America"},
67+
"PR": {"code": "NA", "name": "North America"},
68+
"PS": {"code": "AS", "name": "Asia"},
69+
"PW": {"code": "OC", "name": "Oceania"},
70+
"PT": {"code": "EU", "name": "Europe"},
71+
"SJ": {"code": "EU", "name": "Europe"},
72+
"PY": {"code": "SA", "name": "South America"},
73+
"IQ": {"code": "AS", "name": "Asia"},
74+
"PA": {"code": "NA", "name": "North America"},
75+
"PF": {"code": "OC", "name": "Oceania"},
76+
"PG": {"code": "OC", "name": "Oceania"},
77+
"PE": {"code": "SA", "name": "South America"},
78+
"PK": {"code": "AS", "name": "Asia"},
79+
"PH": {"code": "AS", "name": "Asia"},
80+
"PN": {"code": "OC", "name": "Oceania"},
81+
"PL": {"code": "EU", "name": "Europe"},
82+
"PM": {"code": "NA", "name": "North America"},
83+
"ZM": {"code": "AF", "name": "Africa"},
84+
"EH": {"code": "AF", "name": "Africa"},
85+
"EE": {"code": "EU", "name": "Europe"},
86+
"EG": {"code": "AF", "name": "Africa"},
87+
"ZA": {"code": "AF", "name": "Africa"},
88+
"EC": {"code": "SA", "name": "South America"},
89+
"IT": {"code": "EU", "name": "Europe"},
90+
"VN": {"code": "AS", "name": "Asia"},
91+
"SB": {"code": "OC", "name": "Oceania"},
92+
"ET": {"code": "AF", "name": "Africa"},
93+
"SO": {"code": "AF", "name": "Africa"},
94+
"ZW": {"code": "AF", "name": "Africa"},
95+
"SA": {"code": "AS", "name": "Asia"},
96+
"ES": {"code": "EU", "name": "Europe"},
97+
"ER": {"code": "AF", "name": "Africa"},
98+
"ME": {"code": "EU", "name": "Europe"},
99+
"MD": {"code": "EU", "name": "Europe"},
100+
"MG": {"code": "AF", "name": "Africa"},
101+
"MF": {"code": "NA", "name": "North America"},
102+
"MA": {"code": "AF", "name": "Africa"},
103+
"MC": {"code": "EU", "name": "Europe"},
104+
"UZ": {"code": "AS", "name": "Asia"},
105+
"MM": {"code": "AS", "name": "Asia"},
106+
"ML": {"code": "AF", "name": "Africa"},
107+
"MO": {"code": "AS", "name": "Asia"},
108+
"MN": {"code": "AS", "name": "Asia"},
109+
"MH": {"code": "OC", "name": "Oceania"},
110+
"MK": {"code": "EU", "name": "Europe"},
111+
"MU": {"code": "AF", "name": "Africa"},
112+
"MT": {"code": "EU", "name": "Europe"},
113+
"MW": {"code": "AF", "name": "Africa"},
114+
"MV": {"code": "AS", "name": "Asia"},
115+
"MQ": {"code": "NA", "name": "North America"},
116+
"MP": {"code": "OC", "name": "Oceania"},
117+
"MS": {"code": "NA", "name": "North America"},
118+
"MR": {"code": "AF", "name": "Africa"},
119+
"IM": {"code": "EU", "name": "Europe"},
120+
"UG": {"code": "AF", "name": "Africa"},
121+
"TZ": {"code": "AF", "name": "Africa"},
122+
"MY": {"code": "AS", "name": "Asia"},
123+
"MX": {"code": "NA", "name": "North America"},
124+
"IL": {"code": "AS", "name": "Asia"},
125+
"FR": {"code": "EU", "name": "Europe"},
126+
"IO": {"code": "AS", "name": "Asia"},
127+
"SH": {"code": "AF", "name": "Africa"},
128+
"FI": {"code": "EU", "name": "Europe"},
129+
"FJ": {"code": "OC", "name": "Oceania"},
130+
"FK": {"code": "SA", "name": "South America"},
131+
"FM": {"code": "OC", "name": "Oceania"},
132+
"FO": {"code": "EU", "name": "Europe"},
133+
"NI": {"code": "NA", "name": "North America"},
134+
"NL": {"code": "EU", "name": "Europe"},
135+
"NO": {"code": "EU", "name": "Europe"},
136+
"NA": {"code": "AF", "name": "Africa"},
137+
"VU": {"code": "OC", "name": "Oceania"},
138+
"NC": {"code": "OC", "name": "Oceania"},
139+
"NE": {"code": "AF", "name": "Africa"},
140+
"NF": {"code": "OC", "name": "Oceania"},
141+
"NG": {"code": "AF", "name": "Africa"},
142+
"NZ": {"code": "OC", "name": "Oceania"},
143+
"NP": {"code": "AS", "name": "Asia"},
144+
"NR": {"code": "OC", "name": "Oceania"},
145+
"NU": {"code": "OC", "name": "Oceania"},
146+
"CK": {"code": "OC", "name": "Oceania"},
147+
"XK": {"code": "EU", "name": "Europe"},
148+
"CI": {"code": "AF", "name": "Africa"},
149+
"CH": {"code": "EU", "name": "Europe"},
150+
"CO": {"code": "SA", "name": "South America"},
151+
"CN": {"code": "AS", "name": "Asia"},
152+
"CM": {"code": "AF", "name": "Africa"},
153+
"CL": {"code": "SA", "name": "South America"},
154+
"CC": {"code": "AS", "name": "Asia"},
155+
"CA": {"code": "NA", "name": "North America"},
156+
"CG": {"code": "AF", "name": "Africa"},
157+
"CF": {"code": "AF", "name": "Africa"},
158+
"CD": {"code": "AF", "name": "Africa"},
159+
"CZ": {"code": "EU", "name": "Europe"},
160+
"CY": {"code": "EU", "name": "Europe"},
161+
"CX": {"code": "AS", "name": "Asia"},
162+
"CR": {"code": "NA", "name": "North America"},
163+
"CW": {"code": "NA", "name": "North America"},
164+
"CV": {"code": "AF", "name": "Africa"},
165+
"CU": {"code": "NA", "name": "North America"},
166+
"SZ": {"code": "AF", "name": "Africa"},
167+
"SY": {"code": "AS", "name": "Asia"},
168+
"SX": {"code": "NA", "name": "North America"},
169+
"KG": {"code": "AS", "name": "Asia"},
170+
"KE": {"code": "AF", "name": "Africa"},
171+
"SS": {"code": "AF", "name": "Africa"},
172+
"SR": {"code": "SA", "name": "South America"},
173+
"KI": {"code": "OC", "name": "Oceania"},
174+
"KH": {"code": "AS", "name": "Asia"},
175+
"KN": {"code": "NA", "name": "North America"},
176+
"KM": {"code": "AF", "name": "Africa"},
177+
"ST": {"code": "AF", "name": "Africa"},
178+
"SK": {"code": "EU", "name": "Europe"},
179+
"KR": {"code": "AS", "name": "Asia"},
180+
"SI": {"code": "EU", "name": "Europe"},
181+
"KP": {"code": "AS", "name": "Asia"},
182+
"KW": {"code": "AS", "name": "Asia"},
183+
"SN": {"code": "AF", "name": "Africa"},
184+
"SM": {"code": "EU", "name": "Europe"},
185+
"SL": {"code": "AF", "name": "Africa"},
186+
"SC": {"code": "AF", "name": "Africa"},
187+
"KZ": {"code": "AS", "name": "Asia"},
188+
"KY": {"code": "NA", "name": "North America"},
189+
"SG": {"code": "AS", "name": "Asia"},
190+
"SE": {"code": "EU", "name": "Europe"},
191+
"SD": {"code": "AF", "name": "Africa"},
192+
"DO": {"code": "NA", "name": "North America"},
193+
"DM": {"code": "NA", "name": "North America"},
194+
"DJ": {"code": "AF", "name": "Africa"},
195+
"DK": {"code": "EU", "name": "Europe"},
196+
"VG": {"code": "NA", "name": "North America"},
197+
"DE": {"code": "EU", "name": "Europe"},
198+
"YE": {"code": "AS", "name": "Asia"},
199+
"DZ": {"code": "AF", "name": "Africa"},
200+
"US": {"code": "NA", "name": "North America"},
201+
"UY": {"code": "SA", "name": "South America"},
202+
"YT": {"code": "AF", "name": "Africa"},
203+
"UM": {"code": "OC", "name": "Oceania"},
204+
"LB": {"code": "AS", "name": "Asia"},
205+
"LC": {"code": "NA", "name": "North America"},
206+
"LA": {"code": "AS", "name": "Asia"},
207+
"TV": {"code": "OC", "name": "Oceania"},
208+
"TW": {"code": "AS", "name": "Asia"},
209+
"TT": {"code": "NA", "name": "North America"},
210+
"TR": {"code": "AS", "name": "Asia"},
211+
"LK": {"code": "AS", "name": "Asia"},
212+
"LI": {"code": "EU", "name": "Europe"},
213+
"LV": {"code": "EU", "name": "Europe"},
214+
"TO": {"code": "OC", "name": "Oceania"},
215+
"LT": {"code": "EU", "name": "Europe"},
216+
"LU": {"code": "EU", "name": "Europe"},
217+
"LR": {"code": "AF", "name": "Africa"},
218+
"LS": {"code": "AF", "name": "Africa"},
219+
"TH": {"code": "AS", "name": "Asia"},
220+
"TF": {"code": "AN", "name": "Antarctica"},
221+
"TG": {"code": "AF", "name": "Africa"},
222+
"TD": {"code": "AF", "name": "Africa"},
223+
"TC": {"code": "NA", "name": "North America"},
224+
"LY": {"code": "AF", "name": "Africa"},
225+
"VA": {"code": "EU", "name": "Europe"},
226+
"VC": {"code": "NA", "name": "North America"},
227+
"AE": {"code": "AS", "name": "Asia"},
228+
"AD": {"code": "EU", "name": "Europe"},
229+
"AG": {"code": "NA", "name": "North America"},
230+
"AF": {"code": "AS", "name": "Asia"},
231+
"AI": {"code": "NA", "name": "North America"},
232+
"VI": {"code": "NA", "name": "North America"},
233+
"IS": {"code": "EU", "name": "Europe"},
234+
"IR": {"code": "AS", "name": "Asia"},
235+
"AM": {"code": "AS", "name": "Asia"},
236+
"AL": {"code": "EU", "name": "Europe"},
237+
"AO": {"code": "AF", "name": "Africa"},
238+
"AQ": {"code": "AN", "name": "Antarctica"},
239+
"AS": {"code": "OC", "name": "Oceania"},
240+
"AR": {"code": "SA", "name": "South America"},
241+
"AU": {"code": "OC", "name": "Oceania"},
242+
"AT": {"code": "EU", "name": "Europe"},
243+
"AW": {"code": "NA", "name": "North America"},
244+
"IN": {"code": "AS", "name": "Asia"},
245+
"AX": {"code": "EU", "name": "Europe"},
246+
"AZ": {"code": "AS", "name": "Asia"},
247+
"IE": {"code": "EU", "name": "Europe"},
248+
"ID": {"code": "AS", "name": "Asia"},
249+
"UA": {"code": "EU", "name": "Europe"},
250+
"QA": {"code": "AS", "name": "Asia"},
251+
"MZ": {"code": "AF", "name": "Africa"}
252+
}
253+

ipinfo/handler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
COUNTRY_EU_FILE_DEFAULT,
2020
COUNTRY_FLAG_FILE_DEFAULT,
2121
COUNTRY_CURRENCY_FILE_DEFAULT,
22+
CONTINENT_FILE_DEFAULT,
2223
BATCH_MAX_SIZE,
2324
CACHE_MAXSIZE,
2425
CACHE_TTL,
@@ -70,6 +71,13 @@ def __init__(self, access_token=None, **kwargs):
7071
else COUNTRY_CURRENCY_FILE_DEFAULT
7172
)
7273

74+
# load continent file
75+
self.continents = handler_utils.read_json_file(
76+
kwargs.get("continent_file")
77+
if kwargs.get("continent_file")
78+
else CONTINENT_FILE_DEFAULT
79+
)
80+
7381
# setup req opts
7482
self.request_options = kwargs.get("request_options", {})
7583
if "timeout" not in self.request_options:
@@ -131,6 +139,7 @@ def getDetails(self, ip_address=None, timeout=None):
131139
self.eu_countries,
132140
self.countries_flags,
133141
self.countries_currencies,
142+
self.continents,
134143
)
135144
self.cache[cache_key(ip_address)] = details
136145

@@ -256,6 +265,7 @@ def getBatchDetails(
256265
self.eu_countries,
257266
self.countries_flags,
258267
self.countries_currencies,
268+
self.continents,
259269
)
260270

261271
return result

ipinfo/handler_async.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
COUNTRY_FILE_DEFAULT,
2121
COUNTRY_FLAG_FILE_DEFAULT,
2222
COUNTRY_CURRENCY_FILE_DEFAULT,
23+
CONTINENT_FILE_DEFAULT,
2324
BATCH_MAX_SIZE,
2425
CACHE_MAXSIZE,
2526
CACHE_TTL,
@@ -71,6 +72,13 @@ def __init__(self, access_token=None, **kwargs):
7172
else COUNTRY_CURRENCY_FILE_DEFAULT
7273
)
7374

75+
# load continent file
76+
self.continents = handler_utils.read_json_file(
77+
kwargs.get("continent_file")
78+
if kwargs.get("continent_file")
79+
else CONTINENT_FILE_DEFAULT
80+
)
81+
7482
# setup req opts
7583
self.request_options = kwargs.get("request_options", {})
7684
if "timeout" not in self.request_options:
@@ -154,6 +162,7 @@ async def getDetails(self, ip_address=None, timeout=None):
154162
self.eu_countries,
155163
self.countries_flags,
156164
self.countries_currencies,
165+
self.continents,
157166
)
158167
self.cache[cache_key(ip_address)] = details
159168

@@ -313,6 +322,7 @@ async def _do_batch_req(
313322
self.eu_countries,
314323
self.countries_flags,
315324
self.countries_currencies,
325+
self.continents,
316326
)
317327
self.cache[cache_key(ip_address)] = details
318328

ipinfo/handler_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
COUNTRY_CURRENCY_FILE_DEFAULT = "currency.json"
2424

25+
CONTINENT_FILE_DEFAULT = "continent.json"
26+
2527
# The max amount of IPs allowed by the API per batch request.
2628
BATCH_MAX_SIZE = 1000
2729

@@ -58,7 +60,12 @@ def get_headers(access_token):
5860

5961

6062
def format_details(
61-
details, countries, eu_countries, countries_flags, countries_currencies
63+
details,
64+
countries,
65+
eu_countries,
66+
countries_flags,
67+
countries_currencies,
68+
continents,
6269
):
6370
"""
6471
Format details given a countries object.
@@ -71,6 +78,9 @@ def format_details(
7178
details["country_currency"] = copy.deepcopy(
7279
countries_currencies.get(details.get("country"))
7380
)
81+
details["continent"] = copy.deepcopy(
82+
continents.get(details.get("country"))
83+
)
7484
details["latitude"], details["longitude"] = read_coords(details.get("loc"))
7585

7686

tests/handler_async_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ async def test_get_details():
4949
country_currency = details.country_currency
5050
assert country_currency["code"] == "USD"
5151
assert country_currency["symbol"] == "$"
52+
continent = details.continent
53+
assert continent["code"] == "NA"
54+
assert continent["name"] == "North America"
5255
assert details.loc == "37.4056,-122.0775"
5356
assert details.latitude == "37.4056"
5457
assert details.longitude == "-122.0775"

tests/handler_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def test_get_details():
4646
country_currency = details.country_currency
4747
assert country_currency["code"] == "USD"
4848
assert country_currency["symbol"] == "$"
49+
continent = details.continent
50+
assert continent["code"] == "NA"
51+
assert continent["name"] == "North America"
4952
assert details.loc == "37.4056,-122.0775"
5053
assert details.latitude == "37.4056"
5154
assert details.longitude == "-122.0775"

0 commit comments

Comments
 (0)