Add synchronized lyrics (#126)
This commit is contained in:
1
synchronized-lyrics/.gitignore
vendored
Normal file
1
synchronized-lyrics/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
12
synchronized-lyrics/build.gradle.kts
Normal file
12
synchronized-lyrics/build.gradle.kts
Normal file
@@ -0,0 +1,12 @@
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
}
|
||||
|
||||
sourceSets.all {
|
||||
java.srcDir("src/$name/kotlin")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.kotlin.coroutines)
|
||||
testImplementation(testLibs.junit)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package it.vfsfitvnm.synchronizedlyrics
|
||||
|
||||
import java.io.FileNotFoundException
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
object LujjjhLyrics {
|
||||
suspend fun forSong(artist: String, title: String): Result<String?>? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val artistParameter = URLEncoder.encode(artist, "UTF-8")
|
||||
val titleParameter = URLEncoder.encode(title, "UTF-8")
|
||||
|
||||
URL("https://lyrics-api.lujjjh.com?artist=$artistParameter&name=$titleParameter")
|
||||
.openConnection()
|
||||
.getInputStream()
|
||||
.bufferedReader()
|
||||
.readText()
|
||||
}.recoverIfCancelled()?.recoverCatching { throwable ->
|
||||
if (throwable is FileNotFoundException) null else throw throwable
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package it.vfsfitvnm.synchronizedlyrics
|
||||
|
||||
fun parseSentences(text: String): List<Pair<Long, String>> {
|
||||
return mutableListOf(0L to "").apply {
|
||||
for (line in text.trim().lines()) {
|
||||
val sentence = line.substring(10)
|
||||
|
||||
if (sentence.startsWith(" 作词 : ") || sentence.startsWith(" 作曲 : ")) {
|
||||
continue
|
||||
}
|
||||
|
||||
val position = line.take(10).run {
|
||||
get(8).digitToInt() * 10L +
|
||||
get(7).digitToInt() * 100 +
|
||||
get(5).digitToInt() * 1000 +
|
||||
get(4).digitToInt() * 10000 +
|
||||
get(2).digitToInt() * 60 * 1000 +
|
||||
get(1).digitToInt() * 600 * 1000
|
||||
}
|
||||
|
||||
add(position to sentence)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package it.vfsfitvnm.synchronizedlyrics
|
||||
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
internal fun <T> Result<T>.recoverIfCancelled(): Result<T>? {
|
||||
return when (exceptionOrNull()) {
|
||||
is CancellationException -> null
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
11
synchronized-lyrics/src/test/kotlin/Test.kt
Normal file
11
synchronized-lyrics/src/test/kotlin/Test.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Test
|
||||
|
||||
class Test {
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun test() {
|
||||
runBlocking {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user