fix(pt/Anitube): Update extension (#111)

This commit is contained in:
Dark25 2024-10-18 22:08:48 +02:00 committed by GitHub
parent c60c7d4d73
commit eae235fa10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 435 additions and 94 deletions

View file

@ -1,7 +1,7 @@
ext {
extName = 'Anitube'
extClass = '.Anitube'
extVersionCode = 18
extVersionCode = 19
}
apply from: "$rootDir/common.gradle"

View file

@ -1,31 +1,29 @@
package eu.kanade.tachiyomi.animeextension.pt.anitube
import android.app.Application
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreferenceCompat
import eu.kanade.tachiyomi.animeextension.pt.anitube.extractors.AnitubeDownloadExtractor
import eu.kanade.tachiyomi.animeextension.pt.anitube.extractors.AnitubeExtractor
import eu.kanade.tachiyomi.animesource.ConfigurableAnimeSource
import eu.kanade.tachiyomi.animesource.model.AnimeFilterList
import eu.kanade.tachiyomi.animesource.model.AnimesPage
import eu.kanade.tachiyomi.animesource.model.SAnime
import eu.kanade.tachiyomi.animesource.model.SEpisode
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.animesource.online.ParsedAnimeHttpSource
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.awaitSuccess
import eu.kanade.tachiyomi.util.asJsoup
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import eu.kanade.tachiyomi.util.parallelCatchingFlatMapBlocking
import okhttp3.Request
import okhttp3.Response
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.text.SimpleDateFormat
import java.util.Locale
@ -43,10 +41,8 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
private val json: Json by injectLazy()
override fun headersBuilder() = super.headersBuilder()
.add("Referer", baseUrl)
.add("Referer", "$baseUrl/")
.add("Accept-Language", ACCEPT_LANGUAGE)
// ============================== Popular ===============================
@ -86,6 +82,30 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
override fun latestUpdatesNextPageSelector() = popularAnimeNextPageSelector()
// =============================== Search ===============================
override suspend fun getSearchAnime(
page: Int,
query: String,
filters: AnimeFilterList,
): AnimesPage {
return if (query.startsWith(PREFIX_SEARCH)) {
val path = query.removePrefix(PREFIX_SEARCH)
client.newCall(GET("$baseUrl/$path"))
.awaitSuccess()
.use(::searchAnimeByIdParse)
} else {
super.getSearchAnime(page, query, filters)
}
}
private fun searchAnimeByIdParse(response: Response): AnimesPage {
val details = animeDetailsParse(response).apply {
setUrlWithoutDomain(response.request.url.toString())
initialized = true
}
return AnimesPage(listOf(details), false)
}
override fun getFilterList(): AnimeFilterList = AnitubeFilters.FILTER_LIST
override fun searchAnimeRequest(page: Int, query: String, filters: AnimeFilterList): Request {
@ -97,7 +117,14 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
val char = params.initialChar
when {
season.isNotBlank() -> "$baseUrl/temporada/$season/$year"
genre.isNotBlank() -> "$baseUrl/genero/$genre/page/$page/${char.replace("todos", "")}"
genre.isNotBlank() ->
"$baseUrl/genero/$genre/page/$page/${
char.replace(
"todos",
"",
)
}"
else -> "$baseUrl/anime/page/$page/letra/$char"
}
} else {
@ -119,7 +146,7 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
val infos = content.selectFirst("div.anime_infos")!!
title = doc.selectFirst("div.anime_container_titulo")!!.text()
thumbnail_url = content.selectFirst("img")?.imgAttr()
thumbnail_url = content.selectFirst("img")?.attr("src")
genre = infos.getInfo("Gêneros")
author = infos.getInfo("Autor")
artist = infos.getInfo("Estúdio")
@ -135,14 +162,6 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
}
}
fun Element.imgAttr(): String = when {
hasAttr("data-cfsrc") -> absUrl("data-cfsrc")
hasAttr("data-lazy-src") -> absUrl("data-lazy-src")
hasAttr("data-src") -> absUrl("data-src").substringBefore(" ")
hasAttr("srcset") -> absUrl("srcset").substringBefore(" ")
else -> absUrl("src")
}
// ============================== Episodes ==============================
override fun episodeListSelector() = "div.animepag_episodios_item > a"
@ -163,79 +182,39 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
override fun episodeFromElement(element: Element) = SEpisode.create().apply {
setUrlWithoutDomain(element.attr("href"))
episode_number = element.selectFirst("div.animepag_episodios_item_views")!!
.text().trim()
.text()
.substringAfter(" ")
.toFloatOrNull() ?: 0F
name = element.selectFirst("div[class*='animepag_episodios_item_views']")!!.text()
name = element.selectFirst("div.animepag_episodios_item_nome")!!.text()
date_upload = element.selectFirst("div.animepag_episodios_item_date")!!
.text()
.toDate()
}
// ============================ Video Links =============================
override fun videoListParse(response: Response) = AnitubeExtractor.getVideoList(response, headers).let {
val auth = getToken()
private val anitubeExtractor by lazy { AnitubeExtractor(headers, client, preferences) }
private val downloadExtractor by lazy { AnitubeDownloadExtractor(headers, client) }
it.map { video ->
Video(
url = video.url,
quality = video.quality,
videoUrl = "${video.videoUrl}${auth.value}",
headers = video.headers,
subtitleTracks = video.subtitleTracks,
audioTracks = video.audioTracks,
)
}
}
override fun videoListParse(response: Response): List<Video> {
val document = response.asJsoup()
private fun getToken(): AnitubeToken {
val headers = Headers.Builder()
.set("Accept", "*/*")
.set("Accept-Encoding", "br, zstd")
.set("Accept-Language", "pt-BR,en-US;q=0.7,en;q=0.3")
.set("Cache-Control", "no-cache")
.set("Connection", "keep-alive")
.set("Pragma", "no-cache")
.set("Referer", "$baseUrl/")
.set("Sec-Fetch-Dest", "empty")
.set("Sec-Fetch-Mode", "cors")
.set("Sec-Fetch-Site", "same-site")
.apply {
headers["User-Agent"]?.let {
set("User-Agent", it)
}
val links = mutableListOf(document.location())
if (preferences.getBoolean(PREF_FILE4GO_KEY, PREF_FILE4GO_DEFAULT)!!) {
document.selectFirst("div.abaItemDown > a")?.attr("href")?.let {
links.add(it)
}
.build()
}
val client = OkHttpClient()
val epName = document.selectFirst("meta[itemprop=name]")!!.attr("content")
val ads = client.newCall(GET("https://widgets.outbrain.com/outbrain.js", headers))
.execute()
val form = FormBody.Builder()
.add("category", "client")
.add("type", "premium")
.add("ad", ads.body.string())
.build()
val response = client
.newCall(POST(url = "https://ads.anitube.vip", headers = headers, body = form))
.execute()
val token = response.parseAs<List<AnitubeToken>>().first()
val tokenUrl = "https://ads.anitube.vip".toHttpUrl().newBuilder()
.addQueryParameter("token", token.value)
.build()
return client.newCall(GET(tokenUrl, headers))
.execute()
.parseAs<List<AnitubeToken>>()
.first()
}
private inline fun <reified T> Response.parseAs(): T = use {
json.decodeFromStream(it.body.byteStream())
return links.parallelCatchingFlatMapBlocking {
when {
it.contains("/download/") -> downloadExtractor.videosFromUrl(it, epName)
it.contains("file4go.net") -> downloadExtractor.videosFromUrl(it, epName)
else -> anitubeExtractor.getVideoList(document)
}
}
}
override fun videoListSelector() = throw UnsupportedOperationException()
@ -257,7 +236,32 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
val entry = entryValues[index] as String
preferences.edit().putString(key, entry).commit()
}
}.let(screen::addPreference)
}.also(screen::addPreference)
SwitchPreferenceCompat(screen.context).apply {
key = PREF_FILE4GO_KEY
title = "Usar File4Go como mirror"
setDefaultValue(PREF_FILE4GO_DEFAULT)
summary = PREF_FILE4GO_SUMMARY
setOnPreferenceChangeListener { _, newValue ->
preferences.edit().putBoolean(key, newValue as Boolean).commit()
}
}.also(screen::addPreference)
// Auth Code
EditTextPreference(screen.context).apply {
key = PREF_AUTHCODE_KEY
title = "Auth Code"
setDefaultValue(PREF_AUTHCODE_DEFAULT)
summary = PREF_AUTHCODE_SUMMARY
setOnPreferenceChangeListener { _, newValue ->
runCatching {
val value = (newValue as String).trim().ifBlank { PREF_AUTHCODE_DEFAULT }
preferences.edit().putString(key, value).commit()
}.getOrDefault(false)
}
}.also(screen::addPreference)
}
// ============================= Utilities ==============================
@ -295,8 +299,11 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
override fun List<Video>.sort(): List<Video> {
val quality = preferences.getString(PREF_QUALITY_KEY, PREF_QUALITY_DEFAULT)!!
return sortedWith(
compareByDescending { it.quality.equals(quality) },
)
compareBy<Video>(
{ it.quality.startsWith(quality) },
{ PREF_QUALITY_ENTRIES.indexOf(it.quality.substringBefore(" ")) },
).thenByDescending { it.quality },
).reversed()
}
private fun String.toDate(): Long {
@ -306,10 +313,17 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
}
companion object {
const val PREFIX_SEARCH = "id:"
private val DATE_FORMATTER by lazy { SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH) }
private const val ACCEPT_LANGUAGE = "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
private const val PREF_AUTHCODE_KEY = "authcode"
private const val PREF_AUTHCODE_SUMMARY = "Código de Autenticação"
private const val PREF_AUTHCODE_DEFAULT = ""
private const val PREF_FILE4GO_KEY = "file4go"
private const val PREF_FILE4GO_SUMMARY = "Usar File4Go como mirror"
private const val PREF_FILE4GO_DEFAULT = false
private const val PREF_QUALITY_KEY = "preferred_quality"
private const val PREF_QUALITY_TITLE = "Qualidade preferida"
private const val PREF_QUALITY_DEFAULT = "HD"

