Use the new Server-Time header from KodiSyncQueue

This commit is contained in:
Odd Stråbø 2024-09-19 01:03:29 +00:00
parent 63c424bc4b
commit 3427ab655d
2 changed files with 20 additions and 10 deletions

View file

@ -179,7 +179,10 @@ class HTTP(object):
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)

View file

@ -525,18 +525,25 @@ 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:
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)