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
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,
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 /
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:
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()