Skip to content

Commit

Permalink
Fix Spotify Canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Nov 10, 2024
1 parent 380eea7 commit 5d4771c
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,19 +504,6 @@ class DataStoreManager(private val settingsDataStore: DataStore<Preferences>) {
}
}

val spotifyClientToken: Flow<String> =
settingsDataStore.data.map { preferences ->
preferences[SPOTIFY_CLIENT_TOKEN] ?: ""
}

suspend fun setSpotifyClientToken(token: String) {
withContext(Dispatchers.IO) {
settingsDataStore.edit { settings ->
settings[SPOTIFY_CLIENT_TOKEN] = token
}
}
}

val homeLimit: Flow<Int> =
settingsDataStore.data.map { preferences ->
preferences[HOME_LIMIT] ?: 5
Expand Down Expand Up @@ -595,7 +582,6 @@ class DataStoreManager(private val settingsDataStore: DataStore<Preferences>) {
val SPOTIFY_CANVAS = stringPreferencesKey("spotify_canvas")
val SPOTIFY_PERSONAL_TOKEN = stringPreferencesKey("spotify_personal_token")
val SPOTIFY_PERSONAL_TOKEN_EXPIRES = longPreferencesKey("spotify_personal_token_expires")
val SPOTIFY_CLIENT_TOKEN = stringPreferencesKey("spotify_client_token")
val HOME_LIMIT = intPreferencesKey("home_limit")
val CHART_KEY = stringPreferencesKey("chart_key")
val TRANSLUCENT_BOTTOM_BAR = stringPreferencesKey("translucent_bottom_bar")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ class MainRepository(
dataStoreManager.spotifyPersonalTokenExpires.first() != 0L
) {
spotifyPersonalToken = dataStoreManager.spotifyPersonalToken.first()
Log.d("Lyrics", "spotifyPersonalToken: $spotifyPersonalToken")
Log.d("Canvas", "spotifyPersonalToken: $spotifyPersonalToken")
} else if (dataStoreManager.spdc.first().isNotEmpty()) {
YouTube
.getPersonalToken(dataStoreManager.spdc.first())
Expand All @@ -1937,36 +1937,36 @@ class MainRepository(
dataStoreManager.setSpotifyPersonalTokenExpires(
it.accessTokenExpirationTimestampMs,
)
Log.d("Lyrics", "spotifyPersonalToken: $spotifyPersonalToken")
Log.d("Canvas", "spotifyPersonalToken: $spotifyPersonalToken")
}.onFailure {
it.printStackTrace()
emit(null)
}
}
if (spotifyPersonalToken.isNotEmpty()) {
var clientToken = dataStoreManager.spotifyClientToken.first()
Log.d("Lyrics", "clientToken: $clientToken")
val authToken = spotifyPersonalToken
YouTube
.searchSpotifyTrack(q, clientToken)
.searchSpotifyTrack(q, authToken)
.onSuccess { searchResponse ->
Log.w("Canvas", "searchSpotifyResponse: $searchResponse")
val track =
if (duration != 0) {
searchResponse.tracks.items.find {
searchResponse.data?.searchV2?.tracksV2?.items?.find {
abs(
((it.duration_ms / 1000) - duration),
(((it.item?.data?.duration?.totalMilliseconds ?: (0 / 1000)) - duration)),
) < 1
}
?: searchResponse.tracks.items.firstOrNull()
?: searchResponse.data?.searchV2?.tracksV2?.items?.firstOrNull()
} else {
searchResponse.tracks.items.firstOrNull()
searchResponse.data?.searchV2?.tracksV2?.items?.firstOrNull()
}
if (track != null) {
YouTube
.getSpotifyCanvas(
track.id,
track.item?.data?.id ?: "",
spotifyPersonalToken,
).onSuccess {
Log.w("Spotify Canvas", "canvas: $it")
Log.w("Canvas", "canvas: $it")
emit(it)
}.onFailure {
it.printStackTrace()
Expand All @@ -1977,47 +1977,10 @@ class MainRepository(
}
}.onFailure { throwable ->
throwable.printStackTrace()
YouTube
.getClientToken()
.onSuccess { tokenResponse ->
clientToken = tokenResponse.accessToken
Log.w("Lyrics", "clientToken: $clientToken")
dataStoreManager.setSpotifyClientToken(clientToken)
YouTube
.searchSpotifyTrack(q, clientToken)
.onSuccess { searchResponse ->
val track =
if (duration != 0) {
searchResponse.tracks.items.find {
abs(
((it.duration_ms / 1000) - duration),
) < 1
}
?: searchResponse.tracks.items.firstOrNull()
} else {
searchResponse.tracks.items.firstOrNull()
}
if (track != null) {
YouTube
.getSpotifyCanvas(
track.id,
spotifyPersonalToken,
).onSuccess {
Log.w("Spotify Canvas", "canvas: $it")
emit(it)
}.onFailure {
it.printStackTrace()
emit(null)
}
} else {
emit(null)
}
}
}.onFailure {
it.printStackTrace()
emit(null)
}
emit(null)
}
} else {
emit(null)
}
}
}
Expand Down Expand Up @@ -2067,26 +2030,26 @@ class MainRepository(
}
}
if (spotifyPersonalToken.isNotEmpty()) {
var clientToken = dataStoreManager.spotifyClientToken.first()
Log.d("Lyrics", "clientToken: $clientToken")
val authToken = spotifyPersonalToken
Log.d("Lyrics", "authToken: $authToken")
YouTube
.searchSpotifyTrack(q, clientToken)
.searchSpotifyTrack(q, authToken)
.onSuccess { searchResponse ->
val track =
if (duration != null && duration != 0) {
searchResponse.tracks.items.find {
if (duration != 0 && duration != null) {
searchResponse.data?.searchV2?.tracksV2?.items?.find {
abs(
((it.duration_ms / 1000) - duration),
(((it.item?.data?.duration?.totalMilliseconds ?: (0 / 1000)) - duration)),
) < 1
}
?: searchResponse.tracks.items.firstOrNull()
?: searchResponse.data?.searchV2?.tracksV2?.items?.firstOrNull()
} else {
searchResponse.tracks.items.firstOrNull()
searchResponse.data?.searchV2?.tracksV2?.items?.firstOrNull()
}
Log.d("Lyrics", "track: $track")
if (track != null) {
YouTube
.getSpotifyLyrics(track.id, spotifyPersonalToken)
.getSpotifyLyrics(track.item?.data?.id ?: "", spotifyPersonalToken)
.onSuccess {
emit(Resource.Success<Lyrics>(it.toLyrics()))
}.onFailure {
Expand All @@ -2098,42 +2061,7 @@ class MainRepository(
}
}.onFailure { throwable ->
throwable.printStackTrace()
YouTube
.getClientToken()
.onSuccess {
clientToken = it.accessToken
Log.w("Lyrics", "clientToken: $clientToken")
dataStoreManager.setSpotifyClientToken(clientToken)
YouTube.searchSpotifyTrack(q, clientToken).onSuccess { searchResponse ->
val track =
if (duration != null && duration != 0) {
searchResponse.tracks.items.find {
abs(
((it.duration_ms / 1000) - duration),
) < 1
}
?: searchResponse.tracks.items.firstOrNull()
} else {
searchResponse.tracks.items.firstOrNull()
}
Log.d("Lyrics", "track: $track")
if (track != null) {
YouTube
.getSpotifyLyrics(track.id, spotifyPersonalToken)
.onSuccess {
emit(Resource.Success<Lyrics>(it.toLyrics()))
}.onFailure {
it.printStackTrace()
emit(Resource.Error<Lyrics>("Not found"))
}
} else {
emit(Resource.Error<Lyrics>("Not found"))
}
}
}.onFailure {
it.printStackTrace()
emit(Resource.Error<Lyrics>("Not found"))
}
emit(Resource.Error<Lyrics>("Not found"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ fun NowPlayingScreen(
.padding(horizontal = 40.dp),
verticalAlignment = Alignment.CenterVertically,
) {

Column(Modifier.weight(1f)) {
Text(
text = screenDataState.nowPlayingTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ class SharedViewModel(
videoId: String,
duration: Int,
) {
Log.w("Start getCanvas", "$videoId $duration")
Log.w(tag, "Start getCanvas: $videoId $duration")
// canvasJob?.cancel()
viewModelScope.launch {
if (dataStoreManager.spotifyCanvas.first() == TRUE){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.maxrave.kotlinytmusicscraper.models.response.spotify.CanvasResponse
import com.maxrave.kotlinytmusicscraper.models.response.spotify.PersonalTokenResponse
import com.maxrave.kotlinytmusicscraper.models.response.spotify.SpotifyLyricsResponse
import com.maxrave.kotlinytmusicscraper.models.response.spotify.TokenResponse
import com.maxrave.kotlinytmusicscraper.models.response.spotify.search.SpotifySearchResponse
import com.maxrave.kotlinytmusicscraper.models.response.toLikeStatus
import com.maxrave.kotlinytmusicscraper.models.simpmusic.GithubResponse
import com.maxrave.kotlinytmusicscraper.models.sponsorblock.SkipSegments
Expand Down Expand Up @@ -1707,10 +1708,6 @@ object YouTube {
/***
* Spotify Implementation
*/
suspend fun getClientToken() =
runCatching {
ytMusic.getSpotifyToken().body<TokenResponse>()
}

suspend fun getPersonalToken(spdc: String) =
runCatching {
Expand All @@ -1719,11 +1716,11 @@ object YouTube {

suspend fun searchSpotifyTrack(
query: String,
token: String,
authToken: String
) = runCatching {
ytMusic
.searchSpotifyTrack(query, token)
.body<com.maxrave.kotlinytmusicscraper.models.response.spotify.SearchResponse>()
.searchSpotifyTrack(query, authToken)
.body<SpotifySearchResponse>()
}

suspend fun getSpotifyLyrics(
Expand Down
Loading

0 comments on commit 5d4771c

Please sign in to comment.