Kickassanime & Hianime: Fix subtitles #937
4 changed files with 35 additions and 5 deletions
|
@ -47,7 +47,7 @@ class MegaCloudExtractor(
|
|||
private var shouldUpdateKey = false
|
||||
private const val PREF_KEY_KEY = "megacloud_key_"
|
||||
private const val PREF_KEY_DEFAULT = "[[0, 0]]"
|
||||
|
||||
|
||||
private inline fun <reified R> runLocked(crossinline block: () -> R) = runBlocking(Dispatchers.IO) {
|
||||
MUTEX.withLock { block() }
|
||||
}
|
||||
|
@ -132,6 +132,7 @@ class MegaCloudExtractor(
|
|||
?.filter { it.kind == "captions" }
|
||||
?.map { Track(it.file, it.label) }
|
||||
.orEmpty()
|
||||
.let { playlistUtils.fixSubtitles(it) }
|
||||
return playlistUtils.extractFromHls(
|
||||
masterUrl,
|
||||
videoNameGen = { "$name - $it - $type" },
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package eu.kanade.tachiyomi.lib.playlistutils
|
||||
|
||||
import android.net.Uri
|
||||
import eu.kanade.tachiyomi.animesource.model.Track
|
||||
import eu.kanade.tachiyomi.animesource.model.Video
|
||||
import eu.kanade.tachiyomi.network.GET
|
||||
|
@ -8,6 +9,7 @@ import okhttp3.Headers
|
|||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.internal.commonEmptyHeaders
|
||||
import java.io.File
|
||||
import kotlin.math.abs
|
||||
|
||||
class PlaylistUtils(private val client: OkHttpClient, private val headers: Headers = commonEmptyHeaders) {
|
||||
|
@ -342,7 +344,32 @@ class PlaylistUtils(private val client: OkHttpClient, private val headers: Heade
|
|||
return "${result}p"
|
||||
}
|
||||
|
||||
private fun cleanSubtitleData(matchResult: MatchResult): String {
|
||||
val lineCount = matchResult.groupValues[1].count { it == '\n' }
|
||||
return "\n" + " \n".repeat(lineCount - 1)
|
||||
}
|
||||
|
||||
fun fixSubtitles(subtitleList: List<Track>): List<Track> {
|
||||
return subtitleList.mapNotNull {
|
||||
try {
|
||||
val subData = client.newCall(GET(it.url)).execute().body.string()
|
||||
|
||||
val file = File.createTempFile("subs", "vtt")
|
||||
.also(File::deleteOnExit)
|
||||
|
||||
file.writeText(FIX_SUBTITLE_REGEX.replace(subData, ::cleanSubtitleData))
|
||||
val uri = Uri.fromFile(file)
|
||||
|
||||
Track(uri.toString(), it.lang)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val FIX_SUBTITLE_REGEX = Regex("""${'$'}(\n{2,})(?!(?:\d+:)*\d+(?:\.\d+)?\s-+>\s(?:\d+:)*\d+(?:\.\d+)?)""", RegexOption.MULTILINE)
|
||||
|
||||
private const val PLAYLIST_SEPARATOR = "#EXT-X-STREAM-INF:"
|
||||
|
||||
private val SUBTITLE_REGEX by lazy { Regex("""#EXT-X-MEDIA:TYPE=SUBTITLES.*?NAME="(.*?)".*?URI="(.*?)"""") }
|
||||
|
|
|
@ -291,7 +291,7 @@ class AniPlay : AniListAnimeHttpSource(), ConfigurableAnimeSource {
|
|||
return playlistUtils.extractFromHls(
|
||||
playlistUrl = url,
|
||||
videoNameGen = { quality -> "$serverName - $quality - $typeName" },
|
||||
subtitleList = subtitles,
|
||||
subtitleList = playlistUtils.fixSubtitles(subtitles),
|
||||
masterHeadersGen = { baseHeaders: Headers, _: String ->
|
||||
baseHeaders.newBuilder().apply {
|
||||
set("Accept", "*/*")
|
||||
|
|
|
@ -18,6 +18,8 @@ class KickAssAnimeExtractor(
|
|||
private val json: Json,
|
||||
private val headers: Headers,
|
||||
) {
|
||||
private val playlistUtils by lazy { PlaylistUtils(client, headers) }
|
||||
|
||||
fun videosFromUrl(url: String, name: String): List<Video> {
|
||||
val host = url.toHttpUrl().host
|
||||
val mid = if (name == "DuckStream") "mid" else "id"
|
||||
|
@ -77,7 +79,7 @@ class KickAssAnimeExtractor(
|
|||
val language = "${it.name} (${it.language})"
|
||||
|
||||
Track(subUrl, language)
|
||||
}
|
||||
}.let { playlistUtils.fixSubtitles(it) }
|
||||
|
||||
fun getVideoHeaders(baseHeaders: Headers, referer: String, videoUrl: String): Headers {
|
||||
return baseHeaders.newBuilder().apply {
|
||||
|
@ -92,8 +94,8 @@ class KickAssAnimeExtractor(
|
|||
|
||||
return when {
|
||||
videoObject.hls.isBlank() ->
|
||||
PlaylistUtils(client, headers).extractFromDash(videoObject.playlistUrl, videoNameGen = { res -> "$name - $res" }, subtitleList = subtitles)
|
||||
else -> PlaylistUtils(client, headers).extractFromHls(
|
||||
playlistUtils.extractFromDash(videoObject.playlistUrl, videoNameGen = { res -> "$name - $res" }, subtitleList = subtitles)
|
||||
else -> playlistUtils.extractFromHls(
|
||||
videoObject.playlistUrl,
|
||||
videoNameGen = { "$name - $it" },
|
||||
videoHeadersGen = ::getVideoHeaders,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue