home · contact · privacy
Simplify downloads handling, remove dead code.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 11 Oct 2025 03:44:22 +0000 (05:44 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 11 Oct 2025 03:44:22 +0000 (05:44 +0200)
src/ytplom/http.py
src/ytplom/misc.py

index ff66c5daa76c8a6b0b29f0823cad5f01869e0864..b867914073d557f78eb67ef80d345c2c5bccce91 100644 (file)
@@ -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,
index 411013892979dfbb886d27ef5c3033d1285fee1b..b59ea7f34e1536b127ae885ba0e8e75cbd3921b9 100644 (file)
@@ -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: