Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 85bd87ef authored by Timi's avatar Timi Committed by Sebastiano Barezzi
Browse files

Twelve: JellyfinDataSource: Implement lyrics fetching

Change-Id: Id5b120d3b46105fac8c0e86904099c10bf056fe4
parent 27e846ac
Loading
Loading
Loading
Loading
+28 −3
Original line number Diff line number Diff line
@@ -270,9 +270,21 @@ class JellyfinDataSource(
            uri?.let(this::audio) ?: flowOf(RequestStatus.Error(MediaError.NOT_FOUND))
        }

    override fun lyrics(audioUri: Uri) = flowOf(
        RequestStatus.Error<Lyrics, _>(MediaError.NOT_IMPLEMENTED)
    )
    override fun lyrics(audioUri: Uri) = suspend {
        val id = UUID.fromString(audioUri.lastPathSegment!!)
        client.getLyrics(id).toRequestStatus {
            toModel()
        }.let {
            when (it) {
                is RequestStatus.Loading -> RequestStatus.Loading(it.progress)
                is RequestStatus.Success -> it.data?.let { lyrics ->
                    RequestStatus.Success<_, MediaError>(lyrics)
                } ?: RequestStatus.Error(MediaError.NOT_FOUND)

                is RequestStatus.Error -> RequestStatus.Error(it.error, it.throwable)
            }
        }
    }.asFlow()

    override suspend fun createPlaylist(name: String) = run {
        client.createPlaylist(name).toRequestStatus {
@@ -382,6 +394,19 @@ class JellyfinDataSource(
        .setName(name)
        .build()

    private fun org.lineageos.twelve.datasources.jellyfin.models.Lyrics.toModel() = lyrics?.let {
        Lyrics.Builder()
            .apply {
                it.forEach { lyrics ->
                    addLine(
                        text = lyrics.text,
                        startMs = lyrics.start / 10000,
                    )
                }
            }
            .build()
    }

    private fun getAlbumUri(albumId: String) = albumsUri.buildUpon()
        .appendPath(albumId)
        .build()
+9 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import okhttp3.OkHttpClient
import org.lineageos.twelve.datasources.jellyfin.models.CreatePlaylist
import org.lineageos.twelve.datasources.jellyfin.models.CreatePlaylistResult
import org.lineageos.twelve.datasources.jellyfin.models.Item
import org.lineageos.twelve.datasources.jellyfin.models.Lyrics
import org.lineageos.twelve.datasources.jellyfin.models.PlaylistItems
import org.lineageos.twelve.datasources.jellyfin.models.QueryResult
import org.lineageos.twelve.datasources.jellyfin.models.SystemInfo
@@ -175,6 +176,14 @@ class JellyfinClient(
        ),
    )

    suspend fun getLyrics(id: UUID) = ApiRequest.get<Lyrics>(
        listOf(
            "Audio",
            id.toString(),
            "lyrics",
        ),
    ).execute(api)

    suspend fun createPlaylist(name: String) =
        ApiRequest.post<CreatePlaylist, CreatePlaylistResult>(
            listOf("Playlists"),
+20 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2025 The LineageOS Project
 * SPDX-License-Identifier: Apache-2.0
 */

@file:UseSerializers(UUIDSerializer::class)

package org.lineageos.twelve.datasources.jellyfin.models

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import org.lineageos.twelve.datasources.jellyfin.serializers.UUIDSerializer

@Suppress("PROVIDED_RUNTIME_TOO_LOW")
@Serializable
data class LyricLine(
    @SerialName("Start") val start: Long,
    @SerialName("Text") val text: String
)
+19 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2025 The LineageOS Project
 * SPDX-License-Identifier: Apache-2.0
 */

@file:UseSerializers(UUIDSerializer::class)

package org.lineageos.twelve.datasources.jellyfin.models

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import org.lineageos.twelve.datasources.jellyfin.serializers.UUIDSerializer

@Suppress("PROVIDED_RUNTIME_TOO_LOW")
@Serializable
data class Lyrics(
    @SerialName("Lyrics") val lyrics: List<LyricLine>? = null
)