From: Christian Heller Date: Fri, 15 Nov 2024 05:52:28 +0000 (+0100) Subject: Properly handle download options where download is already happening or scheduled. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/test?a=commitdiff_plain;h=edbee85f6c21beda0927ef2b01ded04c1c85020c;p=ytplom Properly handle download options where download is already happening or scheduled. --- diff --git a/templates/video_about.tmpl b/templates/video_about.tmpl index 06e0c47..358cced 100644 --- a/templates/video_about.tmpl +++ b/templates/video_about.tmpl @@ -9,7 +9,7 @@ duration:{{video_data.duration}} definition:{{video_data.definition}} YouTube ID:{{video_data.id_}} (watch) -download:{{ file_path if file_path else "please do" }} +download:{% if is_temp %}working on it{% else %}{{ file_path if file_path else "please do" }}{% endif %} linked queries: diff --git a/ytplom.py b/ytplom.py index 13e650f..f61a415 100755 --- 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})