from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
from urllib.request import urlretrieve
+from urllib.error import HTTPError
from sqlite3 import connect as sql_connect, Cursor, Row
from jinja2 import Template
from mpv import MPV # type: ignore
YT_DL_PARAMS = {'paths': {'home': PATH_DIR_DOWNLOADS,
'temp': NAME_DIR_TEMP},
'format': YT_DOWNLOAD_FORMAT}
+THUMBNAIL_URL_PREFIX = PathStr('https://i.ytimg.com/vi/')
+THUMBNAIL_URL_SUFFIX = PathStr('/default.jpg')
QUOTA_COST_YOUTUBE_SEARCH = QuotaCost(100)
QUOTA_COST_YOUTUBE_DETAILS = QuotaCost(1)
def _send_thumbnail(self, filename: PathStr) -> None:
path_thumbnail = path_join(PATH_DIR_THUMBNAILS, filename)
if not path_exists(path_thumbnail):
- raise NotFoundException
+ video_id = splitext(filename)[0]
+ url = f'{THUMBNAIL_URL_PREFIX}{video_id}{THUMBNAIL_URL_SUFFIX}'
+ try:
+ urlretrieve(url,
+ path_join(PATH_DIR_THUMBNAILS, f'{video_id}.jpg'))
+ except HTTPError as e:
+ if 404 == e.code:
+ raise NotFoundException from e
+ raise e
with open(path_thumbnail, 'rb') as f:
img = f.read()
self._send_http(img, [('Content-type', 'image/jpg')])