forked from aniyomiorg/aniyomi-extensions
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from Dark25/UniversalExtractor
feat(lib/UniversalExtractor): Implementation of a universal extractor by adly
- Loading branch information
Showing
7 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugins { | ||
id("lib-android") | ||
} | ||
|
||
dependencies { | ||
implementation(project(":lib:playlist-utils")) | ||
} |
86 changes: 86 additions & 0 deletions
86
...-extractor/src/main/java/eu/kanade/tachiyomi/lib/universalextractor/UniversalExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package eu.kanade.tachiyomi.lib.universalextractor | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Application | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.util.Log | ||
import android.webkit.WebResourceRequest | ||
import android.webkit.WebResourceResponse | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import eu.kanade.tachiyomi.animesource.model.Video | ||
import eu.kanade.tachiyomi.lib.playlistutils.PlaylistUtils | ||
import okhttp3.Headers | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import okhttp3.OkHttpClient | ||
import uy.kohesive.injekt.injectLazy | ||
import java.util.Locale | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
class UniversalExtractor(private val client: OkHttpClient) { | ||
private val context: Application by injectLazy() | ||
private val handler by lazy { Handler(Looper.getMainLooper()) } | ||
@SuppressLint("SetJavaScriptEnabled") | ||
fun videosFromUrl(origRequestUrl: String, origRequestHeader: Headers, customQuality: String? = null, prefix: String = ""): List<Video> { | ||
val host = origRequestUrl.toHttpUrl().host.substringBefore(".").proper() | ||
val latch = CountDownLatch(1) | ||
var webView: WebView? = null | ||
var resultUrl = "" | ||
val playlistUtils by lazy { PlaylistUtils(client, origRequestHeader) } | ||
val headers = origRequestHeader.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }.toMutableMap() | ||
|
||
handler.post { | ||
val newView = WebView(context) | ||
webView = newView | ||
with(newView.settings) { | ||
javaScriptEnabled = true | ||
domStorageEnabled = true | ||
databaseEnabled = true | ||
useWideViewPort = false | ||
loadWithOverviewMode = false | ||
userAgentString = origRequestHeader["User-Agent"] | ||
} | ||
newView.webViewClient = object : WebViewClient() { | ||
override fun shouldInterceptRequest( | ||
view: WebView, | ||
request: WebResourceRequest, | ||
): WebResourceResponse? { | ||
val url = request.url.toString() | ||
if (VIDEO_REGEX.containsMatchIn(url)) { | ||
resultUrl = url | ||
latch.countDown() | ||
} | ||
return super.shouldInterceptRequest(view, request) | ||
} | ||
} | ||
|
||
webView?.loadUrl(origRequestUrl, headers) | ||
} | ||
|
||
latch.await(TIMEOUT_SEC, TimeUnit.SECONDS) | ||
|
||
handler.post { | ||
webView?.stopLoading() | ||
webView?.destroy() | ||
webView = null | ||
} | ||
return when { | ||
"m3u8" in resultUrl -> playlistUtils.extractFromHls(resultUrl, origRequestUrl, videoNameGen = { "$prefix - $host: $it" }) | ||
"mpd" in resultUrl -> playlistUtils.extractFromDash(resultUrl, { it -> "$prefix - $host: $it" }, referer = origRequestUrl) | ||
"mp4" in resultUrl -> Video(resultUrl, "$prefix - $host: ${customQuality ?: "Mirror"}", resultUrl, origRequestHeader.newBuilder().add("referer", origRequestUrl).build()).let(::listOf) | ||
else -> emptyList() | ||
} | ||
} | ||
|
||
private fun String.proper(): String { | ||
return this.replaceFirstChar { if (it.isLowerCase()) it.titlecase( | ||
Locale.getDefault()) else it.toString() } | ||
} | ||
|
||
companion object { | ||
const val TIMEOUT_SEC: Long = 20 | ||
private val VIDEO_REGEX by lazy { Regex("\\.(mp4|m3u8|mpd)") } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters