From 3c394bc02cd5d649378e6aea9bb45d81858b2c4f 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 1/2] Use the new Server-Time header from KodiSyncQueue --- jellyfin_kodi/jellyfin/http.py | 7 ++++++- jellyfin_kodi/library.py | 30 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/jellyfin_kodi/jellyfin/http.py b/jellyfin_kodi/jellyfin/http.py index a58ad961..4545ca59 100644 --- a/jellyfin_kodi/jellyfin/http.py +++ b/jellyfin_kodi/jellyfin/http.py @@ -179,7 +179,12 @@ class HTTP(object): else: try: - self.config.data["server-time"] = r.headers["Date"] + # Prefer custom Server-Time header in ISO 8601 format + # TODO: Clean up once the probability of most users having + # the updated server-side plugin is high. + 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 45242976..5e143ee0 100644 --- a/jellyfin_kodi/library.py +++ b/jellyfin_kodi/library.py @@ -525,18 +525,30 @@ class Library(threading.Thread): 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: + # TODO: Clean up once the probability of most users having + # the updated server-side plugin is high. + LOG.warning( + "Server time not in ISO 8601 format, using fallback (update KodiSyncQueue)." + ) + import email.utils - 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: + time_now = email.utils.parsedate_to_datetime(_raw_time) - LOG.exception(error) - time_now = datetime.utcnow() - timedelta(minutes=2) + except Exception as error: + LOG.warning(error) + LOG.warning("Failed to parse server time, falling back to client time.") + time_now = datetime.utcnow() - last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%Sz") + # Add some tolerance in case time is out of sync with server + 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) From 8d1ae2ea78e21e5af1ae79c82fcc884fee9c5f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Sat, 21 Dec 2024 02:59:03 +0100 Subject: [PATCH 2/2] Back out ISO time --- jellyfin_kodi/jellyfin/http.py | 7 +------ jellyfin_kodi/library.py | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/jellyfin_kodi/jellyfin/http.py b/jellyfin_kodi/jellyfin/http.py index 4545ca59..bf6d103e 100644 --- a/jellyfin_kodi/jellyfin/http.py +++ b/jellyfin_kodi/jellyfin/http.py @@ -179,12 +179,7 @@ class HTTP(object): else: try: - # Prefer custom Server-Time header in ISO 8601 format - # TODO: Clean up once the probability of most users having - # the updated server-side plugin is high. - self.config.data["server-time"] = r.headers.get( - "Server-Time", r.headers.get("Date") - ) + self.config.data["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 5e143ee0..2d75313e 100644 --- a/jellyfin_kodi/library.py +++ b/jellyfin_kodi/library.py @@ -526,24 +526,16 @@ class Library(threading.Thread): 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: - # TODO: Clean up once the probability of most users having - # the updated server-side plugin is high. - LOG.warning( - "Server time not in ISO 8601 format, using fallback (update KodiSyncQueue)." - ) - import email.utils - time_now = email.utils.parsedate_to_datetime(_raw_time) + try: + import email.utils - except Exception as error: - LOG.warning(error) - LOG.warning("Failed to parse server time, falling back to client time.") - time_now = datetime.utcnow() + time_now = email.utils.parsedate_to_datetime(_raw_time) + + except Exception as error: + LOG.warning(error) + LOG.warning("Failed to parse server time, falling back to client time.") + time_now = datetime.utcnow() # Add some tolerance in case time is out of sync with server time_now -= timedelta(minutes=2)