Skip to content

Commit

Permalink
Merge pull request #69 from ipinfo/umar/country-currency
Browse files Browse the repository at this point in the history
added country_currency
  • Loading branch information
UmanShahzad authored Nov 3, 2022
2 parents 55f8fc0 + 6980f1e commit b7cae65
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 7 deletions.
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` and `details.country_flag` 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` or change the country flag emoji or unicode by setting the keyword argument `countries_flags_file` 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.

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

Expand Down
252 changes: 252 additions & 0 deletions ipinfo/currency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
{
"AD" : { "code": "EUR" ,"symbol": ""},
"AE" : { "code": "AED" ,"symbol": "د.إ"},
"AF" : { "code": "AFN" ,"symbol": "؋"},
"AG" : { "code": "XCD" ,"symbol": "$"},
"AI" : { "code": "XCD" ,"symbol": "$"},
"AL" : { "code": "ALL" ,"symbol": "L"},
"AM" : { "code": "AMD" ,"symbol": "֏"},
"AO" : { "code": "AOA" ,"symbol": "Kz"},
"AQ" : { "code": "" ,"symbol": "$"},
"AR" : { "code": "ARS" ,"symbol": "$"},
"AS" : { "code": "USD" ,"symbol": "$"},
"AT" : { "code": "EUR" ,"symbol": ""},
"AU" : { "code": "AUD" ,"symbol": "$"},
"AW" : { "code": "AWG" ,"symbol": "ƒ"},
"AX" : { "code": "EUR" ,"symbol": ""},
"AZ" : { "code": "AZN" ,"symbol": ""},
"BA" : { "code": "BAM" ,"symbol": "KM"},
"BB" : { "code": "BBD" ,"symbol": "$"},
"BD" : { "code": "BDT" ,"symbol": ""},
"BE" : { "code": "EUR" ,"symbol": ""},
"BF" : { "code": "XOF" ,"symbol": "CFA"},
"BG" : { "code": "BGN" ,"symbol": "лв"},
"BH" : { "code": "BHD" ,"symbol": ".د.ب"},
"BI" : { "code": "BIF" ,"symbol": "FBu"},
"BJ" : { "code": "XOF" ,"symbol": "CFA"},
"BL" : { "code": "EUR" ,"symbol": ""},
"BM" : { "code": "BMD" ,"symbol": "$"},
"BN" : { "code": "BND" ,"symbol": "$"},
"BO" : { "code": "BOB" ,"symbol": "$b"},
"BQ" : { "code": "USD" ,"symbol": "$"},
"BR" : { "code": "BRL" ,"symbol": "R$"},
"BS" : { "code": "BSD" ,"symbol": "$"},
"BT" : { "code": "BTN" ,"symbol": "Nu."},
"BV" : { "code": "NOK" ,"symbol": "kr"},
"BW" : { "code": "BWP" ,"symbol": "P"},
"BY" : { "code": "BYR" ,"symbol": "Br"},
"BZ" : { "code": "BZD" ,"symbol": "BZ$"},
"CA" : { "code": "CAD" ,"symbol": "$"},
"CC" : { "code": "AUD" ,"symbol": "$"},
"CD" : { "code": "CDF" ,"symbol": "FC"},
"CF" : { "code": "XAF" ,"symbol": "FCFA"},
"CG" : { "code": "XAF" ,"symbol": "FCFA"},
"CH" : { "code": "CHF" ,"symbol": "CHF"},
"CI" : { "code": "XOF" ,"symbol": "CFA"},
"CK" : { "code": "NZD" ,"symbol": "$"},
"CL" : { "code": "CLP" ,"symbol": "$"},
"CM" : { "code": "XAF" ,"symbol": "FCFA"},
"CN" : { "code": "CNY" ,"symbol": "¥"},
"CO" : { "code": "COP" ,"symbol": "$"},
"CR" : { "code": "CRC" ,"symbol": ""},
"CU" : { "code": "CUP" ,"symbol": ""},
"CV" : { "code": "CVE" ,"symbol": "$"},
"CW" : { "code": "ANG" ,"symbol": "ƒ"},
"CX" : { "code": "AUD" ,"symbol": "$"},
"CY" : { "code": "EUR" ,"symbol": ""},
"CZ" : { "code": "CZK" ,"symbol": ""},
"DE" : { "code": "EUR" ,"symbol": ""},
"DJ" : { "code": "DJF" ,"symbol": "Fdj"},
"DK" : { "code": "DKK" ,"symbol": "kr"},
"DM" : { "code": "XCD" ,"symbol": "$"},
"DO" : { "code": "DOP" ,"symbol": "RD$"},
"DZ" : { "code": "DZD" ,"symbol": "دج"},
"EC" : { "code": "USD" ,"symbol": "$"},
"EE" : { "code": "EUR" ,"symbol": ""},
"EG" : { "code": "EGP" ,"symbol": "£"},
"EH" : { "code": "MAD" ,"symbol": "MAD"},
"ER" : { "code": "ERN" ,"symbol": "Nfk"},
"ES" : { "code": "EUR" ,"symbol": ""},
"ET" : { "code": "ETB" ,"symbol": "Br"},
"FI" : { "code": "EUR" ,"symbol": ""},
"FJ" : { "code": "FJD" ,"symbol": "$"},
"FK" : { "code": "FKP" ,"symbol": "£"},
"FM" : { "code": "USD" ,"symbol": "$"},
"FO" : { "code": "DKK" ,"symbol": "kr"},
"FR" : { "code": "EUR" ,"symbol": ""},
"GA" : { "code": "XAF" ,"symbol": "FCFA"},
"GB" : { "code": "GBP" ,"symbol": "£"},
"GD" : { "code": "XCD" ,"symbol": "$"},
"GE" : { "code": "GEL" ,"symbol": ""},
"GF" : { "code": "EUR" ,"symbol": ""},
"GG" : { "code": "GBP" ,"symbol": "£"},
"GH" : { "code": "GHS" ,"symbol": "GH₵"},
"GI" : { "code": "GIP" ,"symbol": "£"},
"GL" : { "code": "DKK" ,"symbol": "kr"},
"GM" : { "code": "GMD" ,"symbol": "D"},
"GN" : { "code": "GNF" ,"symbol": "FG"},
"GP" : { "code": "EUR" ,"symbol": ""},
"GQ" : { "code": "XAF" ,"symbol": "FCFA"},
"GR" : { "code": "EUR" ,"symbol": ""},
"GS" : { "code": "GBP" ,"symbol": "£"},
"GT" : { "code": "GTQ" ,"symbol": "Q"},
"GU" : { "code": "USD" ,"symbol": "$"},
"GW" : { "code": "XOF" ,"symbol": "CFA"},
"GY" : { "code": "GYD" ,"symbol": "$"},
"HK" : { "code": "HKD" ,"symbol": "$"},
"HM" : { "code": "AUD" ,"symbol": "$"},
"HN" : { "code": "HNL" ,"symbol": "L"},
"HR" : { "code": "HRK" ,"symbol": "kn"},
"HT" : { "code": "HTG" ,"symbol": "G"},
"HU" : { "code": "HUF" ,"symbol": "Ft"},
"ID" : { "code": "IDR" ,"symbol": "Rp"},
"IE" : { "code": "EUR" ,"symbol": ""},
"IL" : { "code": "ILS" ,"symbol": ""},
"IM" : { "code": "GBP" ,"symbol": "£"},
"IN" : { "code": "INR" ,"symbol": ""},
"IO" : { "code": "USD" ,"symbol": "$"},
"IQ" : { "code": "IQD" ,"symbol": "ع.د"},
"IR" : { "code": "IRR" ,"symbol": ""},
"IS" : { "code": "ISK" ,"symbol": "kr"},
"IT" : { "code": "EUR" ,"symbol": ""},
"JE" : { "code": "GBP" ,"symbol": "£"},
"JM" : { "code": "JMD" ,"symbol": "J$"},
"JO" : { "code": "JOD" ,"symbol": "JD"},
"JP" : { "code": "JPY" ,"symbol": "¥"},
"KE" : { "code": "KES" ,"symbol": "KSh"},
"KG" : { "code": "KGS" ,"symbol": "лв"},
"KH" : { "code": "KHR" ,"symbol": ""},
"KI" : { "code": "AUD" ,"symbol": "$"},
"KM" : { "code": "KMF" ,"symbol": "CF"},
"KN" : { "code": "XCD" ,"symbol": "$"},
"KP" : { "code": "KPW" ,"symbol": ""},
"KR" : { "code": "KRW" ,"symbol": ""},
"KW" : { "code": "KWD" ,"symbol": "KD"},
"KY" : { "code": "KYD" ,"symbol": "$"},
"KZ" : { "code": "KZT" ,"symbol": ""},
"LA" : { "code": "LAK" ,"symbol": ""},
"LB" : { "code": "LBP" ,"symbol": "£"},
"LC" : { "code": "XCD" ,"symbol": "$"},
"LI" : { "code": "CHF" ,"symbol": "CHF"},
"LK" : { "code": "LKR" ,"symbol": ""},
"LR" : { "code": "LRD" ,"symbol": "$"},
"LS" : { "code": "LSL" ,"symbol": "M"},
"LT" : { "code": "LTL" ,"symbol": "Lt"},
"LU" : { "code": "EUR" ,"symbol": ""},
"LV" : { "code": "EUR" ,"symbol": ""},
"LY" : { "code": "LYD" ,"symbol": "LD"},
"MA" : { "code": "MAD" ,"symbol": "MAD"},
"MC" : { "code": "EUR" ,"symbol": ""},
"MD" : { "code": "MDL" ,"symbol": "lei"},
"ME" : { "code": "EUR" ,"symbol": ""},
"MF" : { "code": "EUR" ,"symbol": ""},
"MG" : { "code": "MGA" ,"symbol": "Ar"},
"MH" : { "code": "USD" ,"symbol": "$"},
"MK" : { "code": "MKD" ,"symbol": "ден"},
"ML" : { "code": "XOF" ,"symbol": "CFA"},
"MM" : { "code": "MMK" ,"symbol": "K"},
"MN" : { "code": "MNT" ,"symbol": ""},
"MO" : { "code": "MOP" ,"symbol": "MOP$"},
"MP" : { "code": "USD" ,"symbol": "$"},
"MQ" : { "code": "EUR" ,"symbol": ""},
"MR" : { "code": "MRO" ,"symbol": "UM"},
"MS" : { "code": "XCD" ,"symbol": "$"},
"MT" : { "code": "EUR" ,"symbol": ""},
"MU" : { "code": "MUR" ,"symbol": ""},
"MV" : { "code": "MVR" ,"symbol": "Rf"},
"MW" : { "code": "MWK" ,"symbol": "MK"},
"MX" : { "code": "MXN" ,"symbol": "$"},
"MY" : { "code": "MYR" ,"symbol": "RM"},
"MZ" : { "code": "MZN" ,"symbol": "MT"},
"NA" : { "code": "NAD" ,"symbol": "$"},
"NC" : { "code": "XPF" ,"symbol": ""},
"NE" : { "code": "XOF" ,"symbol": "CFA"},
"NF" : { "code": "AUD" ,"symbol": "$"},
"NG" : { "code": "NGN" ,"symbol": ""},
"NI" : { "code": "NIO" ,"symbol": "C$"},
"NL" : { "code": "EUR" ,"symbol": ""},
"NO" : { "code": "NOK" ,"symbol": "kr"},
"NP" : { "code": "NPR" ,"symbol": ""},
"NR" : { "code": "AUD" ,"symbol": "$"},
"NU" : { "code": "NZD" ,"symbol": "$"},
"NZ" : { "code": "NZD" ,"symbol": "$"},
"OM" : { "code": "OMR" ,"symbol": ""},
"PA" : { "code": "PAB" ,"symbol": "B/."},
"PE" : { "code": "PEN" ,"symbol": "S/."},
"PF" : { "code": "XPF" ,"symbol": ""},
"PG" : { "code": "PGK" ,"symbol": "K"},
"PH" : { "code": "PHP" ,"symbol": ""},
"PK" : { "code": "PKR" ,"symbol": ""},
"PL" : { "code": "PLN" ,"symbol": ""},
"PM" : { "code": "EUR" ,"symbol": ""},
"PN" : { "code": "NZD" ,"symbol": "$"},
"PR" : { "code": "USD" ,"symbol": "$"},
"PS" : { "code": "ILS" ,"symbol": ""},
"PT" : { "code": "EUR" ,"symbol": ""},
"PW" : { "code": "USD" ,"symbol": "$"},
"PY" : { "code": "PYG" ,"symbol": "Gs"},
"QA" : { "code": "QAR" ,"symbol": ""},
"RE" : { "code": "EUR" ,"symbol": ""},
"RO" : { "code": "RON" ,"symbol": "lei"},
"RS" : { "code": "RSD" ,"symbol": "Дин."},
"RU" : { "code": "RUB" ,"symbol": ""},
"RW" : { "code": "RWF" ,"symbol": "R₣"},
"SA" : { "code": "SAR" ,"symbol": ""},
"SB" : { "code": "SBD" ,"symbol": "$"},
"SC" : { "code": "SCR" ,"symbol": ""},
"SD" : { "code": "SDG" ,"symbol": "ج.س."},
"SE" : { "code": "SEK" ,"symbol": "kr"},
"SG" : { "code": "SGD" ,"symbol": "S$"},
"SH" : { "code": "SHP" ,"symbol": "£"},
"SI" : { "code": "EUR" ,"symbol": ""},
"SJ" : { "code": "NOK" ,"symbol": "kr"},
"SK" : { "code": "EUR" ,"symbol": ""},
"SL" : { "code": "SLL" ,"symbol": "Le"},
"SM" : { "code": "EUR" ,"symbol": ""},
"SN" : { "code": "XOF" ,"symbol": "CFA"},
"SO" : { "code": "SOS" ,"symbol": "S"},
"SR" : { "code": "SRD" ,"symbol": "$"},
"SS" : { "code": "SSP" ,"symbol": "£"},
"ST" : { "code": "STD" ,"symbol": "Db"},
"SV" : { "code": "USD" ,"symbol": "$"},
"SX" : { "code": "ANG" ,"symbol": "ƒ"},
"SY" : { "code": "SYP" ,"symbol": "£"},
"SZ" : { "code": "SZL" ,"symbol": "E"},
"TC" : { "code": "USD" ,"symbol": "$"},
"TD" : { "code": "XAF" ,"symbol": "FCFA"},
"TF" : { "code": "EUR" ,"symbol": ""},
"TG" : { "code": "XOF" ,"symbol": "CFA"},
"TH" : { "code": "THB" ,"symbol": "฿"},
"TJ" : { "code": "TJS" ,"symbol": "SM"},
"TK" : { "code": "NZD" ,"symbol": "$"},
"TL" : { "code": "USD" ,"symbol": "$"},
"TM" : { "code": "TMT" ,"symbol": "T"},
"TN" : { "code": "TND" ,"symbol": "د.ت"},
"TO" : { "code": "TOP" ,"symbol": "T$"},
"TR" : { "code": "TRY" ,"symbol": ""},
"TT" : { "code": "TTD" ,"symbol": "TT$"},
"TV" : { "code": "AUD" ,"symbol": "$"},
"TW" : { "code": "TWD" ,"symbol": "NT$"},
"TZ" : { "code": "TZS" ,"symbol": "TSh"},
"UA" : { "code": "UAH" ,"symbol": ""},
"UG" : { "code": "UGX" ,"symbol": "USh"},
"UM" : { "code": "USD" ,"symbol": "$"},
"US" : { "code": "USD" ,"symbol": "$"},
"UY" : { "code": "UYU" ,"symbol": "$U"},
"UZ" : { "code": "UZS" ,"symbol": "лв"},
"VA" : { "code": "EUR" ,"symbol": ""},
"VC" : { "code": "XCD" ,"symbol": "$"},
"VE" : { "code": "VEF" ,"symbol": "Bs"},
"VG" : { "code": "USD" ,"symbol": "$"},
"VI" : { "code": "USD" ,"symbol": "$"},
"VN" : { "code": "VND" ,"symbol": ""},
"VU" : { "code": "VUV" ,"symbol": "VT"},
"WF" : { "code": "XPF" ,"symbol": ""},
"WS" : { "code": "WST" ,"symbol": "WS$"},
"XK" : { "code": "EUR" ,"symbol": ""},
"YE" : { "code": "YER" ,"symbol": ""},
"YT" : { "code": "EUR" ,"symbol": ""},
"ZA" : { "code": "ZAR" ,"symbol": "R"},
"ZM" : { "code": "ZMK" ,"symbol": "ZK"},
"ZW" : { "code": "ZWL" ,"symbol": "$"}
}
20 changes: 18 additions & 2 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
COUNTRY_FILE_DEFAULT,
COUNTRY_EU_FILE_DEFAULT,
COUNTRY_FLAG_FILE_DEFAULT,
COUNTRY_CURRENCY_FILE_DEFAULT,
BATCH_MAX_SIZE,
CACHE_MAXSIZE,
CACHE_TTL,
Expand Down Expand Up @@ -62,6 +63,13 @@ def __init__(self, access_token=None, **kwargs):
else COUNTRY_FLAG_FILE_DEFAULT
)

