From b4262b9f3582d66226178c29dae225599056dd00 Mon Sep 17 00:00:00 2001 From: Dark25 Date: Fri, 18 Oct 2024 22:12:16 +0200 Subject: [PATCH] fix(pt/megaflix): Update extension --- src/pt/megaflix/build.gradle | 2 +- .../animeextension/pt/megaflix/Megaflix.kt | 39 ++++++++--- .../pt/megaflix/MegaflixUrlActivity.kt | 2 +- .../pt/megaflix/WebViewResolver.kt | 69 +++++++++++++++++++ .../megaflix/extractors/MegaflixExtractor.kt | 2 +- 5 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/WebViewResolver.kt diff --git a/src/pt/megaflix/build.gradle b/src/pt/megaflix/build.gradle index 15b6ff54fa..38616cdb7f 100644 --- a/src/pt/megaflix/build.gradle +++ b/src/pt/megaflix/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Megaflix' extClass = '.Megaflix' - extVersionCode = 21 + extVersionCode = 22 isNsfw = true } diff --git a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/Megaflix.kt b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/Megaflix.kt index edb032cb57..e64a078554 100644 --- a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/Megaflix.kt +++ b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/Megaflix.kt @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.animeextension.pt.megaflix import android.app.Application -import android.util.Base64 import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.animeextension.pt.megaflix.extractors.MegaflixExtractor @@ -14,6 +13,7 @@ import eu.kanade.tachiyomi.animesource.model.Video import eu.kanade.tachiyomi.animesource.online.ParsedAnimeHttpSource import eu.kanade.tachiyomi.lib.filemoonextractor.FilemoonExtractor import eu.kanade.tachiyomi.lib.mixdropextractor.MixDropExtractor +import eu.kanade.tachiyomi.lib.playlistutils.PlaylistUtils import eu.kanade.tachiyomi.lib.streamtapeextractor.StreamTapeExtractor import eu.kanade.tachiyomi.lib.streamwishextractor.StreamWishExtractor import eu.kanade.tachiyomi.lib.vidhideextractor.VidHideExtractor @@ -35,7 +35,7 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() { override val name = "Megaflix" - override val baseUrl = "https://megaflix.co" + override val baseUrl = "https://megaflix.ac" override val lang = "pt-BR" @@ -73,7 +73,11 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() { override fun latestUpdatesNextPageSelector() = "div.nav-links > a:containsOwn(PRÓXIMO)" // =============================== Search =============================== - override suspend fun getSearchAnime(page: Int, query: String, filters: AnimeFilterList): AnimesPage { + override suspend fun getSearchAnime( + page: Int, + query: String, + filters: AnimeFilterList, + ): AnimesPage { return if (query.startsWith(PREFIX_SEARCH)) { // URL intent handler val path = query.removePrefix(PREFIX_SEARCH) client.newCall(GET("$baseUrl/$path")) @@ -135,6 +139,7 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() { episode_number = 1F }, ) + else -> seasons.parallelFlatMapBlocking(::episodesFromSeason).reversed() } } @@ -163,12 +168,14 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() { .parallelCatchingFlatMapBlocking { element -> val language = element.text().substringAfter("-") val id = element.attr("href") - val url = element.parents().get(5)?.selectFirst("div$id a") - ?.run { - attr("href") - .substringAfter("token=") - .let { String(Base64.decode(it, Base64.DEFAULT)) } - .substringAfter("||") + val url = element.closest("body")?.selectFirst("div$id iframe") + ?.let { + val iframeUrl = it.attr("src") + client.newCall(GET(iframeUrl, headers)) + .execute() + .asJsoup() + .selectFirst("iframe") + ?.attr("src") } ?: return@parallelCatchingFlatMapBlocking emptyList() getVideoList(url, language) @@ -176,6 +183,9 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() { } /*--------------------------------Video extractors------------------------------------*/ + private val webViewResolver by lazy { WebViewResolver() } + private val playlistUtils by lazy { PlaylistUtils(client, headers) } + private val mixdropExtractor by lazy { MixDropExtractor(client) } private val streamtapeExtractor by lazy { StreamTapeExtractor(client) } private val megaflixExtractor by lazy { MegaflixExtractor(client, headers) } @@ -193,6 +203,17 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() { arrayOf("filemoon", "moonplayer").any(url) -> filemoonExtractor.videosFromUrl(url, prefix = "$language Filemoon:") arrayOf("vidhide", "vid.").any(url) -> vidHideExtractor.videosFromUrl(url, videoNameGen = { "$language VidHide:$it" }) arrayOf("wishembed", "streamwish", "strwish", "wish", "jwplayerhls").any(url) -> streamWishExtractor.videosFromUrl(url, videoNameGen = { "$language StreamWish:$it" }) + arrayOf("fembedder").any(url) -> { + val webViewResult = webViewResolver.getUrl(url, headers) + if (webViewResult.isBlank()) { + return emptyList() + } + return if (webViewResult.contains("m3u8")) { + playlistUtils.extractFromHls(webViewResult) + } else { + listOf(Video(url, "Default", url)) + } + } else -> emptyList() } } diff --git a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/MegaflixUrlActivity.kt b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/MegaflixUrlActivity.kt index f3f5edff30..cec9a93dd3 100644 --- a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/MegaflixUrlActivity.kt +++ b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/MegaflixUrlActivity.kt @@ -8,7 +8,7 @@ import android.util.Log import kotlin.system.exitProcess /** - * Springboard that accepts https://megaflix.co// intents + * Springboard that accepts https://megaflix.ac// intents * and redirects them to the main Aniyomi process. */ class MegaflixUrlActivity : Activity() { diff --git a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/WebViewResolver.kt b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/WebViewResolver.kt new file mode 100644 index 0000000000..a1c377b096 --- /dev/null +++ b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/WebViewResolver.kt @@ -0,0 +1,69 @@ +package eu.kanade.tachiyomi.animeextension.pt.megaflix + +import android.annotation.SuppressLint +import android.app.Application +import android.os.Handler +import android.os.Looper +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView +import android.webkit.WebViewClient +import okhttp3.Headers +import uy.kohesive.injekt.injectLazy +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + +class WebViewResolver() { + private val context: Application by injectLazy() + private val handler by lazy { Handler(Looper.getMainLooper()) } + + @SuppressLint("SetJavaScriptEnabled") + fun getUrl(origRequestUrl: String, origRequestheader: Headers): String { + val latch = CountDownLatch(1) + var webView: WebView? = null + var resultUrl = "" + val headers = origRequestheader.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }.toMutableMap() + + handler.post { + val webview = WebView(context) + webView = webview + with(webview.settings) { + javaScriptEnabled = true + domStorageEnabled = true + databaseEnabled = true + useWideViewPort = false + loadWithOverviewMode = false + userAgentString = origRequestheader["User-Agent"] + } + webview.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 resultUrl + } + + companion object { + const val TIMEOUT_SEC: Long = 20 + private val VIDEO_REGEX by lazy { Regex("\\.(mp4|m3u8)") } + } +} diff --git a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/extractors/MegaflixExtractor.kt b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/extractors/MegaflixExtractor.kt index a7a4e96e03..849511d815 100644 --- a/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/extractors/MegaflixExtractor.kt +++ b/src/pt/megaflix/src/eu/kanade/tachiyomi/animeextension/pt/megaflix/extractors/MegaflixExtractor.kt @@ -21,7 +21,7 @@ class MegaflixExtractor(private val client: OkHttpClient, private val headers: H return playlistUtils.extractFromHls( playlistUrl, - "https://megaflix.co", + "https://megaflix.ac", videoNameGen = { "Megaflix($lang) - $it" }, ) }