This commit is contained in:
@@ -0,0 +1 @@
|
||||
# This file is intentionally left empty to make the directory a Python package
|
||||
@@ -0,0 +1,117 @@
|
||||
from fastapi import APIRouter, HTTPException, Header
|
||||
from typing import Optional
|
||||
from urllib.parse import unquote
|
||||
from app.config import API_KEY
|
||||
from app.database import get_cached_data, save_to_cache
|
||||
from app.services.browser import (
|
||||
visit_url_service,
|
||||
extract_seo_service,
|
||||
extract_meta_tags_service,
|
||||
detect_pagination_service
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/")
|
||||
async def visit_url(url: str, x_api_key: Optional[str] = Header(None)):
|
||||
# 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
|
||||
cached_result = get_cached_data(decoded_url, "visit")
|
||||
if cached_result:
|
||||
return cached_result
|
||||
|
||||
try:
|
||||
# Call the service function
|
||||
result = await visit_url_service(decoded_url)
|
||||
|
||||
# Save to cache
|
||||
save_to_cache(decoded_url, "visit", result)
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"Error visiting URL {decoded_url}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.get("/seo")
|
||||
async def extract_seo(url: str, x_api_key: Optional[str] = Header(None)):
|
||||
"""Extract SEO information 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
|
||||
cached_result = get_cached_data(decoded_url, "seo")
|
||||
if cached_result:
|
||||
return cached_result
|
||||
|
||||
try:
|
||||
# Call the service function
|
||||
result = await extract_seo_service(decoded_url)
|
||||
|
||||
# Save to cache
|
||||
save_to_cache(decoded_url, "seo", result)
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.get("/meta")
|
||||
async def extract_meta_tags(url: str, x_api_key: Optional[str] = Header(None)):
|
||||
"""Extract meta tags 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
|
||||
cached_result = get_cached_data(decoded_url, "meta")
|
||||
if cached_result:
|
||||
return cached_result
|
||||
|
||||
try:
|
||||
# Call the service function
|
||||
result = await extract_meta_tags_service(decoded_url)
|
||||
|
||||
# Save to cache
|
||||
save_to_cache(decoded_url, "meta", result)
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.get("/pagination")
|
||||
async def detect_pagination(url: str, x_api_key: Optional[str] = Header(None)):
|
||||
"""Detect pagination on a website and determine the pagination pattern"""
|
||||
# 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
|
||||
cached_result = get_cached_data(decoded_url, "pagination")
|
||||
if cached_result:
|
||||
return cached_result
|
||||
|
||||
try:
|
||||
# Call the service function
|
||||
result = await detect_pagination_service(decoded_url)
|
||||
|
||||
# Save to cache
|
||||
save_to_cache(decoded_url, "pagination", result)
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
@@ -0,0 +1,24 @@
|
||||
from fastapi import APIRouter, HTTPException, Header
|
||||
from typing import Optional
|
||||
from app.config import API_KEY
|
||||
from app.services.cache import clear_cache, get_cache_stats
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/cache/clear")
|
||||
async def clear_cache_route(x_api_key: Optional[str] = Header(None)):
|
||||
"""Clear the entire cache database"""
|
||||
# Validate API key
|
||||
if not x_api_key or x_api_key != API_KEY:
|
||||
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||
|
||||
return clear_cache()
|
||||
|
||||
@router.get("/cache/stats")
|
||||
async def cache_stats_route(x_api_key: Optional[str] = Header(None)):
|
||||
"""Get cache statistics"""
|
||||
# Validate API key
|
||||
if not x_api_key or x_api_key != API_KEY:
|
||||
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||
|
||||
return get_cache_stats()
|
||||
@@ -0,0 +1,7 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.head("/")
|
||||
async def health_check():
|
||||
return {"status": "ok"}
|
||||
Reference in New Issue
Block a user