sheerid-verify / web /api /ipinfo.py
phonglanvq002's picture
Upload folder using huggingface_hub
4ac818f verified
"""IP Info API"""
import httpx
from fastapi import APIRouter
router = APIRouter()
@router.get("/")
async def get_ip_info():
"""Get IP information from ip-api.com"""
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get("http://ip-api.com/json/?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query,proxy,hosting,mobile")
data = response.json()
if data.get("status") != "success":
return {"success": False, "message": data.get("message", "Failed to get IP info")}
# Determine IP type
is_hosting = data.get("hosting", False)
is_proxy = data.get("proxy", False)
is_mobile = data.get("mobile", False)
if is_hosting:
ip_type = "datacenter"
ip_type_label = "Data Center (Server/Cloud)"
ip_type_icon = "⚠️"
elif is_proxy:
ip_type = "proxy"
ip_type_label = "Proxy/VPN"
ip_type_icon = "⚠️"
elif is_mobile:
ip_type = "mobile"
ip_type_label = "Mobile Network (3G/4G/5G)"
ip_type_icon = "📱"
else:
ip_type = "residential"
ip_type_label = "Residential (Home Network)"
ip_type_icon = "🏠"
return {
"success": True,
"ip": data.get("query"),
"country": data.get("country"),
"country_code": data.get("countryCode"),
"region": data.get("regionName"),
"city": data.get("city"),
"isp": data.get("isp"),
"org": data.get("org"),
"timezone": data.get("timezone"),
"lat": data.get("lat"),
"lon": data.get("lon"),
"ip_type": ip_type,
"ip_type_label": ip_type_label,
"ip_type_icon": ip_type_icon,
"is_hosting": is_hosting,
"is_proxy": is_proxy,
"is_mobile": is_mobile
}
except Exception as e:
return {"success": False, "message": str(e)}