From: Christian Heller Date: Wed, 27 Nov 2024 06:24:17 +0000 (+0100) Subject: Link playlist entries to file data pages. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%22https:/validator.w3.org/conditions?a=commitdiff_plain;h=ce3abb7b4ca68f6bfa6365a1a09b10260abcb015;p=ytplom Link playlist entries to file data pages. --- diff --git a/src/templates/playlist.tmpl b/src/templates/playlist.tmpl index c1d084e..d62e7ea 100644 --- a/src/templates/playlist.tmpl +++ b/src/templates/playlist.tmpl @@ -40,7 +40,7 @@ td.history { width: 50%; } -{% for prev_title, next_title in tuples %} +{% for prev_video, next_video in tuples %} {% endfor %}
{% if running %}{% if pause %}PAUSED{% else %}PLAYING{% endif %}{% else %}STOPPED{% endif %}:
-{{ current_title }}
+{{ current_video.basename }}
@@ -49,11 +49,11 @@ td.history { width: 50%; }
-{{ prev_title }} +{{ prev_video.basename }} -{{ next_title }} +{{ next_video.basename }}
diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 06ab2a0..972ada2 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -4,7 +4,7 @@ from typing import TypeAlias, Optional, NewType, Callable, Self, Any from os import chdir, environ, getcwd, makedirs, scandir, remove as os_remove from os.path import (dirname, isdir, isfile, exists as path_exists, - join as path_join, splitext, basename) + join as path_join, normpath, splitext, basename) from base64 import urlsafe_b64encode, urlsafe_b64decode from random import shuffle from time import time, sleep @@ -47,11 +47,12 @@ PlayerUpdateId = NewType('PlayerUpdateId', str) B64Str = NewType('B64Str', str) PageNames: TypeAlias = dict[str, PathStr] DownloadsIndex: TypeAlias = dict[YoutubeId, PathStr] +PlaylistTuples = list[tuple[Optional['VideoFile'], Optional['VideoFile']]] TemplateContext: TypeAlias = dict[ str, None | bool | PlayerUpdateId | Optional[PathStr] | YoutubeId | QueryText | QuotaCost | list[FlagName] | 'VideoFile' | 'YoutubeVideo' | PageNames | list['YoutubeVideo'] | list['YoutubeQuery'] - | list[tuple[B64Str, PathStr]] | list[tuple[PathStr, PathStr]]] + | list[tuple[B64Str, PathStr]] | PlaylistTuples] # major expected directories PATH_HOME = PathStr(environ.get('HOME', '')) @@ -384,7 +385,12 @@ class VideoFile(DbData): @property def full_path(self) -> PathStr: """Return self.rel_path suffixed under PATH_DOWNLOADS.""" - return PathStr(path_join(PATH_DOWNLOADS, self.rel_path)) + return PathStr(normpath(path_join(PATH_DOWNLOADS, self.rel_path))) + + @property + def basename(self) -> PathStr: + """Return basename(self.rel_path).""" + return PathStr(basename(self.rel_path)) @property def present(self) -> bool: @@ -466,10 +472,15 @@ class Player: self._mpv: Optional[MPV] = None def _load_filenames(self) -> None: - self._filenames = [PathStr(e.path) for e in scandir(PATH_DOWNLOADS) - if isfile(e.path) - and splitext(e.path)[1][1:] in LEGAL_EXTENSIONS] - shuffle(self._filenames) + conn = DbConnection() + known_files = {f.full_path: f for f in VideoFile.get_all(conn)} + conn.commit_close() + self._files = [known_files[PathStr(e.path)] + for e in scandir(PATH_DOWNLOADS) + if e.path in known_files + and isfile(e.path) + and splitext(e.path)[1][1:] in LEGAL_EXTENSIONS] + shuffle(self._files) self._idx = 0 @property @@ -503,7 +514,7 @@ class Player: self._mpv = None self._signal_update() - for path in self._filenames: + for path in [f.full_path for f in self._files]: self._mpv.playlist_append(path) self._mpv.playlist_play_index(self._idx) @@ -514,21 +525,21 @@ class Player: self._mpv = None @property - def current_filename(self) -> Optional[PathStr]: - """Return what we assume is the name of the currently playing file.""" - if not self._filenames: + def current_file(self) -> Optional[VideoFile]: + """Return what we assume is the currently playing file.""" + if not self._files: return None - return PathStr(basename(self._filenames[self._idx])) + return self._files[self._idx] @property - def prev_files(self) -> list[PathStr]: + def prev_files(self) -> list[VideoFile]: """List 'past' files of playlist.""" - return list(reversed(self._filenames[:self._idx])) + return list(reversed(self._files[:self._idx])) @property - def next_files(self) -> list[PathStr]: + def next_files(self) -> list[VideoFile]: """List 'coming' files of playlist.""" - return self._filenames[self._idx + 1:] + return self._files[self._idx + 1:] @property def is_running(self) -> bool: @@ -935,15 +946,15 @@ class TaskHandler(BaseHTTPRequestHandler): headers=[('Content-type', 'application/json')]) def _send_playlist(self) -> None: - tuples: list[tuple[PathStr, PathStr]] = [] + tuples: PlaylistTuples = [] i: int = 0 while True: - prev, next_ = PathStr(''), PathStr('') + prev, next_ = None, None if len(self.server.player.prev_files) > i: - prev = PathStr(basename(self.server.player.prev_files[i])) + prev = self.server.player.prev_files[i] if len(self.server.player.next_files) > i: - next_ = PathStr(basename(self.server.player.next_files[i])) - if not prev + next_: + next_ = self.server.player.next_files[i] + if not (prev or next_): break tuples += [(prev, next_)] i += 1 @@ -952,5 +963,5 @@ class TaskHandler(BaseHTTPRequestHandler): {'last_update': self.server.player.last_update, 'running': self.server.player.is_running, 'paused': self.server.player.is_paused, - 'current_title': self.server.player.current_filename, + 'current_video': self.server.player.current_file, 'tuples': tuples})