View file

@ -0,0 +1,113 @@
package eu.kanade.tachiyomi.animeextension.pt.anitube.extractors
import android.util.Log
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.util.asJsoup
import eu.kanade.tachiyomi.util.parallelMapNotNullBlocking
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
class AnitubeDownloadExtractor(
private val headers: Headers,
private val client: OkHttpClient,
) {
private val qualities = listOf("SD", "HD", "FULLHD")
private val tag by lazy { javaClass.simpleName }
private fun videosFromFile4Go(url: String, quality: String): Video? {
Log.d(tag, "Checking download for $url")
val newHeaders = headers.newBuilder()
.set("Referer", "https://${url.toHttpUrl().host}/")
.add("Accept", "*/*")
.add("Cache-Control", "no-cache")
.add("Pragma", "no-cache")
.add("Connection", "keep-alive")
.add("Sec-Fetch-Dest", "empty")
.add("Sec-Fetch-Mode", "cors")
.add("Sec-Fetch-Site", "same-site")
.build()
val id = url.split('/').last()
val idusuario =
client.newCall(GET("$ADS_URL/file4go.php", headers = newHeaders))
.execute()
.body.string()
.substringAfter("\"publicidade\"")
.substringAfter('"')
.substringBefore('"')
val body = FormBody.Builder().apply {
add("id", id)
add("idusuario", idusuario)
}.build()
val postUrl = "https://www.file4go.net/getdownload_new_anitube.php"
val postHeaders = headers.newBuilder()
.set("Referer", url)
.build()
val docFinal =
client.newCall(POST(postUrl, headers = postHeaders, body = body))
.execute().asJsoup()
val videoUrl = docFinal.selectFirst("a.novobotao.download")?.attr("href")
if (videoUrl == null) {
Log.d(tag, "Download link not found for $url")
return null
}
return Video(videoUrl, "$quality - File4Go", videoUrl)
}
private fun videosFromDownloadPage(url: String, epName: String): List<Video> {
Log.d(tag, "Extracting videos links for URL: $url")
val docDownload = client.newCall(GET(url)).execute().asJsoup()
val row = docDownload.select("table.downloadpag_episodios tr").firstOrNull {
it.text().contains(epName)
}
if (row == null) {
Log.d(tag, "Episode $epName not found in download page")
return emptyList()
}
val links = row.select("td").mapIndexedNotNull { index, el ->
val link = el.selectFirst("a") ?: return@mapIndexedNotNull null
object {
var quality = qualities.get(index - 1)
var url = link.attr("href")
}
}
Log.d(tag, "Found ${links.size} links for $epName")
return links.parallelMapNotNullBlocking {
if (!it.url.contains("file4go.net")) {
return@parallelMapNotNullBlocking null
}
videosFromFile4Go(it.url, it.quality)
}.reversed()
}
fun videosFromUrl(url: String, epName: String, quality: String = "Default"): List<Video> {
if (url.contains("file4go.net")) {
return listOfNotNull(videosFromFile4Go(url, quality))
}
return videosFromDownloadPage(url, epName)
}
companion object {
private const val ADS_URL = "https://ads.anitube.vip"
}
}