# load countries currency file
self.countries_currencies = handler_utils.read_json_file(
kwargs.get("countries_currencies_file")
if kwargs.get("countries_currencies_file")
else COUNTRY_CURRENCY_FILE_DEFAULT
)

# setup req opts
self.request_options = kwargs.get("request_options", {})
if "timeout" not in self.request_options:
Expand Down Expand Up @@ -118,7 +126,11 @@ def getDetails(self, ip_address=None, timeout=None):

# format & cache
handler_utils.format_details(
details, self.countries, self.eu_countries, self.countries_flags
details,
self.countries,
self.eu_countries,
self.countries_flags,
self.countries_currencies,
)
self.cache[cache_key(ip_address)] = details

Expand Down Expand Up @@ -239,7 +251,11 @@ def getBatchDetails(
for detail in result.values():
if isinstance(detail, dict):
handler_utils.format_details(
detail, self.countries, self.eu_countries, self.countries_flags
detail,
self.countries,
self.eu_countries,
self.countries_flags,
self.countries_currencies,
)

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

# load countries currency file
self.countries_currencies = handler_utils.read_json_file(
kwargs.get("countries_currencies_file")
if kwargs.get("countries_currencies_file")
else COUNTRY_CURRENCY_FILE_DEFAULT
)

# setup req opts
self.request_options = kwargs.get("request_options", {})
if "timeout" not in self.request_options:
Expand Down Expand Up @@ -141,7 +149,11 @@ async def getDetails(self, ip_address=None, timeout=None):

# format & cache
handler_utils.format_details(
details, self.countries, self.eu_countries, self.countries_flags
details,
self.countries,
self.eu_countries,
self.countries_flags,
self.countries_currencies,
)
self.cache[cache_key(ip_address)] = details

Expand Down Expand Up @@ -296,7 +308,11 @@ async def _do_batch_req(
for ip_address, details in json_resp.items():
if isinstance(details, dict):
handler_utils.format_details(
details, self.countries, self.eu_countries, self.countries_flags
details,
self.countries,
self.eu_countries,
self.countries_flags,
self.countries_currencies,
)
self.cache[cache_key(ip_address)] = details

Expand Down
13 changes: 11 additions & 2 deletions ipinfo/handler_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

COUNTRY_FLAG_FILE_DEFAULT = "flags.json"

COUNTRY_CURRENCY_FILE_DEFAULT = "currency.json"

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

Expand Down Expand Up @@ -55,13 +57,20 @@ def get_headers(access_token):
return headers


def format_details(details, countries, eu_countries, countries_flags):
def format_details(
details, countries, eu_countries, countries_flags, countries_currencies
):
"""
Format details given a countries object.
"""
details["country_name"] = countries.get(details.get("country"))
details["isEU"] = details.get("country") in eu_countries
details["country_flag"] = copy.deepcopy(countries_flags.get(details.get("country")))
details["country_flag"] = copy.deepcopy(
countries_flags.get(details.get("country"))
)
details["country_currency"] = copy.deepcopy(
countries_currencies.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 @@ -46,6 +46,9 @@ async def test_get_details():
country_flag = details.country_flag
assert country_flag["emoji"] == "🇺🇸"
assert country_flag["unicode"] == "U+1F1FA U+1F1F8"
country_currency = details.country_currency
assert country_currency["code"] == "USD"
assert country_currency["symbol"] == "$"
assert details.loc == "37.4056,-122.0775"
assert details.latitude == "37.4056"
assert details.longitude == "-122.0775"
Expand Down
Loading

0 comments on commit b7cae65

Please sign in to comment.