Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Continent #70

Merged
merged 4 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
include ipinfo/countries.json
include ipinfo/eu.json
include ipinfo/flags.json
include ipinfo/currency.json
include ipinfo/continent.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh just FYI, we gotta ensure this also exists for nodejs and other places that have such a manifest.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Request behavior can be modified by setting the `request_options` keyword argume

### Internationalization

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.
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.

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

Expand Down
253 changes: 253 additions & 0 deletions ipinfo/continent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
{
"BD": {"code": "AS", "name": "Asia"},
"BE": {"code": "EU", "name": "Europe"},
"BF": {"code": "AF", "name": "Africa"},
"BG": {"code": "EU", "name": "Europe"},
"BA": {"code": "EU", "name": "Europe"},
"BB": {"code": "NA", "name": "North America"},
"WF": {"code": "OC", "name": "Oceania"},
"BL": {"code": "NA", "name": "North America"},
"BM": {"code": "NA", "name": "North America"},
"BN": {"code": "AS", "name": "Asia"},
"BO": {"code": "SA", "name": "South America"},
"BH": {"code": "AS", "name": "Asia"},
"BI": {"code": "AF", "name": "Africa"},
"BJ": {"code": "AF", "name": "Africa"},
"BT": {"code": "AS", "name": "Asia"},
"JM": {"code": "NA", "name": "North America"},
"BV": {"code": "AN", "name": "Antarctica"},
"BW": {"code": "AF", "name": "Africa"},
"WS": {"code": "OC", "name": "Oceania"},
"BQ": {"code": "NA", "name": "North America"},
"BR": {"code": "SA", "name": "South America"},
"BS": {"code": "NA", "name": "North America"},
"JE": {"code": "EU", "name": "Europe"},
"BY": {"code": "EU", "name": "Europe"},
"BZ": {"code": "NA", "name": "North America"},
"RU": {"code": "EU", "name": "Europe"},
"RW": {"code": "AF", "name": "Africa"},
"RS": {"code": "EU", "name": "Europe"},
"TL": {"code": "OC", "name": "Oceania"},
"RE": {"code": "AF", "name": "Africa"},
"TM": {"code": "AS", "name": "Asia"},
"TJ": {"code": "AS", "name": "Asia"},
"RO": {"code": "EU", "name": "Europe"},
"TK": {"code": "OC", "name": "Oceania"},
"GW": {"code": "AF", "name": "Africa"},
"GU": {"code": "OC", "name": "Oceania"},
"GT": {"code": "NA", "name": "North America"},
"GS": {"code": "AN", "name": "Antarctica"},
"GR": {"code": "EU", "name": "Europe"},
"GQ": {"code": "AF", "name": "Africa"},
"GP": {"code": "NA", "name": "North America"},
"JP": {"code": "AS", "name": "Asia"},
"GY": {"code": "SA", "name": "South America"},
"GG": {"code": "EU", "name": "Europe"},
"GF": {"code": "SA", "name": "South America"},
"GE": {"code": "AS", "name": "Asia"},
"GD": {"code": "NA", "name": "North America"},
"GB": {"code": "EU", "name": "Europe"},
"GA": {"code": "AF", "name": "Africa"},
"SV": {"code": "NA", "name": "North America"},
"GN": {"code": "AF", "name": "Africa"},
"GM": {"code": "AF", "name": "Africa"},
"GL": {"code": "NA", "name": "North America"},
"GI": {"code": "EU", "name": "Europe"},
"GH": {"code": "AF", "name": "Africa"},
"OM": {"code": "AS", "name": "Asia"},
"TN": {"code": "AF", "name": "Africa"},
"JO": {"code": "AS", "name": "Asia"},
"HR": {"code": "EU", "name": "Europe"},
"HT": {"code": "NA", "name": "North America"},
"HU": {"code": "EU", "name": "Europe"},
"HK": {"code": "AS", "name": "Asia"},
"HN": {"code": "NA", "name": "North America"},
"HM": {"code": "AN", "name": "Antarctica"},
"VE": {"code": "SA", "name": "South America"},
"PR": {"code": "NA", "name": "North America"},
"PS": {"code": "AS", "name": "Asia"},
"PW": {"code": "OC", "name": "Oceania"},
"PT": {"code": "EU", "name": "Europe"},
"SJ": {"code": "EU", "name": "Europe"},
"PY": {"code": "SA", "name": "South America"},
"IQ": {"code": "AS", "name": "Asia"},
"PA": {"code": "NA", "name": "North America"},
"PF": {"code": "OC", "name": "Oceania"},
"PG": {"code": "OC", "name": "Oceania"},
"PE": {"code": "SA", "name": "South America"},
"PK": {"code": "AS", "name": "Asia"},
"PH": {"code": "AS", "name": "Asia"},
"PN": {"code": "OC", "name": "Oceania"},
"PL": {"code": "EU", "name": "Europe"},
"PM": {"code": "NA", "name": "North America"},
"ZM": {"code": "AF", "name": "Africa"},
"EH": {"code": "AF", "name": "Africa"},
"EE": {"code": "EU", "name": "Europe"},
"EG": {"code": "AF", "name": "Africa"},
"ZA": {"code": "AF", "name": "Africa"},
"EC": {"code": "SA", "name": "South America"},
"IT": {"code": "EU", "name": "Europe"},
"VN": {"code": "AS", "name": "Asia"},
"SB": {"code": "OC", "name": "Oceania"},
"ET": {"code": "AF", "name": "Africa"},
"SO": {"code": "AF", "name": "Africa"},
"ZW": {"code": "AF", "name": "Africa"},
"SA": {"code": "AS", "name": "Asia"},
"ES": {"code": "EU", "name": "Europe"},
"ER": {"code": "AF", "name": "Africa"},
"ME": {"code": "EU", "name": "Europe"},
"MD": {"code": "EU", "name": "Europe"},
"MG": {"code": "AF", "name": "Africa"},
"MF": {"code": "NA", "name": "North America"},
"MA": {"code": "AF", "name": "Africa"},
"MC": {"code": "EU", "name": "Europe"},
"UZ": {"code": "AS", "name": "Asia"},
"MM": {"code": "AS", "name": "Asia"},
"ML": {"code": "AF", "name": "Africa"},
"MO": {"code": "AS", "name": "Asia"},
"MN": {"code": "AS", "name": "Asia"},
"MH": {"code": "OC", "name": "Oceania"},
"MK": {"code": "EU", "name": "Europe"},
"MU": {"code": "AF", "name": "Africa"},
"MT": {"code": "EU", "name": "Europe"},
"MW": {"code": "AF", "name": "Africa"},
"MV": {"code": "AS", "name": "Asia"},
"MQ": {"code": "NA", "name": "North America"},
"MP": {"code": "OC", "name": "Oceania"},
"MS": {"code": "NA", "name": "North America"},
"MR": {"code": "AF", "name": "Africa"},
"IM": {"code": "EU", "name": "Europe"},
"UG": {"code": "AF", "name": "Africa"},
"TZ": {"code": "AF", "name": "Africa"},
"MY": {"code": "AS", "name": "Asia"},
"MX": {"code": "NA", "name": "North America"},
"IL": {"code": "AS", "name": "Asia"},
"FR": {"code": "EU", "name": "Europe"},
"IO": {"code": "AS", "name": "Asia"},
"SH": {"code": "AF", "name": "Africa"},
"FI": {"code": "EU", "name": "Europe"},
"FJ": {"code": "OC", "name": "Oceania"},
"FK": {"code": "SA", "name": "South America"},
"FM": {"code": "OC", "name": "Oceania"},
"FO": {"code": "EU", "name": "Europe"},
"NI": {"code": "NA", "name": "North America"},
"NL": {"code": "EU", "name": "Europe"},
"NO": {"code": "EU", "name": "Europe"},
"NA": {"code": "AF", "name": "Africa"},
"VU": {"code": "OC", "name": "Oceania"},
"NC": {"code": "OC", "name": "Oceania"},
"NE": {"code": "AF", "name": "Africa"},
"NF": {"code": "OC", "name": "Oceania"},
"NG": {"code": "AF", "name": "Africa"},
"NZ": {"code": "OC", "name": "Oceania"},
"NP": {"code": "AS", "name": "Asia"},
"NR": {"code": "OC", "name": "Oceania"},
"NU": {"code": "OC", "name": "Oceania"},
"CK": {"code": "OC", "name": "Oceania"},
"XK": {"code": "EU", "name": "Europe"},
"CI": {"code": "AF", "name": "Africa"},
"CH": {"code": "EU", "name": "Europe"},
"CO": {"code": "SA", "name": "South America"},
"CN": {"code": "AS", "name": "Asia"},
"CM": {"code": "AF", "name": "Africa"},
"CL": {"code": "SA", "name": "South America"},
"CC": {"code": "AS", "name": "Asia"},
"CA": {"code": "NA", "name": "North America"},
"CG": {"code": "AF", "name": "Africa"},
"CF": {"code": "AF", "name": "Africa"},
"CD": {"code": "AF", "name": "Africa"},
"CZ": {"code": "EU", "name": "Europe"},
"CY": {"code": "EU", "name": "Europe"},
"CX": {"code": "AS", "name": "Asia"},
"CR": {"code": "NA", "name": "North America"},
"CW": {"code": "NA", "name": "North America"},
"CV": {"code": "AF", "name": "Africa"},
"CU": {"code": "NA", "name": "North America"},
"SZ": {"code": "AF", "name": "Africa"},
"SY": {"code": "AS", "name": "Asia"},
"SX": {"code": "NA", "name": "North America"},
"KG": {"code": "AS", "name": "Asia"},
"KE": {"code": "AF", "name": "Africa"},
"SS": {"code": "AF", "name": "Africa"},
"SR": {"code": "SA", "name": "South America"},
"KI": {"code": "OC", "name": "Oceania"},
"KH": {"code": "AS", "name": "Asia"},
"KN": {"code": "NA", "name": "North America"},
"KM": {"code": "AF", "name": "Africa"},
"ST": {"code": "AF", "name": "Africa"},
"SK": {"code": "EU", "name": "Europe"},
"KR": {"code": "AS", "name": "Asia"},
"SI": {"code": "EU", "name": "Europe"},
"KP": {"code": "AS", "name": "Asia"},
"KW": {"code": "AS", "name": "Asia"},
"SN": {"code": "AF", "name": "Africa"},
"SM": {"code": "EU", "name": "Europe"},
"SL": {"code": "AF", "name": "Africa"},
"SC": {"code": "AF", "name": "Africa"},
"KZ": {"code": "AS", "name": "Asia"},
"KY": {"code": "NA", "name": "North America"},
"SG": {"code": "AS", "name": "Asia"},
"SE": {"code": "EU", "name": "Europe"},
"SD": {"code": "AF", "name": "Africa"},
"DO": {"code": "NA", "name": "North America"},
"DM": {"code": "NA", "name": "North America"},
"DJ": {"code": "AF", "name": "Africa"},
"DK": {"code": "EU", "name": "Europe"},
"VG": {"code": "NA", "name": "North America"},
"DE": {"code": "EU", "name": "Europe"},
"YE": {"code": "AS", "name": "Asia"},
"DZ": {"code": "AF", "name": "Africa"},
"US": {"code": "NA", "name": "North America"},
"UY": {"code": "SA", "name": "South America"},
"YT": {"code": "AF", "name": "Africa"},
"UM": {"code": "OC", "name": "Oceania"},
"LB": {"code": "AS", "name": "Asia"},
"LC": {"code": "NA", "name": "North America"},
"LA": {"code": "AS", "name": "Asia"},
"TV": {"code": "OC", "name": "Oceania"},
"TW": {"code": "AS", "name": "Asia"},
"TT": {"code": "NA", "name": "North America"},
"TR": {"code": "AS", "name": "Asia"},
"LK": {"code": "AS", "name": "Asia"},
"LI": {"code": "EU", "name": "Europe"},
"LV": {"code": "EU", "name": "Europe"},
"TO": {"code": "OC", "name": "Oceania"},
"LT": {"code": "EU", "name": "Europe"},
"LU": {"code": "EU", "name": "Europe"},
"LR": {"code": "AF", "name": "Africa"},
"LS": {"code": "AF", "name": "Africa"},
"TH": {"code": "AS", "name": "Asia"},
"TF": {"code": "AN", "name": "Antarctica"},
"TG": {"code": "AF", "name": "Africa"},
"TD": {"code": "AF", "name": "Africa"},
"TC": {"code": "NA", "name": "North America"},
"LY": {"code": "AF", "name": "Africa"},
"VA": {"code": "EU", "name": "Europe"},
"VC": {"code": "NA", "name": "North America"},
"AE": {"code": "AS", "name": "Asia"},
"AD": {"code": "EU", "name": "Europe"},
"AG": {"code": "NA", "name": "North America"},
"AF": {"code": "AS", "name": "Asia"},
"AI": {"code": "NA", "name": "North America"},
"VI": {"code": "NA", "name": "North America"},
"IS": {"code": "EU", "name": "Europe"},
"IR": {"code": "AS", "name": "Asia"},
"AM": {"code": "AS", "name": "Asia"},
"AL": {"code": "EU", "name": "Europe"},
"AO": {"code": "AF", "name": "Africa"},
"AQ": {"code": "AN", "name": "Antarctica"},
"AS": {"code": "OC", "name": "Oceania"},
"AR": {"code": "SA", "name": "South America"},
"AU": {"code": "OC", "name": "Oceania"},
"AT": {"code": "EU", "name": "Europe"},
"AW": {"code": "NA", "name": "North America"},
"IN": {"code": "AS", "name": "Asia"},
"AX": {"code": "EU", "name": "Europe"},
"AZ": {"code": "AS", "name": "Asia"},
"IE": {"code": "EU", "name": "Europe"},
"ID": {"code": "AS", "name": "Asia"},
"UA": {"code": "EU", "name": "Europe"},
"QA": {"code": "AS", "name": "Asia"},
"MZ": {"code": "AF", "name": "Africa"}
}

