This commit is contained in:
Hossein 2025-08-07 01:50:31 +03:30
parent 3b3e9823ae
commit 8f60e9bcdf

14
main.py
View File

@ -1,4 +1,4 @@
from fastapi import FastAPI, HTTPException, Depends from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel from pydantic import BaseModel
from typing import List, Optional from typing import List, Optional
@ -9,6 +9,9 @@ from database import get_db, create_tables, Item as ItemModel
from schemas import ScrapeRequest, ScrapedPost from schemas import ScrapeRequest, ScrapedPost
from scraper import scrape_and_store from scraper import scrape_and_store
# API Key for Django backend authentication
API_KEY = os.getenv("ENGINE_API_KEY")
app = FastAPI( app = FastAPI(
title=os.getenv("FASTAPI_TITLE", "FastAPI ISS Project"), title=os.getenv("FASTAPI_TITLE", "FastAPI ISS Project"),
description=os.getenv("FASTAPI_DESCRIPTION", "A FastAPI project with Docker and PostgreSQL support"), description=os.getenv("FASTAPI_DESCRIPTION", "A FastAPI project with Docker and PostgreSQL support"),
@ -99,8 +102,15 @@ async def delete_item(item_id: int, db: Session = Depends(get_db)):
# ============================================================================ # ============================================================================
@app.post("/scrape/", response_model=List[ScrapedPost]) @app.post("/scrape/", response_model=List[ScrapedPost])
async def scrape_posts(request: ScrapeRequest, db: Session = Depends(get_db)): async def scrape_posts(
request: ScrapeRequest,
x_api_key: str = Header(...),
db: Session = Depends(get_db)
):
"""Scrape Instagram posts for a given username""" """Scrape Instagram posts for a given username"""
if x_api_key != API_KEY:
raise HTTPException(status_code=403, detail="Forbidden: Invalid API key")
try: try:
return scrape_and_store(db, request) return scrape_and_store(db, request)
except Exception as e: except Exception as e: