diff --git a/media_manager/main.py b/media_manager/main.py index 3ad57c2..8c37486 100644 --- a/media_manager/main.py +++ b/media_manager/main.py @@ -51,7 +51,9 @@ import media_manager.tv.router as tv_router from media_manager.tv.service import ( auto_download_all_approved_season_requests, import_all_torrents, + update_all_non_ended_shows_metadata, ) + from media_manager.config import BasicConfig import media_manager.torrent.router as torrent_router @@ -77,14 +79,20 @@ else: def hourly_tasks(): - log.info(f"Tasks are running at {datetime.now()}") + log.info(f"Hourly tasks are running at {datetime.now()}") auto_download_all_approved_season_requests() import_all_torrents() +def weekly_tasks(): + log.info(f"Weekly tasks are running at {datetime.now()}") + update_all_non_ended_shows_metadata() + scheduler = BackgroundScheduler() trigger = CronTrigger(minute=0, hour="*") +weekly_trigger = CronTrigger(day_of_week="mon", hour=0, minute=0, jitter=60*60*24*2) scheduler.add_job(hourly_tasks, trigger) +scheduler.add_job(weekly_tasks, trigger) scheduler.start() diff --git a/media_manager/tv/service.py b/media_manager/tv/service.py index 15a236a..3b52fb8 100644 --- a/media_manager/tv/service.py +++ b/media_manager/tv/service.py @@ -707,6 +707,7 @@ def auto_download_all_approved_season_requests() -> None: ) log.info(f"Auto downloaded {count} approved season requests") + db.commit() db.close() @@ -734,3 +735,33 @@ def import_all_torrents() -> None: continue imported_torrents.append(tv_service.import_torrent_files(torrent=t, show=show)) log.info("Finished importing all torrents") + db.commit() + db.close() + + +def update_all_non_ended_shows_metadata() -> None: + """ + Updates the metadata of all non-ended shows. + """ + db: Session = SessionLocal() + tv_repository = TvRepository(db=db) + tv_service = TvService( + tv_repository=tv_repository, + torrent_service=TorrentService(torrent_repository=TorrentRepository(db=db)), + indexer_service=IndexerService(indexer_repository=IndexerRepository(db=db)), + ) + + log.info("Updating metadata for all non-ended shows") + + shows = [show for show in tv_repository.get_shows() if not show.ended] + + log.info(f"Found {len(shows)} non-ended shows to update") + + for show in shows: + updated_show = tv_service.update_show_metadata(db_show=show) + if updated_show: + log.info(f"Successfully updated metadata for show: {updated_show.name}") + else: + log.warning(f"Failed to update metadata for show: {show.name}") + db.commit() + db.close() \ No newline at end of file