From c98ae5ffa79b97c65f8992f1d10310ea9bbe996f Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sat, 11 Oct 2025 05:44:22 +0200 Subject: [PATCH] Simplify downloads handling, remove dead code. --- src/ytplom/http.py | 15 +++++++++------ src/ytplom/misc.py | 37 +++++++++++-------------------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/ytplom/http.py b/src/ytplom/http.py index ff66c5d..b867914 100644 --- a/src/ytplom/http.py +++ b/src/ytplom/http.py @@ -310,12 +310,16 @@ class _TaskHandler(PlomHttpHandler): for f in self.server.player.playlist] } if 'download' in subscriptions: + yt_id = YoutubeId(download_id) with DbConn() as conn: - last_update = self.server.downloads.last_update_for( - conn, YoutubeId(download_id)) - if last_updates['download'] < last_update['time']: - last_updates['download'] = last_update['time'] - payload['download'] = last_update + update = self.server.downloads.last_update_for(conn, yt_id) + if last_updates['download'] < update['time']: + last_updates['download'] = update['time'] + if update['status'] == 'present': + file = VideoFile.get_by_yt_id(conn, yt_id) + update['path'] = str(file.rel_path) + update['digest'] = file.digest.b64 + payload['download'] = update if not payload: sleep(_EVENTS_UPDATE_INTERVAL_S) @@ -402,7 +406,6 @@ class _TaskHandler(PlomHttpHandler): self._send_rendered_template( _NAME_TEMPLATE_YT_RESULT, {'video_data': video_data, - 'is_temp': video_id in self.server.downloads.ids_unfinished, 'file_digest': file_digest, 'file_path': file_path, 'queries': linked_queries, diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 4110138..b59ea7f 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -838,7 +838,7 @@ class DownloadsManager: def __init__(self) -> None: self._to_download: list[YoutubeId] = [] ensure_expected_dirs([PATH_DOWNLOADS, PATH_TEMP]) - self._last_updates = {} + self._last_updates: dict[YoutubeId, dict[str, str]] = {} self._sync_db() def _sync_db(self): @@ -864,8 +864,7 @@ class DownloadsManager: yt_id = self._id_from_filename(path) if (yt_id not in self._last_updates or 'present' != self._last_updates[yt_id]['status']): - digest64 = VideoFile.get_by_yt_id(conn, yt_id).digest.b64 - self._update_status(yt_id, 'present', str(path), digest64) + self._update_status(yt_id, 'present') self._files = VideoFile.get_all(conn) chdir(old_cwd) @@ -887,29 +886,13 @@ class DownloadsManager: self._update_status(yt_id, 'absent') return self._last_updates[yt_id] - def _update_status(self, - yt_id: YoutubeId, - status: str, - path: str = '', - digest: str = '' - ) -> None: + def _update_status(self, yt_id: YoutubeId, status: str) -> None: self._last_updates[yt_id] = {'status': status, 'time': _now_string()} - for k, v in [('path', path), ('digest', digest)]: - if k: - self._last_updates[yt_id] |= {k: v} @staticmethod def _id_from_filename(path: Path) -> YoutubeId: return YoutubeId(path.stem.split('[')[-1].split(']')[0]) - @property - def ids_unfinished(self) -> set[YoutubeId]: - """Return set of IDs of videos awaiting or currently in download.""" - in_temp_dir = [] - for path in [p for p in PATH_TEMP.iterdir() if p.is_file()]: - in_temp_dir += [self._id_from_filename(path)] - return set(self._to_download + in_temp_dir) - def clean_unfinished(self) -> None: """Empty temp directory of unfinished downloads.""" for path in [p for p in PATH_TEMP.iterdir() if p.is_file()]: @@ -919,12 +902,14 @@ class DownloadsManager: def queue_download(self, video_id: YoutubeId) -> None: """Add video_id to download queue *if* not already processed.""" self._sync_db() - pre_existing = (self.ids_unfinished - | set(self._to_download - + [f.full_path for f in self._files])) - if video_id not in pre_existing: - self._to_download += [video_id] - self._update_status(video_id, 'queued') + if video_id in self._to_download: + return + for path in ([p for p in PATH_TEMP.iterdir() if p.is_file()] + + [f.full_path for f in self._files]): + if self._id_from_filename(path) == video_id: + return + self._to_download += [video_id] + self._update_status(video_id, 'queued') def _download_next(self) -> None: if self._to_download: -- 2.30.2