From 04a559e47163907934d4d19818e3a3dcfd202604 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 15 Mar 2023 17:39:37 +0700 Subject: [PATCH] refactor manual import task --- tubearchivist/home/src/index/filesystem.py | 24 ++++++++++++++++++++-- tubearchivist/home/tasks.py | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tubearchivist/home/src/index/filesystem.py b/tubearchivist/home/src/index/filesystem.py index 5f496ae0..c33deefe 100644 --- a/tubearchivist/home/src/index/filesystem.py +++ b/tubearchivist/home/src/index/filesystem.py @@ -178,11 +178,15 @@ class ImportFolderScanner: "subtitle": [".vtt"], } - def __init__(self): + def __init__(self, task=False): + self.task = task self.to_import = False def scan(self): """scan and match media files""" + if self.task: + self.task.send_progress(["Scanning your import folder."]) + all_files = self.get_all_files() self.match_files(all_files) self.process_videos() @@ -261,11 +265,14 @@ class ImportFolderScanner: def process_videos(self): """loop through all videos""" - for current_video in self.to_import: + for idx, current_video in enumerate(self.to_import): if not current_video["media"]: print(f"{current_video}: no matching media file found.") raise ValueError + if self.task: + self._notify(idx, current_video) + self._detect_youtube_id(current_video) self._dump_thumb(current_video) self._convert_thumb(current_video) @@ -275,6 +282,19 @@ class ImportFolderScanner: ManualImport(current_video, self.CONFIG).run() + def _notify(self, idx, current_video): + """send notification back to task""" + filename = os.path.split(current_video["media"])[-1] + if len(filename) > 50: + filename = filename[:50] + "..." + + message = [ + f"Import queue processing video {idx + 1}/{len(self.to_import)}", + filename, + ] + progress = (idx + 1) / len(self.to_import) + self.task.send_progress(message, progress=progress) + def _detect_youtube_id(self, current_video): """find video id from filename or json""" youtube_id = self._extract_id_from_filename(current_video["media"]) diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 92759e24..5c62429d 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -211,7 +211,7 @@ def check_reindex(self, data=False, extract_videos=False): Reindex(task=self).reindex_all() -@shared_task(bind=True, name="manual_import") +@shared_task(bind=True, name="manual_import", base=BaseTask) def run_manual_import(self): """called from settings page, to go through import folder""" manager = TaskManager() @@ -220,7 +220,7 @@ def run_manual_import(self): return manager.init(self) - ImportFolderScanner().scan() + ImportFolderScanner(task=self).scan() @shared_task(bind=True, name="run_backup")