Skip to content

Commit

Permalink
fix(pt/anitube): Fixed Anitube and HinataSoul for old videos (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
WebDitto authored Aug 29, 2024
1 parent dec0bd4 commit c7b610a
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/pt/anitube/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
extName = 'Anitube'
extClass = '.Anitube'
extVersionCode = 18
extVersionCode = 19
}

apply from: "$rootDir/common.gradle"
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen
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
Expand All @@ -15,6 +16,7 @@ import eu.kanade.tachiyomi.animesource.online.ParsedAnimeHttpSource
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.awaitSuccess
import eu.kanade.tachiyomi.util.asJsoup
import eu.kanade.tachiyomi.util.parallelCatchingFlatMapBlocking
import okhttp3.Request
import okhttp3.Response
import org.jsoup.nodes.Document
Expand Down Expand Up @@ -189,9 +191,29 @@ class Anitube : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
}

// ============================ Video Links =============================
private val extractor by lazy { AnitubeExtractor(headers, client, preferences) }
private val anitubeExtractor by lazy { AnitubeExtractor(headers, client, preferences) }
private val downloadExtractor by lazy { AnitubeDownloadExtractor(headers, client) }

override fun videoListParse(response: Response): List<Video> {
val document = response.asJsoup()

val links = mutableListOf(document.location())

document.selectFirst("div.abaItemDown > a")?.attr("href")?.let {
links.add(it)
}

val epName = document.selectFirst("meta[itemprop=name]")!!.attr("content")

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 videoListParse(response: Response) = extractor.getVideoList(response)
override fun videoListSelector() = throw UnsupportedOperationException()
override fun videoFromElement(element: Element) = throw UnsupportedOperationException()
override fun videoUrlParse(document: Document) = throw UnsupportedOperationException()
Expand Down Expand Up @@ -264,8 +286,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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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.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 docDownload = client.newCall(GET(url)).execute().asJsoup()

val form =
docDownload.selectFirst("button.download")?.closest("form")

if (form == null) {
Log.d(tag, "Download form not found for $url")
return null
}

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

val postUrl = form.attr("action")

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ 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
import okhttp3.Request
import okhttp3.Response
import org.jsoup.nodes.Document
import java.net.ProtocolException

class AnitubeExtractor(
private val headers: Headers,
Expand All @@ -20,6 +23,32 @@ class AnitubeExtractor(

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,
Expand All @@ -28,15 +57,21 @@ class AnitubeExtractor(
): String {
val videoName = serverUrl.split('/').last()

Log.d(tag, "Accessing the link $link")
val response = client.newCall(GET(link, headers = linkHeaders)).execute()
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", link).build()
val newHeaders = linkHeaders.newBuilder().set("Referer", finalLink).build()
Log.d(tag, "Following link redirection to $newLink")

return getAdsUrl(serverUrl, thumbUrl, newLink, newHeaders)
Expand All @@ -47,30 +82,32 @@ class AnitubeExtractor(
Log.d(tag, "Final URL: $referer")
Log.d(tag, "Fetching ADS URL")

val newHeaders = linkHeaders.newBuilder().set("Referer", referer).build()
val newHeaders =
linkHeaders.newBuilder().set("Referer", "https://${referer.toHttpUrl().host}/").build()

try {
val now = System.currentTimeMillis()
val adsUrl =
client.newCall(
GET(
"$SITE_URL/playerricas.php?name=apphd/$videoName&img=$thumbUrl&pais=pais=BR&time=$now&url=$serverUrl",
headers = newHeaders,
),
)
.execute()
.body.string()
.let {
Regex("""ADS_URL\s*=\s*['"]([^'"]+)['"]""")
.find(it)?.groups?.get(1)?.value
?: ""
}
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
Expand All @@ -84,15 +121,9 @@ class AnitubeExtractor(
if (authCode.isNotBlank()) {
Log.d(tag, "AuthCode found in preferences")

val request = Request.Builder()
.head()
.url("${serverUrl}$authCode")
.headers(headers)
.build()
val response = checkVideoExists("${serverUrl}$authCode")

val response = client.newCall(request).execute()

if (response.isSuccessful || response.code == 500) {
if (response.exists || response.code == 500) {
Log.d(tag, "AuthCode is OK")
return authCode
}
Expand All @@ -112,7 +143,7 @@ class AnitubeExtractor(
.build()

val newHeaders = headers.newBuilder()
.set("Referer", SITE_URL)
.set("Referer", "https://${SITE_URL.toHttpUrl().host}/")
.add("Accept", "*/*")
.add("Cache-Control", "no-cache")
.add("Pragma", "no-cache")
Expand Down Expand Up @@ -165,8 +196,7 @@ class AnitubeExtractor(
return authCode
}

fun getVideoList(response: Response): List<Video> {
val doc = response.asJsoup()
fun getVideoList(doc: Document): List<Video> {
val hasFHD = doc.selectFirst("div.abaItem:contains(FULLHD)") != null
val serverUrl = doc.selectFirst("meta[itemprop=contentURL]")!!
.attr("content")
Expand All @@ -188,16 +218,27 @@ class AnitubeExtractor(

val authCode = getAuthCode(serverUrl, thumbUrl, firstLink)

return qualities.mapIndexed { index, quality ->
val path = paths[index]
val url = serverUrl.replace(type, path) + authCode
Video(url, quality, url, headers = headers)
}.reversed()
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"
private const val SITE_URL = "https://www.anitube.vip/playerricas.php"
}
}
2 changes: 1 addition & 1 deletion src/pt/hinatasoul/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
extName = 'Hinata Soul'
extClass = '.HinataSoul'
extVersionCode = 8
extVersionCode = 9
}

apply from: "$rootDir/common.gradle"
Loading

0 comments on commit c7b610a

Please sign in to comment.