From 3427ab655d82a7287ff6dde290b2fa38a661f8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Thu, 19 Sep 2024 01:03:29 +0000 Subject: [PATCH] Use the new Server-Time header from KodiSyncQueue --- jellyfin_kodi/jellyfin/http.py | 5 ++++- jellyfin_kodi/library.py | 25 ++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/jellyfin_kodi/jellyfin/http.py b/jellyfin_kodi/jellyfin/http.py index a58ad9616..de0c29e1f 100644 --- a/jellyfin_kodi/jellyfin/http.py +++ b/jellyfin_kodi/jellyfin/http.py @@ -179,7 +179,10 @@ def request(self, data, session=None): else: try: - self.config.data["server-time"] = r.headers["Date"] + # Prefer custom Server-Time header in ISO 8601 format + self.config.data["server-time"] = r.headers.get( + "Server-Time", r.headers.get("Date") + ) elapsed = int(r.elapsed.total_seconds() * 1000) response = r.json() LOG.debug("---<[ http ][%s ms]", elapsed) diff --git a/jellyfin_kodi/library.py b/jellyfin_kodi/library.py index 452429761..8b744af33 100644 --- a/jellyfin_kodi/library.py +++ b/jellyfin_kodi/library.py @@ -525,18 +525,25 @@ def fast_sync(self): return True def save_last_sync(self): + _raw_time = self.server.config.data["server-time"] + # The ISO 8601 header always end with Z + if _raw_time and _raw_time[-1] == "Z": + time_now = datetime.strptime(_raw_time, "%Y-%m-%dT%H:%M:%SZ") + else: + try: + import locale - try: - time_now = datetime.strptime( - self.server.config.data["server-time"].split(", ", 1)[1], - "%d %b %Y %H:%M:%S GMT", - ) - timedelta(minutes=2) - except Exception as error: + old = locale.getlocale(locale.LC_TIME) + locale.setlocale(locale.LC_TIME, ("C", "UTF-8")) + time_now = datetime.strptime(_raw_time, "%a, %d %b %Y %H:%M:%S %Z") + locale.setlocale(locale.LC_TIME, old) + except Exception as error: - LOG.exception(error) - time_now = datetime.utcnow() - timedelta(minutes=2) + LOG.warning(error) + time_now = datetime.utcnow() - last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%Sz") + time_now -= timedelta(minutes=2) + last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%SZ") settings("LastIncrementalSync", value=last_sync) LOG.info("--[ sync/%s ]", last_sync)