Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Support [my-ip.io](https://www.my-ip.io/)
- Support [ifconfig.co](https://ifconfig.co/json)
- Support [reallyfreegeoip.org](https://reallyfreegeoip.org/json/)
- Support [freeipapi.com](https://freeipapi.com/api/json/)
- `AUTO_SAFE` mode
- `_get_json_standard` function
- `_get_json_ipv4_forced` function
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Public IP and Location Info:

#### IPv4 API

ℹ️ `ipv4-api` valid choices: [`auto-safe`, `auto`, `ip-api.com`, `ipinfo.io`, `ip.sb`, `ident.me`, `tnedi.me`, `ipapi.co`, `ipleak.net`, `my-ip.io`, `ifconfig.co`, `reallyfreegeoip.org`]
ℹ️ `ipv4-api` valid choices: [`auto-safe`, `auto`, `ip-api.com`, `ipinfo.io`, `ip.sb`, `ident.me`, `tnedi.me`, `ipapi.co`, `ipleak.net`, `my-ip.io`, `ifconfig.co`, `reallyfreegeoip.org`, `freeipapi.com`]

ℹ️ The default value: `auto-safe`

Expand Down
33 changes: 33 additions & 0 deletions ipspot/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,34 @@ def _tnedi_me_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
return {"status": False, "error": str(e)}


def _freeipapi_com_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]=5
) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
"""
Get public IP and geolocation using freeipapi.com.

:param geo: geolocation flag
:param timeout: timeout value for API
"""
try:
data = _get_json_ipv4_forced(url="https://freeipapi.com/api/json", timeout=timeout)
result = {"status": True, "data": {"ip": data["ipAddress"], "api": "freeipapi.com"}}
if geo:
geo_data = {
"city": data.get("cityName"),
"region": data.get("regionName"),
"country": data.get("countryName"),
"country_code": data.get("countryCode"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": None,
"timezone": data.get("timeZone")
}
result["data"].update(geo_data)
return result
except Exception as e:
return {"status": False, "error": str(e)}


IPV4_API_MAP = {
IPv4API.IFCONFIG_CO: {
"thread_safe": False,
Expand Down Expand Up @@ -430,6 +458,11 @@ def _tnedi_me_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
"geo": True,
"function": _reallyfreegeoip_org_ipv4
},
IPv4API.FREEIPAPI_COM: {
"thread_safe": False,
"geo": True,
"function": _freeipapi_com_ipv4,
}
}


Expand Down
1 change: 1 addition & 0 deletions ipspot/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class IPv4API(Enum):
MY_IP_IO = "my-ip.io"
IFCONFIG_CO = "ifconfig.co"
REALLYFREEGEOIP_ORG = "reallyfreegeoip.org"
FREEIPAPI_COM = "freeipapi.com"


PARAMETERS_NAME_MAP = {
Expand Down
20 changes: 20 additions & 0 deletions tests/test_ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,26 @@ def test_public_ipv4_ifconfig_co_net_error():
assert result["error"] == "No Internet"


def test_public_ipv4_freeipapi_com_success():
result = get_public_ipv4(api=IPv4API.FREEIPAPI_COM, geo=True)
assert result["status"]
assert is_ipv4(result["data"]["ip"])
assert set(result["data"].keys()) == DATA_ITEMS
assert result["data"]["api"] == "freeipapi.com"


def test_public_ipv4_freeipapi_com_timeout_error():
result = get_public_ipv4(api=IPv4API.FREEIPAPI_COM, geo=True, timeout="5")
assert not result["status"]


def test_public_ipv4_freeipapi_com_net_error():
with mock.patch.object(requests.Session, "get", side_effect=Exception("No Internet")):
result = get_public_ipv4(api=IPv4API.FREEIPAPI_COM)
assert not result["status"]
assert result["error"] == "No Internet"


def test_public_ipv4_ip_api_com_success():
result = get_public_ipv4(api=IPv4API.IP_API_COM, geo=True)
assert result["status"]
Expand Down