Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,22 @@ class SpotifyAPI {
* @return The server time in milliseconds.
*/
private suspend fun getServerTime(): Long {
val response = client.get(
webPlayerURL + "api/server-time"
) {
reqHeaders.forEach { (key, value) -> header(key, value) }
return try {
val response = client.get(
webPlayerURL + "api/server-time"
) {
reqHeaders.forEach { (key, value) -> header(key, value) }
}
val body = response.bodyAsText(Charsets.UTF_8)
val serverTimeSeconds = json.decodeFromString<ServerTimeResponse>(body).serverTime

// If serverTime is 0 (default) or missing, fallback to system time.
// Spotify returns seconds, so we multiply by 1000. System time is already millis.
if (serverTimeSeconds > 0) serverTimeSeconds * 1000 else System.currentTimeMillis()
} catch (e: Exception) {
// Fallback to local time if request fails completely
System.currentTimeMillis()
}
val body = response.bodyAsText(Charsets.UTF_8)
return json.decodeFromString<ServerTimeResponse>(body).serverTime * 1000
}

/**
Expand Down Expand Up @@ -211,4 +220,4 @@ class SpotifyAPI {
albumArtURL
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.Serializable

@Serializable
data class ServerTimeResponse(
val serverTime: Long,
val serverTime: Long = 0,
)

@Serializable
Expand All @@ -13,4 +13,4 @@ data class WebPlayerTokenResponse(
val accessToken: String,
val accessTokenExpirationTimestampMs: Long,
val isAnonymous: Boolean,
)
)