10 changes: 10 additions & 0 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
COUNTRY_EU_FILE_DEFAULT,
COUNTRY_FLAG_FILE_DEFAULT,
COUNTRY_CURRENCY_FILE_DEFAULT,
CONTINENT_FILE_DEFAULT,
BATCH_MAX_SIZE,
CACHE_MAXSIZE,
CACHE_TTL,
Expand Down Expand Up @@ -70,6 +71,13 @@ def __init__(self, access_token=None, **kwargs):
else COUNTRY_CURRENCY_FILE_DEFAULT
)

# load continent file
self.continents = handler_utils.read_json_file(
kwargs.get("continent_file")
if kwargs.get("continent_file")
else CONTINENT_FILE_DEFAULT
)

# setup req opts
self.request_options = kwargs.get("request_options", {})
if "timeout" not in self.request_options:
Expand Down Expand Up @@ -131,6 +139,7 @@ def getDetails(self, ip_address=None, timeout=None):
self.eu_countries,
self.countries_flags,
self.countries_currencies,
self.continents,
)
self.cache[cache_key(ip_address)] = details

Expand Down Expand Up @@ -256,6 +265,7 @@ def getBatchDetails(
self.eu_countries,
self.countries_flags,
self.countries_currencies,
self.continents,
)

return result
Expand Down
10 changes: 10 additions & 0 deletions ipinfo/handler_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
COUNTRY_FILE_DEFAULT,
COUNTRY_FLAG_FILE_DEFAULT,
COUNTRY_CURRENCY_FILE_DEFAULT,
CONTINENT_FILE_DEFAULT,
BATCH_MAX_SIZE,
CACHE_MAXSIZE,
CACHE_TTL,
Expand Down Expand Up @@ -71,6 +72,13 @@ def __init__(self, access_token=None, **kwargs):
else COUNTRY_CURRENCY_FILE_DEFAULT
)

