From: Christian Heller Date: Mon, 11 Nov 2024 03:35:47 +0000 (+0100) Subject: Add /videos listing of downloaded videos. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/copy_free?a=commitdiff_plain;h=4e0ab8a5fcf31d666025ab47ab814a56a07aabde;p=ytplom Add /videos listing of downloaded videos. --- diff --git a/templates/videos.tmpl b/templates/videos.tmpl new file mode 100644 index 0000000..f1cac55 --- /dev/null +++ b/templates/videos.tmpl @@ -0,0 +1,11 @@ + + + +

downloaded videos:

+ + + diff --git a/ytplom.py b/ytplom.py index 7dcdfab..dac6f7d 100755 --- a/ytplom.py +++ b/ytplom.py @@ -26,7 +26,8 @@ QueryData: TypeAlias = dict[str, str | int | list[Result]] QuotaLog: TypeAlias = dict[DatetimeStr, QuotaCost] Header: TypeAlias = tuple[str, str] DownloadsDB = dict[VideoId, FilePathStr] -TemplateContext = dict[str, int | str | QueryData | list[QueryData]] +TemplateContext = dict[str, int | str | QueryData | list[QueryData] | + list[tuple[VideoId, FilePathStr]]] API_KEY = environ.get('GOOGLE_API_KEY') HTTP_PORT = 8083 @@ -39,6 +40,7 @@ PATH_DIR_TEMPLATES = FilePathStr('templates') NAME_DIR_TEMP = FilePathStr('temp') NAME_TEMPLATE_INDEX = FilePathStr('index.tmpl') NAME_TEMPLATE_RESULTS = FilePathStr('results.tmpl') +NAME_TEMPLATE_VIDEOS = FilePathStr('videos.tmpl') PATH_DIR_TEMP = FilePathStr(path_join(PATH_DIR_DOWNLOADS, NAME_DIR_TEMP)) EXPECTED_DIRS = [PATH_DIR_DOWNLOADS, PATH_DIR_TEMP, PATH_DIR_THUMBNAILS, @@ -211,6 +213,8 @@ class TaskHandler(BaseHTTPRequestHandler): if 'dl' == page_name: self._send_or_download_video(VideoId(toks_url[2]), parse_qs(url.query)) + if 'videos' == page_name: + self._send_videos_index() if 'query' == page_name: self._send_query_page(QueryId(toks_url[2])) else: # e.g. for / @@ -229,12 +233,15 @@ class TaskHandler(BaseHTTPRequestHandler): self._send_http(bytes(html, 'utf8')) @staticmethod - def _make_template_context() -> TemplateContext: + def _make_template_context(with_quota_count: bool = True + ) -> TemplateContext: """Create template context dictionary with current quota count.""" - quota_count = 0 - tmpl_ctx: TemplateContext = {'quota_count': quota_count} - for amount in read_quota_log().values(): - quota_count += amount + tmpl_ctx: TemplateContext = {} + if with_quota_count: + quota_count = 0 + tmpl_ctx['quota_count'] = quota_count + for amount in read_quota_log().values(): + quota_count += amount return tmpl_ctx def _make_downloads_db(self) -> DownloadsDB: @@ -329,6 +336,16 @@ class TaskHandler(BaseHTTPRequestHandler): tmpl_ctx['queries'] = queries self._send_rendered_template(NAME_TEMPLATE_INDEX, tmpl_ctx) + def _send_videos_index(self) -> None: + """Send listing of downloaded videos, linked to their /dl pages.""" + downloads_db = self._make_downloads_db() + tmpl_ctx = self._make_template_context(with_quota_count=False) + videos = [(id_, FilePathStr(basename(path))) + for id_, path in downloads_db.items()] + videos.sort(key=lambda t: t[1]) + tmpl_ctx['videos'] = videos + self._send_rendered_template(NAME_TEMPLATE_VIDEOS, tmpl_ctx) + if __name__ == '__main__': ensure_expected_dirs_and_files()