Some checks failed
Test Suite / test (push) Has been cancelled
- Introduced `/api/tmdb` and `/api/collection/missing-locations` endpoints to the backend for improved media management. - Added a new `get_media_by_country` function in the collection API to fetch media items based on country codes. - Updated configuration to allow overriding *arr base URLs via environment variables for better flexibility. - Enhanced frontend with a new `MissingLocations` component and integrated it into the routing structure. - Improved the `CollectionMap` component to handle country selection and display media items accordingly. - Added testing dependencies in `requirements.txt` and updated frontend configuration for testing support.
132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
"""Collection API endpoints"""
|
|
from fastapi import APIRouter, Query, HTTPException
|
|
from typing import List, Optional
|
|
import json
|
|
from app.core.database import init_db, pool as db_pool
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/summary")
|
|
async def get_collection_summary(
|
|
types: Optional[str] = Query(None, description="Comma-separated list: movie,show,music")
|
|
):
|
|
"""
|
|
Get collection summary by country and media type.
|
|
Returns counts per country per media type.
|
|
"""
|
|
# Ensure pool is initialized
|
|
from app.core.database import init_db, pool as db_pool
|
|
await init_db()
|
|
|
|
if db_pool is None:
|
|
from fastapi import HTTPException
|
|
raise HTTPException(status_code=503, detail="Database not available")
|
|
|
|
# Parse types filter
|
|
type_filter = []
|
|
if types:
|
|
type_filter = [t.strip() for t in types.split(",") if t.strip() in ["movie", "show", "music"]]
|
|
|
|
async with db_pool.connection() as conn:
|
|
async with conn.cursor() as cur:
|
|
# Build query
|
|
query = """
|
|
SELECT
|
|
mc.country_code,
|
|
mi.media_type,
|
|
COUNT(*) as count
|
|
FROM moviemap.media_country mc
|
|
JOIN moviemap.media_item mi ON mc.media_item_id = mi.id
|
|
"""
|
|
params = []
|
|
if type_filter:
|
|
query += " WHERE mi.media_type = ANY(%s)"
|
|
params.append(type_filter)
|
|
|
|
query += """
|
|
GROUP BY mc.country_code, mi.media_type
|
|
ORDER BY mc.country_code, mi.media_type
|
|
"""
|
|
|
|
await cur.execute(query, params if params else None)
|
|
rows = await cur.fetchall()
|
|
|
|
# Transform to nested dict structure
|
|
result = {}
|
|
for row in rows:
|
|
country_code, media_type, count = row
|
|
if country_code not in result:
|
|
result[country_code] = {}
|
|
result[country_code][media_type] = count
|
|
|
|
return result
|
|
|
|
|
|
@router.get("/by-country")
|
|
async def get_media_by_country(
|
|
country_code: str = Query(..., description="ISO 3166-1 alpha-2 country code"),
|
|
types: Optional[str] = Query(None, description="Comma-separated list: movie,show,music")
|
|
):
|
|
"""
|
|
Get list of media items for a specific country.
|
|
Returns media items with their details.
|
|
"""
|
|
await init_db()
|
|
if db_pool is None:
|
|
raise HTTPException(status_code=503, detail="Database not available")
|
|
|
|
# Validate country code
|
|
if len(country_code) != 2 or not country_code.isalpha():
|
|
raise HTTPException(status_code=400, detail="Country code must be 2 letters (ISO 3166-1 alpha-2)")
|
|
|
|
country_code = country_code.upper()
|
|
|
|
# Parse types filter
|
|
type_filter = []
|
|
if types:
|
|
type_filter = [t.strip() for t in types.split(",") if t.strip() in ["movie", "show", "music"]]
|
|
|
|
async with db_pool.connection() as conn:
|
|
async with conn.cursor() as cur:
|
|
query = """
|
|
SELECT
|
|
mi.id,
|
|
mi.source_kind,
|
|
mi.source_item_id,
|
|
mi.title,
|
|
mi.year,
|
|
mi.media_type
|
|
FROM moviemap.media_country mc
|
|
JOIN moviemap.media_item mi ON mc.media_item_id = mi.id
|
|
WHERE mc.country_code = %s
|
|
"""
|
|
params = [country_code]
|
|
|
|
if type_filter:
|
|
query += " AND mi.media_type = ANY(%s)"
|
|
params.append(type_filter)
|
|
|
|
query += " ORDER BY mi.title"
|
|
|
|
await cur.execute(query, params)
|
|
rows = await cur.fetchall()
|
|
|
|
items = []
|
|
for row in rows:
|
|
items.append({
|
|
"id": str(row[0]),
|
|
"source_kind": row[1],
|
|
"source_item_id": row[2],
|
|
"title": row[3],
|
|
"year": row[4],
|
|
"media_type": row[5],
|
|
})
|
|
|
|
return {
|
|
"country_code": country_code,
|
|
"count": len(items),
|
|
"items": items
|
|
}
|
|
|