5
5
from typing import Union , Dict , Any
6
6
import requests
7
7
from art import tprint
8
- from .params import IPv4API , PARAMETERS_NAME_MAP
8
+ from .params import REQUEST_HEADERS , IPv4API , PARAMETERS_NAME_MAP
9
9
from .params import IPSPOT_OVERVIEW , IPSPOT_REPO , IPSPOT_VERSION
10
10
11
11
@@ -27,6 +27,34 @@ def get_private_ipv4() -> Dict[str, Union[bool, Dict[str, str], str]]:
27
27
return {"status" : False , "error" : str (e )}
28
28
29
29
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
+
30
58
def _ipapi_ipv4 (geo : bool = False ) -> Dict [str , Union [bool , Dict [str , Union [str , float ]], str ]]:
31
59
"""
32
60
Get public IP and geolocation using ip-api.com.
@@ -98,6 +126,7 @@ def get_public_ipv4(api: IPv4API=IPv4API.AUTO,
98
126
api_map = {
99
127
IPv4API .IPAPI : _ipapi_ipv4 ,
100
128
IPv4API .IPINFO : _ipinfo_ipv4 ,
129
+ IPv4API .IPSB : _ipsb_ipv4
101
130
}
102
131
103
132
if api == IPv4API .AUTO :
0 commit comments