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 20, 2024
1 parent 63c424b commit 3c394bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
7 changes: 6 additions & 1 deletion jellyfin_kodi/jellyfin/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ 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
# 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(

Check warning on line 185 in jellyfin_kodi/jellyfin/http.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/jellyfin/http.py#L185

Added line #L185 was not covered by tests
"Server-Time", r.headers.get("Date")
)
elapsed = int(r.elapsed.total_seconds() * 1000)
response = r.json()
LOG.debug("---<[ http ][%s ms]", elapsed)
Expand Down
30 changes: 21 additions & 9 deletions jellyfin_kodi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,30 @@ def fast_sync(self):
return True

def save_last_sync(self):
_raw_time = self.server.config.data["server-time"]

Check warning on line 528 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L528

Added line #L528 was not covered by tests
# 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")

Check warning on line 531 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L531

Added line #L531 was not covered by tests
else:
try:

Check warning on line 533 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L533

Added line #L533 was not covered by tests
# TODO: Clean up once the probability of most users having
# the updated server-side plugin is high.
LOG.warning(

Check warning on line 536 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L536

Added line #L536 was not covered by tests
"Server time not in ISO 8601 format, using fallback (update KodiSyncQueue)."
)
import email.utils

Check warning on line 539 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L539

Added line #L539 was not covered by tests

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)

Check warning on line 541 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L541

Added line #L541 was not covered by tests

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()

Check warning on line 546 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L543-L546

Added lines #L543 - L546 were not covered by tests

# Add some tolerance in case time is out of sync with server
time_now -= timedelta(minutes=2)

Check warning on line 549 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L549

Added line #L549 was not covered by tests

last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%Sz")
last_sync = time_now.strftime("%Y-%m-%dT%H:%M:%SZ")

Check warning on line 551 in jellyfin_kodi/library.py

View check run for this annotation

Codecov / codecov/patch

jellyfin_kodi/library.py#L551

Added line #L551 was not covered by tests
settings("LastIncrementalSync", value=last_sync)
LOG.info("--[ sync/%s ]", last_sync)

Expand Down

0 comments on commit 3c394bc

Please sign in to comment.