From 7bc919ce8944c3b871ef3761c8e30b04f74ce597 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Mon, 8 Aug 2022 11:59:53 +0200 Subject: [PATCH] Set duration tolerance when searching synchronized lyrics --- .../src/main/kotlin/it/vfsfitvnm/kugou/KuGou.kt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kugou/src/main/kotlin/it/vfsfitvnm/kugou/KuGou.kt b/kugou/src/main/kotlin/it/vfsfitvnm/kugou/KuGou.kt index 8daae42..6b02815 100644 --- a/kugou/src/main/kotlin/it/vfsfitvnm/kugou/KuGou.kt +++ b/kugou/src/main/kotlin/it/vfsfitvnm/kugou/KuGou.kt @@ -53,12 +53,21 @@ object KuGou { suspend fun lyrics(artist: String, title: String, duration: Long): Result? { return runCatching { val keyword = keyword(artist, title) + val infoByKeyword = searchSong(keyword) - 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() + if (infoByKeyword.isNotEmpty()) { + var tolerance = 0 + + while (tolerance <= 5) { + for (info in infoByKeyword) { + if (info.duration >= duration - tolerance && info.duration <= duration + tolerance) { + searchLyricsByHash(info.hash).firstOrNull()?.let { candidate -> + return@runCatching downloadLyrics(candidate.id, candidate.accessKey).normalize() + } + } } + + tolerance++ } }