# load continent file
self.continents = handler_utils.read_json_file(
kwargs.get("continent_file")
if kwargs.get("continent_file")
else CONTINENT_FILE_DEFAULT
)

# setup req opts
self.request_options = kwargs.get("request_options", {})
if "timeout" not in self.request_options:
Expand Down Expand Up @@ -154,6 +162,7 @@ async def getDetails(self, ip_address=None, timeout=None):
self.eu_countries,
self.countries_flags,
self.countries_currencies,
self.continents,
)
self.cache[cache_key(ip_address)] = details

Expand Down Expand Up @@ -313,6 +322,7 @@ async def _do_batch_req(
self.eu_countries,
self.countries_flags,
self.countries_currencies,
self.continents,
)
self.cache[cache_key(ip_address)] = details

Expand Down
12 changes: 11 additions & 1 deletion ipinfo/handler_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

COUNTRY_CURRENCY_FILE_DEFAULT = "currency.json"

CONTINENT_FILE_DEFAULT = "continent.json"

# The max amount of IPs allowed by the API per batch request.
BATCH_MAX_SIZE = 1000

Expand Down Expand Up @@ -58,7 +60,12 @@ def get_headers(access_token):


def format_details(
details, countries, eu_countries, countries_flags, countries_currencies
details,
countries,
eu_countries,
countries_flags,
countries_currencies,
continents,
):
"""
Format details given a countries object.
Expand All @@ -71,6 +78,9 @@ def format_details(
details["country_currency"] = copy.deepcopy(
countries_currencies.get(details.get("country"))
)
details["continent"] = copy.deepcopy(
continents.get(details.get("country"))
)
details["latitude"], details["longitude"] = read_coords(details.get("loc"))


Expand Down
3 changes: 3 additions & 0 deletions tests/handler_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ async def test_get_details():
country_currency = details.country_currency
assert country_currency["code"] == "USD"
assert country_currency["symbol"] == "$"
continent = details.continent
assert continent["code"] == "NA"
assert continent["name"] == "North America"
assert details.loc == "37.4056,-122.0775"
assert details.latitude == "37.4056"
assert details.longitude == "-122.0775"
Expand Down
3 changes: 3 additions & 0 deletions tests/handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def test_get_details():
country_currency = details.country_currency
assert country_currency["code"] == "USD"
assert country_currency["symbol"] == "$"
continent = details.continent
assert continent["code"] == "NA"
assert continent["name"] == "North America"
assert details.loc == "37.4056,-122.0775"
assert details.latitude == "37.4056"
assert details.longitude == "-122.0775"
Expand Down