View file

@ -1,17 +1,208 @@
package eu.kanade.tachiyomi.animeextension.pt.anitube.extractors
import android.content.SharedPreferences
import android.util.Log
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.util.asJsoup
import eu.kanade.tachiyomi.util.parallelMapNotNullBlocking
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.Response
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import org.jsoup.nodes.Document
import java.net.ProtocolException
object AnitubeExtractor {
fun getVideoList(response: Response, headers: Headers): List<Video> {
val doc = response.asJsoup()
class AnitubeExtractor(
private val headers: Headers,
private val client: OkHttpClient,
private val preferences: SharedPreferences,
) {
private val tag by lazy { javaClass.simpleName }
private data class VideoExists(
val exists: Boolean,
val code: Int,
)
private fun checkVideoExists(url: String): VideoExists {
try {
val request = Request.Builder()
.head()
.url(url)
.headers(headers)
.build()
val response = client.newCall(request).execute()
return VideoExists(response.isSuccessful, response.code)
} catch (e: ProtocolException) {
// There are a bug in the response that sometimes that the content is without headers
if (e.message?.contains("Unexpected status line") == true) {
return VideoExists(true, 200)
}
}
return VideoExists(false, 404)
}
private fun getAdsUrl(
serverUrl: String,
thumbUrl: String,
link: String,
linkHeaders: Headers,
): String {
val videoName = serverUrl.split('/').last()
val finalLink =
if (link.startsWith("//")) {
"https:$link"
} else {
link
}
Log.d(tag, "Accessing the link $finalLink")
val response = client.newCall(GET(finalLink, headers = linkHeaders)).execute()
val docLink = response.asJsoup()
val refresh = docLink.selectFirst("meta[http-equiv=refresh]")?.attr("content")
if (!refresh.isNullOrBlank()) {
val newLink = refresh.substringAfter("=")
val newHeaders = linkHeaders.newBuilder().set("Referer", finalLink).build()
Log.d(tag, "Following link redirection to $newLink")
return getAdsUrl(serverUrl, thumbUrl, newLink, newHeaders)
}
val referer: String = docLink.location() ?: link
Log.d(tag, "Final URL: $referer")
Log.d(tag, "Fetching ADS URL")
val newHeaders =
linkHeaders.newBuilder().set("Referer", "https://${referer.toHttpUrl().host}/").build()
try {
val now = System.currentTimeMillis()
val body = client.newCall(
GET(
"$SITE_URL?name=apphd/$videoName&img=$thumbUrl&pais=pais=BR&time=$now&url=$serverUrl",
headers = newHeaders,
),
)
.execute()
.body.string()
val adsUrl = body.let {
Regex("""ADS_URL\s*=\s*['"]([^'"]+)['"]""")
.find(it)?.groups?.get(1)?.value
?: ""
}
if (adsUrl.startsWith("http")) {
Log.d(tag, "ADS URL: $adsUrl")
return adsUrl
}
} catch (e: Exception) {
Log.e(tag, e.toString())
}
// Try default url
Log.e(tag, "Failed to get the ADS URL, trying the default")
return "https://www.popads.net/js/adblock.js"
}
private fun getAuthCode(serverUrl: String, thumbUrl: String, link: String): String {
var authCode = preferences.getString(PREF_AUTHCODE_KEY, "")!!
if (authCode.isNotBlank()) {
Log.d(tag, "AuthCode found in preferences")
val response = checkVideoExists("${serverUrl}$authCode")
if (response.exists || response.code == 500) {
Log.d(tag, "AuthCode is OK")
return authCode
}
Log.d(tag, "AuthCode is invalid")
}
Log.d(tag, "Fetching new authCode")
val adsUrl = getAdsUrl(serverUrl, thumbUrl, link, headers)
val adsContent = client.newCall(GET(adsUrl)).execute().body.string()
val body = FormBody.Builder()
.add("category", "client")
.add("type", "premium")
.add("ad", adsContent)
.build()
val newHeaders = headers.newBuilder()
.set("Referer", "https://${SITE_URL.toHttpUrl().host}/")
.add("Accept", "*/*")
.add("Cache-Control", "no-cache")
.add("Pragma", "no-cache")
.add("Connection", "keep-alive")
.add("Sec-Fetch-Dest", "empty")
.add("Sec-Fetch-Mode", "cors")
.add("Sec-Fetch-Site", "same-site")
.build()
val publicidade =
client.newCall(POST("$ADS_URL/", headers = newHeaders, body = body))
.execute()
.body.string()
.substringAfter("\"publicidade\"")
.substringAfter('"')
.substringBefore('"')
if (publicidade.isBlank()) {
Log.e(
tag,
"Failed to fetch \"publicidade\" code, the current response: $publicidade",
)
throw Exception("Por favor, abra o vídeo uma vez no navegador para liberar o IP")
}
authCode =
client.newCall(
GET(
"$ADS_URL/?token=$publicidade",
headers = newHeaders,
),
)
.execute()
.body.string()
.substringAfter("\"publicidade\"")
.substringAfter('"')
.substringBefore('"')
if (authCode.startsWith("?")) {
Log.d(tag, "Auth code fetched successfully")
preferences.edit().putString(PREF_AUTHCODE_KEY, authCode).commit()
} else {
Log.e(
tag,
"Failed to fetch auth code, the current response: $authCode",
)
}
return authCode
}
fun getVideoList(doc: Document): List<Video> {
val hasFHD = doc.selectFirst("div.abaItem:contains(FULLHD)") != null
val serverUrl = doc.selectFirst("meta[itemprop=contentURL]")!!
.attr("content")
.replace("cdn1", "cdn3")
val thumbUrl = doc.selectFirst("meta[itemprop=thumbnailUrl]")!!
.attr("content")
val type = serverUrl.split("/").get(3)
val qualities = listOfNotNull("SD", "HD", if (hasFHD) "FULLHD" else null)
val paths = listOf("appsd", "apphd").let {
@ -21,10 +212,33 @@ object AnitubeExtractor {
it
}
} + listOf("appfullhd")
return qualities.mapIndexed { index, quality ->
val path = paths[index]
val url = serverUrl.replace(type, path)
Video(url, quality, url, headers = headers)
}.reversed()
val firstLink =
doc.selectFirst("div.video_container > a, div.playerContainer > a")!!.attr("href")
val authCode = getAuthCode(serverUrl, thumbUrl, firstLink)
return qualities
.mapIndexed { index, quality ->
object {
var path = paths[index]
var url = serverUrl.replace(type, path) + authCode
var quality = "$quality - Anitube"
}
}
.parallelMapNotNullBlocking {
if (!checkVideoExists(it.url).exists) {
Log.d(tag, "Video not exists: ${it.url.substringBefore("?")}")
return@parallelMapNotNullBlocking null
}
Video(it.url, it.quality, it.url, headers = headers)
}
.reversed()
}
companion object {
private const val PREF_AUTHCODE_KEY = "authcode"
private const val ADS_URL = "https://ads.anitube.vip"
private const val SITE_URL = "https://www.anitube.vip/playerricas.php"
}
}