This commit is contained in:
@@ -9,7 +9,6 @@ from app.services.browser import (
|
||||
extract_meta_tags_service,
|
||||
detect_pagination_service,
|
||||
capture_outgoing_calls_service,
|
||||
fetch_json_service,
|
||||
get_resulting_url_service
|
||||
)
|
||||
|
||||
@@ -156,34 +155,6 @@ async def capture_outgoing_calls(url: str, skipCache: bool = False, x_api_key: O
|
||||
print(f"Error capturing outgoing calls for URL {decoded_url}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.get("/json")
|
||||
async def fetch_json(url: str, skipCache: bool = False, x_api_key: Optional[str] = Header(None)):
|
||||
"""Fetch JSON content from a website"""
|
||||
# Validate API key
|
||||
if not x_api_key or x_api_key != API_KEY:
|
||||
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||
|
||||
# Decode URL if it's encoded
|
||||
decoded_url = unquote(url)
|
||||
|
||||
# Check cache first (unless skipCache is True)
|
||||
if not skipCache:
|
||||
cached_result = get_cached_data(decoded_url, "json")
|
||||
if cached_result:
|
||||
return cached_result
|
||||
|
||||
try:
|
||||
# Call the service function
|
||||
result = await fetch_json_service(decoded_url)
|
||||
|
||||
# Save to cache
|
||||
if(result["status"] == "success"):
|
||||
save_to_cache(decoded_url, "json", result)
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"Error fetching JSON for URL {decoded_url}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.get("/resulting-url")
|
||||
async def get_resulting_url(url: str, skipCache: bool = False, x_api_key: Optional[str] = Header(None)):
|
||||
|
||||
@@ -895,57 +895,6 @@ async def capture_outgoing_calls_service(decoded_url):
|
||||
|
||||
# Perform the operation
|
||||
return await safe_browser_operation(decoded_url, outgoing_calls_operation)
|
||||
|
||||
async def fetch_json_service(decoded_url):
|
||||
"""Service function to fetch JSON content from a website"""
|
||||
print(f"Fetching JSON from: {decoded_url}")
|
||||
|
||||
# Define the operation to perform with the browser
|
||||
async def json_operation(page):
|
||||
try:
|
||||
response = await page.goto(decoded_url, wait_until='networkidle', timeout=30000)
|
||||
if not response:
|
||||
print(f"Warning: No response object returned for {decoded_url}")
|
||||
|
||||
# Get the final URL after any redirects and decode it
|
||||
final_url = page.url
|
||||
decoded_final_url = unquote(final_url)
|
||||
|
||||
# Try to get JSON content
|
||||
try:
|
||||
content = await page.content()
|
||||
# Check if the page contains JSON
|
||||
if content.strip().startswith('{') or content.strip().startswith('['):
|
||||
return {
|
||||
"status": "success",
|
||||
"url": decoded_url,
|
||||
"final_url": decoded_final_url,
|
||||
"content": content,
|
||||
"content_type": "json"
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"status": "success",
|
||||
"url": decoded_url,
|
||||
"final_url": decoded_final_url,
|
||||
"content": content,
|
||||
"content_type": "html"
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"status": "partial",
|
||||
"url": decoded_url,
|
||||
"final_url": decoded_final_url,
|
||||
"error": f"Could not get content: {str(e)}"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error during JSON fetch: {e}")
|
||||
return {"status": "error", "url": decoded_url, "error": str(e)}
|
||||
|
||||
# Perform the operation
|
||||
return await safe_browser_operation(decoded_url, json_operation)
|
||||
|
||||
async def get_resulting_url_service(decoded_url):
|
||||
"""Service function to get the resulting URL after navigation (handles redirects)"""
|
||||
print(f"Getting resulting URL for: {decoded_url}")
|
||||
|
||||
Reference in New Issue
Block a user