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)
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)
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})