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
105 changes: 54 additions & 51 deletions ipspot/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,24 @@ def _ipsb_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
:param timeout: timeout value for API
"""
try:
response = requests.get("https://api-ipv4.ip.sb/geoip", headers=REQUEST_HEADERS, timeout=timeout)
response.raise_for_status()
data = response.json()
result = {"status": True, "data": {"ip": data.get("ip"), "api": "ip.sb"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": data.get("region"),
"country": data.get("country"),
"country_code": data.get("country_code"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": data.get("organization"),
"timezone": data.get("timezone")
}
result["data"].update(geo_data)
return result
with requests.Session() as session:
response = session.get("https://api-ipv4.ip.sb/geoip", headers=REQUEST_HEADERS, timeout=timeout)
response.raise_for_status()
data = response.json()
result = {"status": True, "data": {"ip": data.get("ip"), "api": "ip.sb"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": data.get("region"),
"country": data.get("country"),
"country_code": data.get("country_code"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": data.get("organization"),
"timezone": data.get("timezone")
}
result["data"].update(geo_data)
return result
except Exception as e:
return {"status": False, "error": str(e)}

Expand Down Expand Up @@ -212,23 +213,24 @@ def _ident_me_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
:param timeout: timeout value for API
"""
try:
response = requests.get("https://4.ident.me/json", headers=REQUEST_HEADERS, timeout=timeout)
response.raise_for_status()
data = response.json()
result = {"status": True, "data": {"ip": data.get("ip"), "api": "ident.me"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": None,
"country": data.get("country"),
"country_code": data.get("cc"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": data.get("aso"),
"timezone": data.get("tz")
}
result["data"].update(geo_data)
return result
with requests.Session() as session:
response = session.get("https://4.ident.me/json", headers=REQUEST_HEADERS, timeout=timeout)
response.raise_for_status()
data = response.json()
result = {"status": True, "data": {"ip": data.get("ip"), "api": "ident.me"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": None,
"country": data.get("country"),
"country_code": data.get("cc"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": data.get("aso"),
"timezone": data.get("tz")
}
result["data"].update(geo_data)
return result
except Exception as e:
return {"status": False, "error": str(e)}

Expand All @@ -242,23 +244,24 @@ def _tnedime_ipv4(geo: bool=False, timeout: Union[float, Tuple[float, float]]
:param timeout: timeout value for API
"""
try:
response = requests.get("https://4.tnedi.me/json", headers=REQUEST_HEADERS, timeout=timeout)
response.raise_for_status()
data = response.json()
result = {"status": True, "data": {"ip": data.get("ip"), "api": "tnedi.me"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": None,
"country": data.get("country"),
"country_code": data.get("cc"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": data.get("aso"),
"timezone": data.get("tz")
}
result["data"].update(geo_data)
return result
with requests.Session() as session:
response = session.get("https://4.tnedi.me/json", headers=REQUEST_HEADERS, timeout=timeout)
response.raise_for_status()
data = response.json()
result = {"status": True, "data": {"ip": data.get("ip"), "api": "tnedi.me"}}
if geo:
geo_data = {
"city": data.get("city"),
"region": None,
"country": data.get("country"),
"country_code": data.get("cc"),
"latitude": data.get("latitude"),
"longitude": data.get("longitude"),
"organization": data.get("aso"),
"timezone": data.get("tz")
}
result["data"].update(geo_data)
return result
except Exception as e:
return {"status": False, "error": str(e)}

Expand Down
8 changes: 4 additions & 4 deletions tests/test_ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_public_ipv4_auto_timeout_error():


def test_public_ipv4_auto_net_error():
with mock.patch("requests.get", side_effect=Exception("No Internet")), mock.patch.object(requests.Session, "get", side_effect=Exception("No Internet")):
with mock.patch.object(requests.Session, "get", side_effect=Exception("No Internet")):
result = get_public_ipv4(api=IPv4API.AUTO)
assert not result["status"]
assert result["error"] == "All attempts failed."
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_public_ipv4_ipsb_timeout_error():


def test_public_ipv4_ipsb_net_error():
with mock.patch("requests.get", side_effect=Exception("No Internet")):
with mock.patch.object(requests.Session, "get", side_effect=Exception("No Internet")):
result = get_public_ipv4(api=IPv4API.IPSB)
assert not result["status"]
assert result["error"] == "No Internet"
Expand All @@ -169,7 +169,7 @@ def test_public_ipv4_identme_timeout_error():


def test_public_ipv4_identme_net_error():
with mock.patch("requests.get", side_effect=Exception("No Internet")):
with mock.patch.object(requests.Session, "get", side_effect=Exception("No Internet")):
result = get_public_ipv4(api=IPv4API.IDENTME)
assert not result["status"]
assert result["error"] == "No Internet"
Expand All @@ -189,7 +189,7 @@ def test_public_ipv4_tnedime_timeout_error():


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