From: Christian Heller Date: Mon, 11 Nov 2024 07:49:21 +0000 (+0100) Subject: Minor formal fixes. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/day?a=commitdiff_plain;h=2d29e133746955b5cc55e3d69174d34090980963;p=ytplom Minor formal fixes. --- diff --git a/ytplom.py b/ytplom.py index 37c73ef..0a95119 100755 --- a/ytplom.py +++ b/ytplom.py @@ -19,7 +19,7 @@ import googleapiclient.discovery # type: ignore DatetimeStr = NewType('DatetimeStr', str) QuotaCost = NewType('QuotaCost', int) VideoId = NewType('VideoId', str) -FilePathStr = NewType('FilePathStr', str) +PathStr = NewType('PathStr', str) QueryId = NewType('QueryId', str) QueryText = NewType('QueryText', str) AmountDownloads = NewType('AmountDownloads', int) @@ -29,33 +29,33 @@ VideoData: TypeAlias = dict[str, str | bool] QueryData: TypeAlias = dict[str, QueryId | QueryText | DatetimeStr | AmountDownloads | list[Result]] QuotaLog: TypeAlias = dict[DatetimeStr, QuotaCost] -DownloadsDB = dict[VideoId, FilePathStr] -TemplateContext = dict[str, str | QuotaCost | QueryData | VideoData | - list[QueryData] | list[VideoData] | - list[tuple[VideoId, FilePathStr]] | - list[tuple[QueryId, QueryText]]] +DownloadsDB = dict[VideoId, PathStr] +TemplateContext = dict[str, PathStr | VideoId | QuotaCost | QueryData + | VideoData | list[QueryData] | list[VideoData] + | list[tuple[VideoId, PathStr]] + | list[tuple[QueryId, QueryText]]] API_KEY = environ.get('GOOGLE_API_KEY') HTTP_PORT = 8083 -PATH_QUOTA_LOG = FilePathStr('quota_log.json') -PATH_DIR_DOWNLOADS = FilePathStr('downloads') -PATH_DIR_THUMBNAILS = FilePathStr('thumbnails') -PATH_DIR_REQUESTS_CACHE = FilePathStr('cache_googleapi') -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') -NAME_TEMPLATE_VIDEO_ABOUT = FilePathStr('video_about.tmpl') - -PATH_DIR_TEMP = FilePathStr(path_join(PATH_DIR_DOWNLOADS, NAME_DIR_TEMP)) +PATH_QUOTA_LOG = PathStr('quota_log.json') +PATH_DIR_DOWNLOADS = PathStr('downloads') +PATH_DIR_THUMBNAILS = PathStr('thumbnails') +PATH_DIR_REQUESTS_CACHE = PathStr('cache_googleapi') +PATH_DIR_TEMPLATES = PathStr('templates') +NAME_DIR_TEMP = PathStr('temp') +NAME_TEMPLATE_INDEX = PathStr('index.tmpl') +NAME_TEMPLATE_RESULTS = PathStr('results.tmpl') +NAME_TEMPLATE_VIDEOS = PathStr('videos.tmpl') +NAME_TEMPLATE_VIDEO_ABOUT = PathStr('video_about.tmpl') + +PATH_DIR_TEMP = PathStr(path_join(PATH_DIR_DOWNLOADS, NAME_DIR_TEMP)) EXPECTED_DIRS = [PATH_DIR_DOWNLOADS, PATH_DIR_TEMP, PATH_DIR_THUMBNAILS, PATH_DIR_REQUESTS_CACHE] -PATH_TEMPLATE_INDEX = FilePathStr(path_join(PATH_DIR_TEMPLATES, - NAME_TEMPLATE_INDEX)) +PATH_TEMPLATE_INDEX = PathStr(path_join(PATH_DIR_TEMPLATES, + NAME_TEMPLATE_INDEX)) TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f' -YOUTUBE_URL_PREFIX = 'https://www.youtube.com/watch?v=' +YOUTUBE_URL_PREFIX = PathStr('https://www.youtube.com/watch?v=') YT_DOWNLOAD_FORMAT = 'bestvideo[height<=1080][width<=1920]+bestaudio'\ '/best[height<=1080][width<=1920]' YT_DL_PARAMS = {'paths': {'home': PATH_DIR_DOWNLOADS, @@ -215,7 +215,7 @@ class TaskHandler(BaseHTTPRequestHandler): toks_url: list[str] = url.path.split('/') page_name = toks_url[1] if 'thumbnails' == page_name: - self._send_thumbnail(FilePathStr(toks_url[2])) + self._send_thumbnail(PathStr(toks_url[2])) elif 'dl' == page_name: self._send_or_download_video(VideoId(toks_url[2])) elif 'videos' == page_name: @@ -228,7 +228,7 @@ class TaskHandler(BaseHTTPRequestHandler): self._send_queries_index_and_search() def _send_rendered_template(self, - tmpl_name: FilePathStr, + tmpl_name: PathStr, tmpl_ctx: TemplateContext ) -> None: with open(path_join(PATH_DIR_TEMPLATES, tmpl_name), @@ -243,7 +243,7 @@ class TaskHandler(BaseHTTPRequestHandler): 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_] = FilePathStr(e.path) + downloads_db[id_] = PathStr(e.path) return downloads_db def _harvest_queries(self) -> list[tuple[QueryId, list[Result], @@ -295,10 +295,10 @@ class TaskHandler(BaseHTTPRequestHandler): 'available': result['id'] in downloads_db, 'duration': reformat_duration(result['duration']), 'title': result['title'], - 'definition': result['definition'] + 'definition': result['definition'].upper() } - def _send_thumbnail(self, filename: FilePathStr) -> None: + def _send_thumbnail(self, filename: PathStr) -> None: with open(path_join(PATH_DIR_THUMBNAILS, filename), 'rb') as f: img = f.read() self._send_http(img, [('Content-type', 'image/jpg')]) @@ -357,7 +357,7 @@ class TaskHandler(BaseHTTPRequestHandler): first_result, self._make_downloads_db())}) def _send_videos_index(self) -> None: - videos = [(id_, FilePathStr(basename(path))) + videos = [(id_, PathStr(basename(path))) for id_, path in self._make_downloads_db().items()] videos.sort(key=lambda t: t[1]) self._send_rendered_template(NAME_TEMPLATE_VIDEOS, {'videos': videos})