Skip to content

Commit

Permalink
Use the new Server-Time header from KodiSyncQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
oddstr13 committed Sep 19, 2024
1 parent 63c424b commit 3427ab6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
5 changes: 4 additions & 1 deletion jellyfin_kodi/jellyfin/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 16 additions & 9 deletions jellyfin_kodi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3427ab6

Please sign in to comment.