refactor: move sync_series_from_data_files to anime_service
- Moved _sync_series_to_database from fastapi_app.py to anime_service.py - Renamed to sync_series_from_data_files for better clarity - Updated all imports and test references - Removed completed TODO tasks from instructions.md
This commit is contained in:
@@ -361,3 +361,84 @@ class AnimeService:
|
||||
def get_anime_service(series_app: SeriesApp) -> AnimeService:
|
||||
"""Factory used for creating AnimeService with a SeriesApp instance."""
|
||||
return AnimeService(series_app)
|
||||
|
||||
|
||||
async def sync_series_from_data_files(
|
||||
anime_directory: str,
|
||||
logger=None
|
||||
) -> int:
|
||||
"""
|
||||
Sync series from data files to the database.
|
||||
|
||||
Scans the anime directory for data files and adds any new series
|
||||
to the database. Existing series are skipped (no duplicates).
|
||||
|
||||
This function is typically called during application startup to ensure
|
||||
series metadata stored in filesystem data files is available in the
|
||||
database.
|
||||
|
||||
Args:
|
||||
anime_directory: Path to the anime directory with data files
|
||||
logger: Optional logger instance for logging operations.
|
||||
If not provided, uses structlog.
|
||||
|
||||
Returns:
|
||||
Number of new series added to the database
|
||||
"""
|
||||
log = logger or structlog.get_logger(__name__)
|
||||
|
||||
try:
|
||||
from src.core.entities.SerieList import SerieList
|
||||
from src.server.database.connection import get_db_session
|
||||
|
||||
log.info(
|
||||
"Starting data file to database sync",
|
||||
directory=anime_directory
|
||||
)
|
||||
|
||||
# Get all series from data files using SeriesApp
|
||||
series_app = SeriesApp(anime_directory)
|
||||
all_series = await asyncio.to_thread(
|
||||
series_app.get_all_series_from_data_files
|
||||
)
|
||||
|
||||
if not all_series:
|
||||
log.info("No series found in data files to sync")
|
||||
return 0
|
||||
|
||||
log.info(
|
||||
"Found series in data files, syncing to database",
|
||||
count=len(all_series)
|
||||
)
|
||||
|
||||
async with get_db_session() as db:
|
||||
serie_list = SerieList(
|
||||
anime_directory,
|
||||
db_session=db,
|
||||
skip_load=True
|
||||
)
|
||||
added_count = 0
|
||||
for serie in all_series:
|
||||
result = await serie_list.add_to_db(serie, db)
|
||||
if result:
|
||||
added_count += 1
|
||||
log.debug(
|
||||
"Added series to database",
|
||||
name=serie.name,
|
||||
key=serie.key
|
||||
)
|
||||
|
||||
log.info(
|
||||
"Data file sync complete",
|
||||
added=added_count,
|
||||
skipped=len(all_series) - added_count
|
||||
)
|
||||
return added_count
|
||||
|
||||
except Exception as e:
|
||||
log.warning(
|
||||
"Failed to sync series to database",
|
||||
error=str(e),
|
||||
exc_info=True
|
||||
)
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user