From: Christian Heller Date: Sun, 23 Feb 2025 04:29:09 +0000 (+0100) Subject: Minor refactoring. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/booking/static/edit?a=commitdiff_plain;ds=sidebyside;p=ytplom Minor refactoring. --- diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 0a94f96..db5fe54 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -97,6 +97,11 @@ def _readable_seconds(seconds: int) -> str: for s in (hours_str, minutes_str, seconds_str)]) +def _now_string() -> DatetimeStr: + """Return datetime.now() in TIMESTAMP_FMT.""" + return DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT)) + + class TagSet: """Collection of tags as used in VideoFile.tags.""" @@ -251,8 +256,7 @@ class YoutubeQuery(DbData): continue result.set_duration_from_yt_string(content_details['duration']) result.definition = content_details['definition'].upper() - query = cls(None, query_txt, - DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT))) + query = cls(None, query_txt, _now_string()) query.save(conn) for result in [r for r in results if r.id_ not in unfinished_streams]: result.save(conn) @@ -358,18 +362,12 @@ class VideoFile(DbData): duration_ms if duration_ms >= 0 else int(ONE_MILLION * Decimal( ffprobe(self.full_path)['format']['duration']))) - if last_update is None: - self._renew_last_update() - else: - self.last_update = last_update + self.last_update = last_update if last_update else _now_string() self._hash_on_last_update = hash(self) def __hash__(self) -> int: return hash(tuple(getattr(self, k) for k in self._cols)) - def _renew_last_update(self): - self.last_update = DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT)) - @property def as_dict(self) -> dict[str, bool | str | list[str]]: """Return dict of values relevant for /files.""" @@ -386,7 +384,7 @@ class VideoFile(DbData): def save(self, conn: DbConn) -> None: """Extend super().save by new .last_update if sufficient changes.""" if hash(self) != self._hash_on_last_update: - self._renew_last_update() + self.last_update = _now_string() return super().save(conn) def test_deletion(self, do_raise: bool) -> bool: @@ -566,9 +564,7 @@ class QuotaLog(DbData): def update(cls, conn: DbConn, cost: QuotaCost) -> None: """Adds cost mapped to current datetime.""" cls._remove_old(conn) - new = cls(None, - DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT)), - QuotaCost(cost)) + new = cls(None, _now_string(), QuotaCost(cost)) new.save(conn) @classmethod @@ -601,7 +597,7 @@ class Player: def _signal_update(self) -> None: """Update .last_update as signal player state has changed relevantly""" - self.last_update = DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT)) + self.last_update = _now_string() def _monitor_kill(self) -> None: """Properly enforce mpv shutdown from direct interaction with mpv