Skip to content

Commit b83ae16

Browse files
ip.sb (#12)
* feat : _ipsb_ipv4 function added * fix : IPv4API updated * doc : README.md updated * doc : CHANGELOG.md updated * fix : autopep8 * fix : REQUEST_HEADERS added
1 parent 7f06071 commit b83ae16

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Support [ip.sb](https://api.ip.sb/geoip)
810
### Changed
911
- `README.md` updated
1012
## [0.1] - 2025-04-25

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Public IP and Location Info:
139139

140140
#### IPv4 API
141141

142-
ℹ️ `ipv4-api` valid choices: [`auto`, `ipapi`, `ipinfo`]
142+
ℹ️ `ipv4-api` valid choices: [`auto`, `ipapi`, `ipinfo`, `ipsb`]
143143

144144
ℹ️ The default value: `auto`
145145

ipspot/functions.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Union, Dict, Any
66
import requests
77
from art import tprint
8-
from .params import IPv4API, PARAMETERS_NAME_MAP
8+
from .params import REQUEST_HEADERS, IPv4API, PARAMETERS_NAME_MAP
99
from .params import IPSPOT_OVERVIEW, IPSPOT_REPO, IPSPOT_VERSION
1010

1111

@@ -27,6 +27,34 @@ def get_private_ipv4() -> Dict[str, Union[bool, Dict[str, str], str]]:
2727
return {"status": False, "error": str(e)}
2828

2929

30+
def _ipsb_ipv4(geo: bool=False) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
31+
"""
32+
Get public IP and geolocation using ip.sb.
33+
34+
:param geo: geolocation flag
35+
"""
36+
try:
37+
response = requests.get("https://api.ip.sb/geoip", headers=REQUEST_HEADERS, timeout=5)
38+
response.raise_for_status()
39+
data = response.json()
40+
result = {"status": True, "data": {"ip": data.get("ip"), "api": "ip.sb"}}
41+
if geo:
42+
geo_data = {
43+
"city": data.get("city"),
44+
"region": data.get("region"),
45+
"country": data.get("country"),
46+
"country_code": data.get("country_code"),
47+
"latitude": data.get("latitude"),
48+
"longitude": data.get("longitude"),
49+
"organization": data.get("organization"),
50+
"timezone": data.get("timezone")
51+
}
52+
result["data"].update(geo_data)
53+
return result
54+
except Exception as e:
55+
return {"status": False, "error": str(e)}
56+
57+
3058
def _ipapi_ipv4(geo: bool=False) -> Dict[str, Union[bool, Dict[str, Union[str, float]], str]]:
3159
"""
3260
Get public IP and geolocation using ip-api.com.
@@ -98,6 +126,7 @@ def get_public_ipv4(api: IPv4API=IPv4API.AUTO,
98126
api_map = {
99127
IPv4API.IPAPI: _ipapi_ipv4,
100128
IPv4API.IPINFO: _ipinfo_ipv4,
129+
IPv4API.IPSB: _ipsb_ipv4
101130
}
102131

103132
if api == IPv4API.AUTO:

ipspot/params.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
Designed with simplicity and modularity in mind, IPSpot offers quick IP and geolocation lookups directly from your machine.
1111
'''
1212

13+
REQUEST_HEADERS = {
14+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
15+
"AppleWebKit/537.36 (KHTML, like Gecko) "
16+
"Chrome/123.0.0.0 Safari/537.36"
17+
}
18+
1319
IPSPOT_REPO = "Repo : https://github.com/openscilab/ipspot"
1420

1521

@@ -19,6 +25,7 @@ class IPv4API(Enum):
1925
AUTO = "auto"
2026
IPAPI = "ipapi"
2127
IPINFO = "ipinfo"
28+
IPSB = "ipsb"
2229

2330

2431
PARAMETERS_NAME_MAP = {

0 commit comments

Comments
 (0)