home · contact · privacy
For VideoFiles, renew .last_update any time .save() saves actual changes to other... master
authorChristian Heller <c.heller@plomlompom.de>
Tue, 3 Dec 2024 06:01:52 +0000 (07:01 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 3 Dec 2024 06:01:52 +0000 (07:01 +0100)
src/ytplom/misc.py

index 4b36b5a7bad2768ab3b8aac490625e15d7dceef4..d0b55e894dc7ecc9b541d0198d872826c7fd538c 100644 (file)
@@ -12,6 +12,7 @@ from uuid import uuid4
 from pathlib import Path
 from threading import Thread
 from queue import Queue
+from sqlite3 import Cursor
 # non-included libs
 import googleapiclient.discovery  # type: ignore
 from mpv import MPV  # type: ignore
@@ -270,9 +271,22 @@ class VideoFile(DbData):
             self._renew_last_update()
         else:
             self.last_update = last_update
+        self._hash_on_last_update = hash(self)
+
+    def __hash__(self) -> int:
+        return hash(f'{self.digest.b64}|{self.rel_path}|{self.flags}|'
+                    f'{self.yt_id}|{self.last_update}|{self.tags_str}')
 
     def _renew_last_update(self):
+        print("DEBUG calling_renew_last_update", self.rel_path)
         self.last_update = DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT))
+        self._hash_on_last_update = hash(self)
+
+    def save(self, conn: BaseDbConn) -> Cursor:
+        """Extend super().save by new .last_update if sufficient changes."""
+        if hash(self) != self._hash_on_last_update:
+            self._renew_last_update()
+        return super().save(conn)
 
     @classmethod
     def get_by_yt_id(cls, conn: BaseDbConn, yt_id: YoutubeId) -> Self:
@@ -324,7 +338,6 @@ class VideoFile(DbData):
         self.flags = FlagsInt(0)
         for flag in flags:
             self.flags = FlagsInt(self.flags | flag)
-        self._renew_last_update()
 
     def is_flag_set(self, flag_name: FlagName) -> bool:
         """Return if flag of flag_name is set in flags field."""