This commit is contained in:
@@ -5,7 +5,7 @@ Provides endpoints for the web-based database manipulation interface
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from typing import List, Optional, Dict, Any
|
from typing import List, Optional, Dict, Any
|
||||||
from fastapi import HTTPException, Query
|
from fastapi import HTTPException, Query, BackgroundTasks
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@@ -14,6 +14,10 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||||||
from api.models import *
|
from api.models import *
|
||||||
|
|
||||||
|
|
||||||
|
# Global status tracking for database population
|
||||||
|
_populate_status = {"running": False, "completed": False}
|
||||||
|
|
||||||
|
|
||||||
def map_source_to_description(source: str) -> str:
|
def map_source_to_description(source: str) -> str:
|
||||||
"""Map technical source codes to user-friendly descriptions"""
|
"""Map technical source codes to user-friendly descriptions"""
|
||||||
if not source or source == "no_valid_date_source":
|
if not source or source == "no_valid_date_source":
|
||||||
@@ -1024,6 +1028,111 @@ async def delete_movie(dependencies: dict, imdb_id: str):
|
|||||||
raise HTTPException(status_code=500, detail=f"Failed to delete movie: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"Failed to delete movie: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
async def populate_database(background_tasks: BackgroundTasks, media_type: str = "both", dependencies: dict = None):
|
||||||
|
"""
|
||||||
|
Populate NFOGuard database from Radarr/Sonarr sources
|
||||||
|
|
||||||
|
Args:
|
||||||
|
background_tasks: FastAPI background tasks
|
||||||
|
media_type: Type of media to populate ("movies", "tv", or "both")
|
||||||
|
dependencies: Dictionary with db, radarr_client, sonarr_client
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Status message indicating population has started
|
||||||
|
"""
|
||||||
|
from core.database_populator import DatabasePopulator
|
||||||
|
|
||||||
|
db = dependencies["db"]
|
||||||
|
config = dependencies["config"]
|
||||||
|
|
||||||
|
# Get Radarr and Sonarr clients
|
||||||
|
from clients.radarr_client import RadarrClient
|
||||||
|
from clients.sonarr_client import SonarrClient
|
||||||
|
|
||||||
|
radarr_client = RadarrClient(config)
|
||||||
|
sonarr_client = SonarrClient(config)
|
||||||
|
|
||||||
|
if media_type not in ["both", "movies", "tv"]:
|
||||||
|
raise HTTPException(status_code=400, detail="media_type must be 'both', 'movies', or 'tv'")
|
||||||
|
|
||||||
|
# Create global status tracking
|
||||||
|
populate_status = {
|
||||||
|
"running": True,
|
||||||
|
"media_type": media_type,
|
||||||
|
"start_time": datetime.now().isoformat(),
|
||||||
|
"movies": {"status": "pending", "stats": None},
|
||||||
|
"tv": {"status": "pending", "stats": None},
|
||||||
|
"completed": False,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
# Store status globally so it can be queried
|
||||||
|
global _populate_status
|
||||||
|
_populate_status = populate_status
|
||||||
|
|
||||||
|
async def run_population():
|
||||||
|
"""Background task to populate the database"""
|
||||||
|
try:
|
||||||
|
populator = DatabasePopulator(db, radarr_client, sonarr_client)
|
||||||
|
|
||||||
|
print(f"INFO: Starting database population: {media_type}")
|
||||||
|
|
||||||
|
if media_type == "movies":
|
||||||
|
populate_status["movies"]["status"] = "running"
|
||||||
|
movie_stats = populator.populate_movies()
|
||||||
|
populate_status["movies"]["status"] = "completed"
|
||||||
|
populate_status["movies"]["stats"] = movie_stats
|
||||||
|
print(f"INFO: Movie population completed: {movie_stats}")
|
||||||
|
|
||||||
|
elif media_type == "tv":
|
||||||
|
populate_status["tv"]["status"] = "running"
|
||||||
|
tv_stats = populator.populate_tv_episodes()
|
||||||
|
populate_status["tv"]["status"] = "completed"
|
||||||
|
populate_status["tv"]["stats"] = tv_stats
|
||||||
|
print(f"INFO: TV population completed: {tv_stats}")
|
||||||
|
|
||||||
|
elif media_type == "both":
|
||||||
|
populate_status["movies"]["status"] = "running"
|
||||||
|
movie_stats = populator.populate_movies()
|
||||||
|
populate_status["movies"]["status"] = "completed"
|
||||||
|
populate_status["movies"]["stats"] = movie_stats
|
||||||
|
print(f"INFO: Movie population completed: {movie_stats}")
|
||||||
|
|
||||||
|
populate_status["tv"]["status"] = "running"
|
||||||
|
tv_stats = populator.populate_tv_episodes()
|
||||||
|
populate_status["tv"]["status"] = "completed"
|
||||||
|
populate_status["tv"]["stats"] = tv_stats
|
||||||
|
print(f"INFO: TV population completed: {tv_stats}")
|
||||||
|
|
||||||
|
populate_status["completed"] = True
|
||||||
|
populate_status["running"] = False
|
||||||
|
print("INFO: Database population completed successfully")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERROR: Database population failed: {e}")
|
||||||
|
populate_status["error"] = str(e)
|
||||||
|
populate_status["running"] = False
|
||||||
|
populate_status["completed"] = True
|
||||||
|
|
||||||
|
# Add task to background
|
||||||
|
background_tasks.add_task(run_population)
|
||||||
|
|
||||||
|
print(f"INFO: Database population started for: {media_type}")
|
||||||
|
return {
|
||||||
|
"status": "started",
|
||||||
|
"media_type": media_type,
|
||||||
|
"message": f"Database population started for {media_type}"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def get_populate_status():
|
||||||
|
"""Get the current status of database population"""
|
||||||
|
global _populate_status
|
||||||
|
if '_populate_status' not in globals():
|
||||||
|
return {"running": False, "completed": False}
|
||||||
|
return _populate_status
|
||||||
|
|
||||||
|
|
||||||
def register_web_routes(app, dependencies):
|
def register_web_routes(app, dependencies):
|
||||||
"""Register all web API routes with FastAPI app"""
|
"""Register all web API routes with FastAPI app"""
|
||||||
from fastapi import Request, Response
|
from fastapi import Request, Response
|
||||||
@@ -1148,6 +1257,17 @@ def register_web_routes(app, dependencies):
|
|||||||
response.delete_cookie("nfoguard_session")
|
response.delete_cookie("nfoguard_session")
|
||||||
return {"status": "logged_out", "message": "Session cleared"}
|
return {"status": "logged_out", "message": "Session cleared"}
|
||||||
|
|
||||||
|
# Database population endpoints
|
||||||
|
@app.post("/admin/populate-database")
|
||||||
|
async def api_populate_database(background_tasks: BackgroundTasks, media_type: str = "both"):
|
||||||
|
"""Populate database from Radarr/Sonarr"""
|
||||||
|
return await populate_database(background_tasks, media_type, dependencies)
|
||||||
|
|
||||||
|
@app.get("/api/populate/status")
|
||||||
|
async def api_populate_status():
|
||||||
|
"""Get database population status"""
|
||||||
|
return await get_populate_status()
|
||||||
|
|
||||||
# Health endpoint
|
# Health endpoint
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health_check():
|
async def health_check():
|
||||||
|
|||||||
Reference in New Issue
Block a user