diff --git a/jellyfin_kodi/jellyfin/http.py b/jellyfin_kodi/jellyfin/http.py index a58ad9616..bf6d103e3 100644 --- a/jellyfin_kodi/jellyfin/http.py +++ b/jellyfin_kodi/jellyfin/http.py @@ -179,7 +179,7 @@ def request(self, data, session=None): else: try: - self.config.data["server-time"] = r.headers["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 452429761..2d75313ed 100644 --- a/jellyfin_kodi/library.py +++ b/jellyfin_kodi/library.py @@ -525,18 +525,22 @@ def fast_sync(self): return True def save_last_sync(self): + _raw_time = self.server.config.data["server-time"] try: - time_now = datetime.strptime( - self.server.config.data["server-time"].split(", ", 1)[1], - "%d %b %Y %H:%M:%S GMT", - ) - timedelta(minutes=2) + import email.utils + + 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() - LOG.exception(error) - time_now = datetime.utcnow() - timedelta(minutes=2) + # 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") + last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%SZ") settings("LastIncrementalSync", value=last_sync) LOG.info("--[ sync/%s ]", last_sync)