home · contact · privacy
Add option for tags whitelist prefilter, i.e. tags of which to show files even if... master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 11 Dec 2024 06:28:10 +0000 (07:28 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 11 Dec 2024 06:28:10 +0000 (07:28 +0100)
src/ytplom/http.py
src/ytplom/misc.py

index 1f0b7037535dcfc57901e7853ab113dbc325a7fc..cc7a90887fb821d2ae53866d3c0acdb7d6b9c373 100644 (file)
@@ -94,7 +94,8 @@ class Server(ThreadingHTTPServer):
                          *args, **kwargs)
         self.config = config
         self.jinja = JinjaEnv(loader=JinjaFSLoader(_PATH_TEMPLATES))
-        self.player = Player(config.needed_tags_prefilter)
+        self.player = Player(config.needed_tags_prefilter,
+                             config.whitelist_tags_prefilter)
         self.downloads = DownloadsManager()
         self.downloads.clean_unfinished()
         self.downloads.start_thread()
@@ -323,12 +324,15 @@ class _TaskHandler(BaseHTTPRequestHandler):
     def _send_files_index(self, params: _ReqMap) -> None:
         filter_path = FilterStr(params.first_for('filter_path'))
         needed_tags_str = params.first_for('needed_tags')
-        needed_tags = (self.server.config.needed_tags_prefilter
-                       | Tag.from_joined(needed_tags_str))
         show_absent = bool(params.first_for('show_absent'))
         with DbConn() as conn:
             files = VideoFile.get_filtered(
-                    conn, filter_path, needed_tags, show_absent)
+                    conn,
+                    filter_path,
+                    self.server.config.needed_tags_prefilter,
+                    Tag.from_joined(needed_tags_str),
+                    self.server.config.whitelist_tags_prefilter,
+                    show_absent)
         files.sort(key=lambda t: t.rel_path)
         self._send_rendered_template(_NAME_TEMPLATE_FILES,
                                      {'files': files,
index 54519243e9d7e0dca393328c78ec76d7bda91f87..86a1c1fae9ce04a6bf1d866e517a107692416e61 100644 (file)
@@ -29,6 +29,7 @@ DEFAULTS = {
     'port_remote': 8090,
     'background_color': '#ffffff',
     'queries_cutoff': '',
+    'whitelist_tags_prefilter_str': '',
     'needed_tags_prefilter_str': '',
     'allow_file_edit': True
 }
@@ -92,6 +93,7 @@ class Config:
     background_color: str
     queries_cutoff: str
     needed_tags_prefilter_str: str
+    whitelist_tags_prefilter_str: str
     allow_file_edit: bool
 
     def __init__(self):
@@ -108,6 +110,8 @@ class Config:
                              if k.isupper() and k.startswith(ENVIRON_PREFIX)})
         self.needed_tags_prefilter = Tag.from_joined(
                 self.needed_tags_prefilter_str)
+        self.whitelist_tags_prefilter = Tag.from_joined(
+                self.whitelist_tags_prefilter_str)
 
 
 class YoutubeQuery(DbData):
@@ -328,14 +332,18 @@ class VideoFile(DbData):
     def get_filtered(cls,
                      conn: BaseDbConn,
                      filter_path: FilterStr,
-                     needed_tags: set[Tag],
+                     needed_tags_dark: set[Tag],
+                     needed_tags_seen: set[Tag],
+                     whitelist_tags: set[Tag],
                      show_absent: bool = False
                      ) -> list[Self]:
         """Return cls.get_all matching provided filter criteria."""
         return [f for f in cls.get_all(conn)
                 if (show_absent or f.present)
                 and str(filter_path).lower() in str(f.rel_path).lower()
-                and not [t for t in needed_tags if t not in f.tags]]
+                and ([t for t in whitelist_tags if t in f.tags]
+                     or not [t for t in needed_tags_dark if t not in f.tags])
+                and not [t for t in needed_tags_seen if t not in f.tags]]
 
     def unused_tags(self, conn: BaseDbConn) -> set[Tag]:
         """Return tags used among other VideoFiles, not in self."""
@@ -430,12 +438,16 @@ class Player:
     """MPV representation with some additional features."""
     _idx: int
 
-    def __init__(self, needed_tags_prefilter: set[Tag]) -> None:
+    def __init__(self,
+                 whitelist_tags_prefilter: set[Tag],
+                 needed_tags_prefilter: set[Tag],
+                 ) -> None:
         self.last_update = DatetimeStr('')
         self._mpv: Optional[MPV] = None
         self._kill_queue: Queue = Queue()
         self._monitoring_kill = False
         self.filter_path = FilterStr('')
+        self._whitelist_tags_prefilter = whitelist_tags_prefilter
         self._needed_tags_prefilter = needed_tags_prefilter
         self.needed_tags: set[Tag] = set()
         self.load_files_and_start()
@@ -466,8 +478,11 @@ class Player:
             known_files = {
                 f.full_path: f for f
                 in VideoFile.get_filtered(
-                        conn, self.filter_path,
-                        self._needed_tags_prefilter | self.needed_tags)}
+                        conn,
+                        self.filter_path,
+                        self._needed_tags_prefilter,
+                        self.needed_tags,
+                        self._whitelist_tags_prefilter)}
         self.files = [known_files[p] for p in PATH_DOWNLOADS.iterdir()
                       if p in known_files
                       and p.is_file()