-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Musixmatch API Integration (#88)
* Add support for Musixmatch lyrics provider * TODO: Handle Musixmatch provider selected * Added `musixmatchID` to track Musixmatch song ID * Add musixmatchID field to SongInfo model * Add MusixmatchAPI class for searching and retrieving synced lyrics * fix: correct data type for Musixmatch album_id in search response * refactor: rename fields in Musixmatch response models for consistency * fix: correct Musixmatch API usage in LyricsProviderService * feat: add separate fields for synced and unsynced lyrics to Musixmatch API response
- Loading branch information
Showing
5 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/others/MusixmatchAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package pl.lambada.songsync.data.remote.lyrics_providers.others | ||
|
||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import pl.lambada.songsync.domain.model.SongInfo | ||
import pl.lambada.songsync.domain.model.lyrics_providers.others.MusixmatchLyricsResponse | ||
import pl.lambada.songsync.domain.model.lyrics_providers.others.MusixmatchSearchResponse | ||
import pl.lambada.songsync.util.EmptyQueryException | ||
import pl.lambada.songsync.util.networking.Ktor.client | ||
import pl.lambada.songsync.util.networking.Ktor.json | ||
import java.net.URLEncoder | ||
|
||
class MusixmatchAPI { | ||
private val baseURL = "https://kerollosy.alwaysdata.net" | ||
|
||
/** | ||
* Searches for synced lyrics using the song name and artist name. | ||
* @param query The SongInfo object with songName and artistName fields filled. | ||
* @return Search result as a SongInfo object. | ||
*/ | ||
suspend fun getSongInfo(query: SongInfo): SongInfo? { | ||
val search = withContext(Dispatchers.IO) { | ||
URLEncoder.encode( | ||
"${query.songName} ${query.artistName}", | ||
Charsets.UTF_8.toString() | ||
) | ||
} | ||
|
||
if (search == " ") | ||
throw EmptyQueryException() | ||
|
||
val response = client.get( | ||
"$baseURL/search?q=$search" | ||
) | ||
val responseBody = response.bodyAsText(Charsets.UTF_8) | ||
|
||
if (response.status.value !in 200..299 || responseBody == "[]") | ||
return null | ||
|
||
val json = json.decodeFromString<List<MusixmatchSearchResponse>>(responseBody) | ||
|
||
val result = json[0] | ||
|
||
return SongInfo( | ||
songName = result.songName, | ||
artistName = result.artistName, | ||
songLink = result.url, | ||
albumCoverLink = result.artwork, | ||
musixmatchID = result.id, | ||
) | ||
} | ||
|
||
/** | ||
* Searches for synced lyrics using the song name and artist name. | ||
* @param id The ID of the song from search results. | ||
* @return The synced lyrics as a string. | ||
*/ | ||
suspend fun getSyncedLyrics(id: Long): String? { | ||
val response = client.get( | ||
"$baseURL/lyrics?id=$id" | ||
) | ||
val responseBody = response.bodyAsText(Charsets.UTF_8) | ||
|
||
if (response.status.value !in 200..299) | ||
return null | ||
|
||
val json = json.decodeFromString<MusixmatchLyricsResponse>(responseBody) | ||
return json.lyrics | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/src/main/java/pl/lambada/songsync/domain/model/lyrics_providers/others/Musixmatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.lambada.songsync.domain.model.lyrics_providers.others | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class MusixmatchSearchResponse( | ||
val id: Long, | ||
val songName: String, | ||
val artistName: String, | ||
val albumName: String, | ||
val artwork: String, | ||
val releaseDate: String, | ||
val duration: Int, | ||
val url: String, | ||
val albumId: Long, | ||
val hasSyncedLyrics: Boolean, | ||
val hasUnsyncedLyrics: Boolean | ||
) | ||
|
||
@Serializable | ||
data class MusixmatchLyricsResponse( | ||
val id: Long, | ||
val duration: Int, | ||
val language: String, | ||
val updatedTime: String, | ||
val lyrics: String, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters