home · contact · privacy
Add /videos listing of downloaded videos.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 11 Nov 2024 03:35:47 +0000 (04:35 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 11 Nov 2024 03:35:47 +0000 (04:35 +0100)
templates/videos.tmpl [new file with mode: 0644]
ytplom.py

diff --git a/templates/videos.tmpl b/templates/videos.tmpl
new file mode 100644 (file)
index 0000000..f1cac55
--- /dev/null
@@ -0,0 +1,11 @@
+<html>
+<meta charset="UTF-8">
+<body>
+<p>downloaded videos:</p>
+<ul>
+{% for video_id, path in videos %}
+<li><a href="/dl/{{video_id}}">{{ path }}</a>
+{% endfor %}
+</ul>
+</body>
+</html>
index 7dcdfabec68a7c780ecc61cc6135ff164c931c9e..dac6f7d60cfd8a52ea3206fc22be7042e52490e0 100755 (executable)
--- 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()