Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new Server-Time header from KodiSyncQueue #918

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@
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