removed useless var
This commit is contained in:
@@ -9,26 +9,21 @@ from logger import get_app_logger
|
|||||||
|
|
||||||
app_logger = get_app_logger()
|
app_logger = get_app_logger()
|
||||||
|
|
||||||
# Cache for IP geolocation data to avoid repeated API calls
|
|
||||||
_geoloc_cache = {}
|
|
||||||
|
|
||||||
|
|
||||||
def fetch_ip_geolocation(ip_address: str) -> Optional[Dict[str, Any]]:
|
def fetch_ip_geolocation(ip_address: str) -> Optional[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Fetch geolocation data for an IP address using ip-api.com.
|
Fetch geolocation data for an IP address using ip-api.com.
|
||||||
|
|
||||||
|
Results are persisted to the database by the caller (fetch_ip_rep task),
|
||||||
|
so no in-memory caching is needed.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
ip_address: IP address to lookup
|
ip_address: IP address to lookup
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dictionary containing geolocation data or None if lookup fails
|
Dictionary containing geolocation data or None if lookup fails
|
||||||
"""
|
"""
|
||||||
# Check cache first
|
|
||||||
if ip_address in _geoloc_cache:
|
|
||||||
return _geoloc_cache[ip_address]
|
|
||||||
# This is now replacing lcrawl to fetch IP data like latitude/longitude, city, etc...
|
|
||||||
try:
|
try:
|
||||||
# Use ip-api.com API for geolocation
|
|
||||||
url = f"http://ip-api.com/json/{ip_address}"
|
url = f"http://ip-api.com/json/{ip_address}"
|
||||||
params = {
|
params = {
|
||||||
"fields": "status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,reverse,mobile,proxy,hosting,query"
|
"fields": "status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,reverse,mobile,proxy,hosting,query"
|
||||||
@@ -39,16 +34,12 @@ def fetch_ip_geolocation(ip_address: str) -> Optional[Dict[str, Any]]:
|
|||||||
|
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
# Check if the API call was successful
|
|
||||||
if data.get("status") != "success":
|
if data.get("status") != "success":
|
||||||
app_logger.warning(
|
app_logger.warning(
|
||||||
f"IP lookup failed for {ip_address}: {data.get('message')}"
|
f"IP lookup failed for {ip_address}: {data.get('message')}"
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Cache the result
|
|
||||||
_geoloc_cache[ip_address] = data
|
|
||||||
|
|
||||||
app_logger.debug(f"Fetched geolocation for {ip_address}")
|
app_logger.debug(f"Fetched geolocation for {ip_address}")
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user