Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pt/megaflix): Update extension #113

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/pt/megaflix/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
extName = 'Megaflix'
extClass = '.Megaflix'
extVersionCode = 21
extVersionCode = 22
isNsfw = true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -135,6 +139,7 @@ class Megaflix : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
episode_number = 1F
},
)

else -> seasons.parallelFlatMapBlocking(::episodesFromSeason).reversed()
}
}
Expand Down Expand Up @@ -163,19 +168,24 @@ 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)
}
}

/*--------------------------------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) }
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.util.Log
import kotlin.system.exitProcess

/**
* Springboard that accepts https://megaflix.co/<type>/<item> intents
* Springboard that accepts https://megaflix.ac/<type>/<item> intents
* and redirects them to the main Aniyomi process.
*/
class MegaflixUrlActivity : Activity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
)
}
Expand Down