alt-scraper-api / app.py
ubuntu593's picture
Update app.py
96d4b52 verified
Raw History Blame Contribute Delete
1.04 kB
import os
from fastapi import FastAPI, Header, HTTPException
# Securely load the key from Hugging Face environment variables, with a default fallback key
SECRET_KEY = os.getenv("AUTOALT_SECRET_KEY")
try:
from api import app, verify_key, run_scraper
except ImportError:
app = FastAPI()
@app.get("/scrape")
@app.get("/api/scrape")
def run_scraper(x_autoalt_key: str = Header(None, alias="X-AutoAlt-Key")):
# Block if the header is missing
if not x_autoalt_key:
raise HTTPException(status_code=401, detail="Unauthorized: X-AutoAlt-Key header is missing")
# Block if the header value does not match your secret hash
if x_autoalt_key != SECRET_KEY:
raise HTTPException(status_code=403, detail="Forbidden: Invalid X-AutoAlt-Key")
# If successful, return the scraped data
return {"status": "success", "data": "Scraped data goes here"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)