Skip to content

Commit

Permalink
[skip ci] refactor(lib): Refactor some libraries (#2454)
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudemirovsky authored Nov 1, 2023
1 parent e3afbe2 commit 9db910e
Show file tree
Hide file tree
Showing 27 changed files with 153 additions and 330 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

tasks.register<Delete>("clean") {
delete(rootProject.buildDir)
delete(rootProject.layout.buildDirectory.asFile.get())
}

allprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BloggerExtractor(private val client: OkHttpClient) {
"37" -> "1080p"
else -> "Unknown"
}
Video(videoUrl, "Blogger - $quality $suffix".trim(), videoUrl, headers)
Video(videoUrl, "Blogger - $quality $suffix".trimEnd(), videoUrl, headers)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@ class BurstCloudExtractor(private val client: OkHttpClient) {
private val json: Json by injectLazy()

fun videoFromUrl(url: String, headers: Headers, name: String = "BurstCloud", prefix: String = ""): List<Video> {

val newHeaders = headers.newBuilder().add("referer", "https://www.burstcloud.co/").build()
val newHeaders = headers.newBuilder().set("referer", BURSTCLOUD_URL).build()
return runCatching {
val response = client.newCall(GET(url, headers = newHeaders)).execute()
val document = response.asJsoup()
val response = client.newCall(GET(url, newHeaders)).execute()
val document = response.use { it.asJsoup() }
val videoId = document.selectFirst("div#player")!!.attr("data-file-id")

val formBody = FormBody.Builder()
.add("fileId", videoId)
.build()
val jsonHeaders = headers.newBuilder().add("referer", document.location()).build()
val jsonString = client.newCall(POST("https://www.burstcloud.co/file/play-request/", jsonHeaders, formBody)).execute().body.string()

val jsonHeaders = headers.newBuilder().set("referer", document.location()).build()
val request = POST("$BURSTCLOUD_URL/file/play-request/", jsonHeaders, formBody)
val jsonString = client.newCall(request).execute().use { it.body.string() }

val jsonObj = json.decodeFromString<BurstCloudDto>(jsonString)
val videoUrl = jsonObj.purchase.cdnUrl

if (videoUrl.isNotEmpty()) {
val quality = prefix + name
listOf(Video(videoUrl, quality, videoUrl, newHeaders))
} else {
null
}

}.getOrNull() ?: emptyList<Video>()
}.getOrNull().orEmpty()
}
}

private const val BURSTCLOUD_URL = "https://www.burstcloud.co"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package eu.kanade.tachiyomi.lib.burstcloudextractor
import kotlinx.serialization.Serializable

@Serializable
data class BurstCloudDto(
val purchase: Purchase,
)
data class BurstCloudDto(val purchase: Purchase)

@Serializable
data class Purchase(
val cdnUrl: String,
)
data class Purchase(val cdnUrl: String)
7 changes: 5 additions & 2 deletions lib/fastream-extractor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ android {
}

dependencies {
compileOnly(libs.bundles.common)
implementation("dev.datlag.jsunpacker:jsunpacker:1.0.1") {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
}
implementation(project(":lib-playlist-utils"))
}
compileOnly(libs.bundles.common)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,61 @@ package eu.kanade.tachiyomi.lib.fastreamextractor
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.lib.playlistutils.PlaylistUtils
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import okhttp3.FormBody
import eu.kanade.tachiyomi.util.asJsoup
import kotlinx.serialization.json.Json
import okhttp3.Headers
import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy

class FastreamExtractor(private val client: OkHttpClient) {
private val json: Json by injectLazy()
private fun fetchUrls(text: String?): List<String> {
if (text.isNullOrEmpty()) return listOf()
val linkRegex = "(http|ftp|https):\\/\\/([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:\\/~+#-]*[\\w@?^=%&\\/~+#-])".toRegex()
return linkRegex.findAll(text).map { it.value.trim().removeSurrounding("\"") }.toList()
import okhttp3.internal.commonEmptyHeaders
import dev.datlag.jsunpacker.JsUnpacker

class FastreamExtractor(private val client: OkHttpClient, private val headers: Headers = commonEmptyHeaders) {
private val videoHeaders by lazy {
headers.newBuilder()
.set("Referer", "$FASTREAM_URL/")
.set("Origin", FASTREAM_URL)
.build()
}

fun videoFromUrl(url: String, prefix: String = "Fastream:", headers: Headers? = null): List<Video> {
val videoList = mutableListOf<Video>()
try {
val document = client.newCall(GET(url)).execute().asJsoup()
val videoHeaders = (headers?.newBuilder() ?: Headers.Builder())
.set("Referer", "https://fastream.to/")
.set("Origin", "https://fastream.to")
.build()
document.select("script").forEach {
if (it!!.data().contains("jwplayer(jwplayer(\"vplayer\").setup({")) {
val basicUrl = it.data().substringAfter("file: '").substringBefore("',")
videoList.add(Video(basicUrl, prefix, basicUrl, headers = videoHeaders))
} else {
val packedRegex = "eval\\(function\\(p,a,c,k,e,.*\\)\\)".toRegex()
packedRegex.findAll(it.data()).map { packed -> packed.value }.toList().map { eval ->
val unpack = JsUnpacker.unpack(eval)
val serverRegex = "fastream.*?\\.m3u8([^&\">]?)".toRegex()
fetchUrls(unpack.first()).filter { serverRegex.containsMatchIn(it) }.map { url ->
PlaylistUtils(client, videoHeaders).extractFromHls(url, videoNameGen = { "$prefix$it" }).let { videoList.addAll(it) }
}
}
private val playlistUtils by lazy { PlaylistUtils(client, videoHeaders) }

fun videosFromUrl(url: String, prefix: String = "Fastream:", needsSleep: Boolean = true): List<Video> {
return runCatching {
val firstDoc = client.newCall(GET(url, videoHeaders)).execute().use { it.asJsoup() }

val form = FormBody.Builder().apply {
firstDoc.select("input[name]").forEach {
add(it.attr("name"), it.attr("value"))
}
}.build()

if (needsSleep) Thread.sleep(5100L) // 5s is the minimum
val doc = client.newCall(POST(url, videoHeaders, body = form)).execute()
.use { it.asJsoup() }

val scriptElement = doc.selectFirst("script:containsData(jwplayer):containsData(vplayer)")
?: return emptyList()

val scriptData = scriptElement.data().let {
when {
it.contains("eval(function(") -> JsUnpacker.unpackAndCombine(it)
else -> it
}
} ?: return emptyList()

val videoUrl = scriptData.substringAfter("file:")
.substringBefore('}')
.substringBefore(',')
.trim('"', '\'', ' ')

return when {
videoUrl.contains(".m3u8") -> {
playlistUtils.extractFromHls(videoUrl, videoNameGen = { "$prefix$it" })
}
else -> listOf(Video(videoUrl, prefix, videoUrl, videoHeaders))
}
} catch (_: Exception) {}
return videoList
}.getOrElse { emptyList() }
}
}

private const val FASTREAM_URL = "https://fastream.to"

This file was deleted.

1 change: 1 addition & 0 deletions lib/filemoon-extractor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ dependencies {
implementation("dev.datlag.jsunpacker:jsunpacker:1.0.1") {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
}
implementation(project(":lib-playlist-utils"))
compileOnly(libs.bundles.common)
}
Loading

0 comments on commit 9db910e

Please sign in to comment.