home · contact · privacy
Link playlist entries to file data pages. master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Nov 2024 06:24:17 +0000 (07:24 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Nov 2024 06:24:17 +0000 (07:24 +0100)
src/templates/playlist.tmpl
src/ytplom/misc.py

index c1d084e66b839556fffb3d40705b4109b4c7fbde..d62e7ea7d98f931385150857dd60bf7939b1fbae 100644 (file)
@@ -40,7 +40,7 @@ td.history { width: 50%; }
 <table>
 <tr><td id="status" colspan=2>
 {% if running %}{% if pause %}PAUSED{% else %}PLAYING{% endif %}{% else %}STOPPED{% endif %}:<br />
-{{ current_title }}<br />
+<a href="/{{page_names.file}}/{{current_video.rel_path_b64}}">{{ current_video.basename }}</a><br />
 <form action="/{{page_names.playlist}}" method="POST">
 <input type="submit" name="pause" autofocus value="{% if paused %}resume{% else %}pause{% endif %}">
 <input type="submit" name="prev" value="prev">
@@ -49,11 +49,11 @@ td.history { width: 50%; }
 <input type="submit" name="reload" value="reload">
 </form>
 </td></tr>
-{% for prev_title, next_title in tuples %}
+{% for prev_video, next_video in tuples %}
 <tr><td class="history">
-{{ prev_title }}
+<a href="/{{page_names.file}}/{{prev_video.rel_path_b64}}">{{ prev_video.basename }}</a>
 </td><td class="history">
-{{ next_title }}
+<a href="/{{page_names.file}}/{{next_video.rel_path_b64}}">{{ next_video.basename }}</a>
 </td></tr>
 {% endfor %}
 </table>
index 06ab2a09e2369f992eb0aa0d5bea6452753f66a5..972ada29a99d0adfa1e3b164c33446d366147c6b 100644 (file)
@@ -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})