home · contact · privacy
Properly handle download options where download is already happening or scheduled.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 15 Nov 2024 05:52:28 +0000 (06:52 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 15 Nov 2024 05:52:28 +0000 (06:52 +0100)
templates/video_about.tmpl
ytplom.py

index 06e0c47d84e5cdd9bc6d7a525bebcd93fdd524f4..358cced6a2c8ce482987fb6de5d8c1c8afe4a4af 100644 (file)
@@ -9,7 +9,7 @@
 <tr><th>duration:</th><td>{{video_data.duration}}</td></tr>
 <tr><th>definition:</th><td>{{video_data.definition}}</td></tr>
 <tr><th>YouTube ID:</th><td>{{video_data.id_}} (<a href="{{youtube_prefix}}{{video_data.id_}}">watch</a>)</td></tr>
-<tr><th>download:</th><td><a href="/dl/{{video_data.id_}}">{{ file_path if file_path else "please do" }}</a></td></tr>
+<tr><th>download:</th><td>{% if is_temp %}working on it{% else %}<a href="/dl/{{video_data.id_}}">{{ file_path if file_path else "please do" }}</a>{% endif %}</td></tr>
 <tr>
 <th>linked queries:</th>
 <td>
index 13e650fd2124d19d012379f65293fa812dc11ee2..f61a415a736336479b102175f906322eb537b104 100755 (executable)
--- a/ytplom.py
+++ b/ytplom.py
@@ -585,13 +585,24 @@ class TaskHandler(BaseHTTPRequestHandler):
         html = tmpl.render(**tmpl_ctx)
         self._send_http(bytes(html, 'utf8'))
 
-    def _make_downloads_db(self) -> DownloadsDb:
+    def _make_downloads_db(self) -> tuple[DownloadsDb, list[VideoId]]:
+
+        def id_from_filename(path: PathStr, double_split: bool = False
+                             ) -> VideoId:
+            before_ext = splitext(path)[0]
+            if double_split:
+                before_ext = splitext(before_ext)[0]
+            return VideoId(before_ext.split('[')[-1].split(']')[0])
+
         downloads_db = {}
-        for e in [e for e in scandir(PATH_DIR_DOWNLOADS) if isfile(e.path)]:
-            before_ext = splitext(e.path)[0]
-            id_ = VideoId(before_ext.split('[')[-1].split(']')[0])
-            downloads_db[id_] = PathStr(e.path)
-        return downloads_db
+        for path in [PathStr(e.path) for e
+                     in scandir(PATH_DIR_DOWNLOADS) if isfile(e.path)]:
+            downloads_db[id_from_filename(path)] = PathStr(path)
+        unfinished = []
+        for path in [PathStr(e.path) for e
+                     in scandir(PATH_DIR_TEMP) if isfile(e.path)]:
+            unfinished += [id_from_filename(path)]
+        return downloads_db, unfinished
 
     def _send_thumbnail(self, filename: PathStr) -> None:
         path_thumbnail = path_join(PATH_DIR_THUMBNAILS, filename)
@@ -602,13 +613,14 @@ class TaskHandler(BaseHTTPRequestHandler):
         self._send_http(img, [('Content-type', 'image/jpg')])
 
     def _send_or_download_video(self, video_id: VideoId) -> None:
-        downloads_db = self._make_downloads_db()
+        downloads_db, unfinished = self._make_downloads_db()
         if video_id in downloads_db:
             with open(downloads_db[video_id], 'rb') as video_file:
                 video = video_file.read()
             self._send_http(content=video)
             return
-        to_download.append(video_id)
+        if video_id not in to_download + unfinished:
+            to_download.append(video_id)
         self._send_http(headers=[('Location', f'/video_about/{video_id}')],
                         code=302)
 
@@ -639,16 +651,18 @@ class TaskHandler(BaseHTTPRequestHandler):
         except NotFoundException:
             video_data = VideoData(video_id)
         conn.commit_close()
+        downloads_db, unfinished = self._make_downloads_db()
         self._send_rendered_template(
                 NAME_TEMPLATE_VIDEO_ABOUT,
                 {'video_data': video_data,
-                 'file_path': self._make_downloads_db().get(video_id, None),
+                 'is_temp': video_id in to_download + unfinished,
+                 'file_path': downloads_db.get(video_id, None),
                  'youtube_prefix': YOUTUBE_URL_PREFIX,
                  'queries': linked_queries})
 
     def _send_videos_index(self) -> None:
         videos = [(id_, PathStr(basename(path)))
-                  for id_, path in self._make_downloads_db().items()]
+                  for id_, path in self._make_downloads_db()[0].items()]
         videos.sort(key=lambda t: t[1])
         self._send_rendered_template(NAME_TEMPLATE_VIDEOS, {'videos': videos})