Add searchLyricsByKeyword as last resort

This commit is contained in:
vfsfitvnm 2022-08-06 23:29:24 +02:00
parent 51e767b422
commit 41b22eab70
2 changed files with 20 additions and 5 deletions

View file

@ -133,7 +133,7 @@ fun Lyrics(
}
}
KuGou.lyrics(mediaMetadata.artist?.toString() ?: "", mediaMetadata.title?.toString() ?: "", duration)?.map {
KuGou.lyrics(mediaMetadata.artist?.toString() ?: "", mediaMetadata.title?.toString() ?: "", duration / 1000)?.map {
it?.value
}
} else {

View file

@ -52,14 +52,20 @@ object KuGou {
suspend fun lyrics(artist: String, title: String, duration: Long): Result<Lyrics?>? {
return runCatching {
for (info in searchSong(keyword(artist, title))) {
if (info.duration >= duration / 1000 - 2 && info.duration <= duration / 1000 + 2) {
searchLyrics(info.hash).firstOrNull()?.let { candidate ->
val keyword = keyword(artist, title)
for (info in searchSong(keyword)) {
if (info.duration >= duration - 2 && info.duration <= duration + 2) {
searchLyricsByHash(info.hash).firstOrNull()?.let { candidate ->
return@runCatching downloadLyrics(candidate.id, candidate.accessKey).normalize()
}
}
}
searchLyricsByKeyword(keyword).firstOrNull()?.let { candidate ->
return@runCatching downloadLyrics(candidate.id, candidate.accessKey).normalize()
}
null
}.recoverIfCancelled()
}
@ -75,7 +81,7 @@ object KuGou {
}.body<DownloadLyricsResponse>().content.decodeBase64String().let(::Lyrics)
}
private suspend fun searchLyrics(hash: String): List<SearchLyricsResponse.Candidate> {
private suspend fun searchLyricsByHash(hash: String): List<SearchLyricsResponse.Candidate> {
return client.get("/search") {
parameter("ver", 1)
parameter("man", "yes")
@ -84,6 +90,15 @@ object KuGou {
}.body<SearchLyricsResponse>().candidates
}
private suspend fun searchLyricsByKeyword(keyword: String): List<SearchLyricsResponse.Candidate> {
return client.get("/search") {
parameter("ver", 1)
parameter("man", "yes")
parameter("client", "mobi")
url.encodedParameters.append("keyword", keyword.encodeURLParameter(spaceToPlus = false))
}.body<SearchLyricsResponse>().candidates
}
private suspend fun searchSong(keyword: String): List<SearchSongResponse.Data.Info> {
return client.get("https://mobileservice.kugou.com/api/v3/search/song") {
parameter("version", 9108)