9anime should work now

This commit is contained in:
LagradOst 2022-07-25 23:46:17 +02:00
parent 9b6b06437f
commit c6a10518f5
5 changed files with 311 additions and 261 deletions

View file

@ -837,9 +837,9 @@ fun AnimeSearchResponse.addDubStatus(
fun AnimeSearchResponse.addDubStatus(status: String, episodes: Int? = null) { fun AnimeSearchResponse.addDubStatus(status: String, episodes: Int? = null) {
if (status.contains("(dub)", ignoreCase = true)) { if (status.contains("(dub)", ignoreCase = true)) {
addDubStatus(DubStatus.Dubbed) addDubStatus(DubStatus.Dubbed, episodes)
} else if (status.contains("(sub)", ignoreCase = true)) { } else if (status.contains("(sub)", ignoreCase = true)) {
addDubStatus(DubStatus.Subbed) addDubStatus(DubStatus.Subbed, episodes)
} }
} }
@ -1020,25 +1020,28 @@ interface LoadResponse {
} }
fun LoadResponse.addDuration(input: String?) { fun LoadResponse.addDuration(input: String?) {
val cleanInput = input?.trim()?.replace(" ", "") ?: return this.duration = getDurationFromString(input) ?: this.duration
}
}
}
fun getDurationFromString(input: String?): Int? {
val cleanInput = input?.trim()?.replace(" ", "") ?: return null
Regex("([0-9]*)h.*?([0-9]*)m").find(cleanInput)?.groupValues?.let { values -> Regex("([0-9]*)h.*?([0-9]*)m").find(cleanInput)?.groupValues?.let { values ->
if (values.size == 3) { if (values.size == 3) {
val hours = values[1].toIntOrNull() val hours = values[1].toIntOrNull()
val minutes = values[2].toIntOrNull() val minutes = values[2].toIntOrNull()
this.duration = if (minutes != null && hours != null) { return if (minutes != null && hours != null) {
hours * 60 + minutes hours * 60 + minutes
} else null } else null
if (this.duration != null) return
} }
} }
Regex("([0-9]*)m").find(cleanInput)?.groupValues?.let { values -> Regex("([0-9]*)m").find(cleanInput)?.groupValues?.let { values ->
if (values.size == 2) { if (values.size == 2) {
this.duration = values[1].toIntOrNull() return values[1].toIntOrNull()
if (this.duration != null) return
}
}
} }
} }
return null
} }
fun LoadResponse?.isEpisodeBased(): Boolean { fun LoadResponse?.isEpisodeBased(): Boolean {
@ -1118,7 +1121,7 @@ data class AnimeLoadResponse(
) : LoadResponse, EpisodeResponse ) : LoadResponse, EpisodeResponse
fun AnimeLoadResponse.addEpisodes(status: DubStatus, episodes: List<Episode>?) { fun AnimeLoadResponse.addEpisodes(status: DubStatus, episodes: List<Episode>?) {
if (episodes == null) return if (episodes.isNullOrEmpty()) return
this.episodes[status] = episodes this.episodes[status] = episodes
} }

View file

@ -3,11 +3,10 @@ package com.lagradost.cloudstream3.animeproviders
import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.Qualities
import com.lagradost.cloudstream3.utils.loadExtractor import com.lagradost.cloudstream3.utils.loadExtractor
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.util.*
class NineAnimeProvider : MainAPI() { class NineAnimeProvider : MainAPI() {
override var mainUrl = "https://9anime.id" override var mainUrl = "https://9anime.id"
@ -16,6 +15,7 @@ class NineAnimeProvider : MainAPI() {
override val hasChromecastSupport = true override val hasChromecastSupport = true
override val hasDownloadSupport = true override val hasDownloadSupport = true
override val supportedTypes = setOf(TvType.Anime) override val supportedTypes = setOf(TvType.Anime)
override val hasQuickSearch = true
companion object { companion object {
fun getDubStatus(title: String): DubStatus { fun getDubStatus(title: String): DubStatus {
@ -25,65 +25,27 @@ class NineAnimeProvider : MainAPI() {
DubStatus.Subbed DubStatus.Subbed
} }
} }
}
override suspend fun getMainPage(): HomePageResponse {
val items = listOf(
Pair("$mainUrl/ajax/home/widget?name=trending", "Trending"),
Pair("$mainUrl/ajax/home/widget?name=updated_all", "All"),
Pair("$mainUrl/ajax/home/widget?name=updated_sub&page=1", "Recently Updated (SUB)"),
Pair(
"$mainUrl/ajax/home/widget?name=updated_dub&page=1",
"Recently Updated (DUB)"
),
Pair(
"$mainUrl/ajax/home/widget?name=updated_chinese&page=1",
"Recently Updated (Chinese)"
),
Pair("$mainUrl/ajax/home/widget?name=random", "Random"),
).apmap { (url, name) ->
val home = Jsoup.parse(
app.get(
url
).parsed<Response>().html
).select("ul.anime-list li").map {
val title = it.selectFirst("a.name")!!.text()
val link = it.selectFirst("a")!!.attr("href")
val poster = it.selectFirst("a.poster img")!!.attr("src")
newAnimeSearchResponse(title, link) { private const val nineAnimeKey =
this.posterUrl = poster "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
addDubStatus(getDubStatus(title)) private const val cipherKey = "rTKp3auwu0ULA6II"
}
}
HomePageList(name, home) private fun encodeVrf(text: String): String {
} return encode(
encrypt(
return HomePageResponse(items) cipher(cipherKey, encode(text)),
} nineAnimeKey
).replace("""=+$""".toRegex(), "")
//Credits to https://github.com/jmir1
private val key =
"c/aUAorINHBLxWTy3uRiPt8J+vjsOheFG1E0q2X9CYwDZlnmd4Kb5M6gSVzfk7pQ" //key credits to @Modder4869
private fun getVrf(id: String): String? {
val reversed = ue(encode(id) + "0000000").slice(0..5).reversed()
return reversed + ue(je(reversed, encode(id) ?: return null)).replace(
"""=+$""".toRegex(),
""
) )
} }
private fun getLink(url: String): String? { private fun decodeVrf(text: String): String {
val i = url.slice(0..5) return decode(cipher(cipherKey, decrypt(text, nineAnimeKey)))
val n = url.slice(6..url.lastIndex)
return decode(je(i, ze(n)))
} }
private fun ue(input: String): String { fun encrypt(input: String, key: String): String {
if (input.any { it.code >= 256 }) throw Exception("illegal characters!") if (input.any { it.code > 255 }) throw Exception("illegal characters!")
var output = "" var output = ""
for (i in input.indices step 3) { for (i in input.indices step 3) {
val a = intArrayOf(-1, -1, -1, -1) val a = intArrayOf(-1, -1, -1, -1)
@ -104,38 +66,39 @@ class NineAnimeProvider : MainAPI() {
} }
} }
} }
return output; return output
} }
private fun je(inputOne: String, inputTwo: String): String { fun cipher(key: String, text: String): String {
val arr = IntArray(256) { it } val arr = IntArray(256) { it }
var output = ""
var u = 0 var u = 0
var r: Int var r: Int
for (a in arr.indices) { arr.indices.forEach {
u = (u + arr[a] + inputOne[a % inputOne.length].code) % 256 u = (u + arr[it] + key[it % key.length].code) % 256
r = arr[a] r = arr[it]
arr[a] = arr[u] arr[it] = arr[u]
arr[u] = r arr[u] = r
} }
u = 0 u = 0
var c = 0 var c = 0
for (f in inputTwo.indices) {
c = (c + f) % 256 return text.indices.map { j ->
c = (c + 1) % 256
u = (u + arr[c]) % 256 u = (u + arr[c]) % 256
r = arr[c] r = arr[c]
arr[c] = arr[u] arr[c] = arr[u]
arr[u] = r arr[u] = r
output += (inputTwo[f].code xor arr[(arr[c] + arr[u]) % 256]).toChar() (text[j].code xor arr[(arr[c] + arr[u]) % 256]).toChar()
} }.joinToString("")
return output
} }
private fun ze(input: String): String { @Suppress("SameParameterValue")
private fun decrypt(input: String, key: String): String {
val t = if (input.replace("""[\t\n\f\r]""".toRegex(), "").length % 4 == 0) { val t = if (input.replace("""[\t\n\f\r]""".toRegex(), "").length % 4 == 0) {
input.replace(Regex("""/==?$/"""), "") input.replace("""==?$""".toRegex(), "")
} else input } else input
if (t.length % 4 == 1 || t.contains(Regex("""[^+/0-9A-Za-z]"""))) throw Exception("bad input") if (t.length % 4 == 1 || t.contains("""[^+/0-9A-Za-z]""".toRegex())) throw Exception("bad input")
var i: Int var i: Int
var r = "" var r = ""
var e = 0 var e = 0
@ -166,100 +129,200 @@ class NineAnimeProvider : MainAPI() {
} }
} }
private fun encode(input: String): String? = java.net.URLEncoder.encode(input, "utf-8") private fun encode(input: String): String =
java.net.URLEncoder.encode(input, "utf-8").replace("+", "%20")
private fun decode(input: String): String? = java.net.URLDecoder.decode(input, "utf-8") private fun decode(input: String): String = java.net.URLDecoder.decode(input, "utf-8")
}
override suspend fun search(query: String): List<SearchResponse> { override suspend fun getMainPage(): HomePageResponse {
val url = "$mainUrl/filter?sort=title%3Aasc&keyword=$query" val items = listOf(
"$mainUrl/ajax/home/widget/trending?page=1" to "Trending",
"$mainUrl/ajax/home/widget/updated-all?page=1" to "All",
"$mainUrl/ajax/home/widget/updated-sub?page=1" to "Recently Updated (SUB)",
"$mainUrl/ajax/home/widget/updated-dub?page=1" to
"Recently Updated (DUB)",
"$mainUrl/ajax/home/widget/updated-china?page=1" to
"Recently Updated (Chinese)",
"$mainUrl/ajax/home/widget/random?page=1" to "Random",
).apmap { (url, name) ->
val home = Jsoup.parse(
app.get(
url
).parsed<Response>().html
).select("div.item").mapNotNull { element ->
val title = element.selectFirst(".info > .name") ?: return@mapNotNull null
val link = title.attr("href")
val poster = element.selectFirst(".poster > a > img")?.attr("src")
val meta = element.selectFirst(".poster > a > .meta > .inner > .left")
val subbedEpisodes = meta?.selectFirst(".sub")?.text()?.toIntOrNull()
val dubbedEpisodes = meta?.selectFirst(".dub")?.text()?.toIntOrNull()
return app.get(url).document.select("ul.anime-list li").mapNotNull { newAnimeSearchResponse(title.text() ?: return@mapNotNull null, link) {
val title = it.selectFirst("a.name")!!.text() this.posterUrl = poster
val href = addDubStatus(
fixUrlNull(it.selectFirst("a")!!.attr("href"))?.replace( dubbedEpisodes != null,
Regex("(\\?ep=(\\d+)\$)"), subbedEpisodes != null,
"" dubbedEpisodes,
) subbedEpisodes
?: return@mapNotNull null
val image = it.selectFirst("a.poster img")!!.attr("src")
AnimeSearchResponse(
title,
href,
this.name,
TvType.Anime,
image,
null,
if (title.contains("(DUB)") || title.contains("(Dub)")) EnumSet.of(
DubStatus.Dubbed
) else EnumSet.of(DubStatus.Subbed),
) )
} }
} }
HomePageList(name, home)
}
return HomePageResponse(items)
}
data class Response( data class Response(
@JsonProperty("html") val html: String @JsonProperty("result") val html: String
) )
override suspend fun load(url: String): LoadResponse? { data class QuickSearchResponse(
//@JsonProperty("status") val status: Int? = null,
@JsonProperty("result") val result: QuickSearchResult? = null,
//@JsonProperty("message") val message: String? = null,
//@JsonProperty("messages") val messages: ArrayList<String> = arrayListOf()
)
data class QuickSearchResult(
@JsonProperty("html") val html: String? = null,
//@JsonProperty("linkMore") val linkMore: String? = null
)
override suspend fun quickSearch(query: String): List<SearchResponse>? {
val vrf = encodeVrf(query)
val url =
"$mainUrl/ajax/anime/search?keyword=$query&vrf=$vrf"
val response = app.get(url).parsedSafe<QuickSearchResponse>()
val document = Jsoup.parse(response?.result?.html ?: return null)
return document.select(".items > a").mapNotNull { element ->
val link = fixUrl(element?.attr("href") ?: return@mapNotNull null)
val title = element.selectFirst(".info > .name")?.text() ?: return@mapNotNull null
newAnimeSearchResponse(title, link) {
posterUrl = element.selectFirst(".poster > span > img")?.attr("src")
}
}
}
override suspend fun search(query: String): List<SearchResponse>? {
val vrf = encodeVrf(query)
//?language%5B%5D=${if (selectDub) "dubbed" else "subbed"}&
val url =
"$mainUrl/filter?keyword=${encode(query)}&vrf=${vrf}&page=1"
return app.get(url).document.select("#list-items div.ani.poster.tip > a").mapNotNull {
val link = fixUrl(it.attr("href") ?: return@mapNotNull null)
val img = it.select("img")
val title = img.attr("alt")
newAnimeSearchResponse(title, link) {
posterUrl = img.attr("src")
}
}
}
override suspend fun load(url: String): LoadResponse {
val validUrl = url.replace("https://9anime.to", mainUrl) val validUrl = url.replace("https://9anime.to", mainUrl)
val doc = app.get(validUrl).document val doc = app.get(validUrl).document
val animeid =
doc.selectFirst("div.player-wrapper.watchpage")!!.attr("data-id") ?: return null
val animeidencoded = encode(getVrf(animeid) ?: return null)
val poster = doc.selectFirst("aside.main div.thumb div img")!!.attr("src")
val title = doc.selectFirst(".info .title")!!.text()
val description = doc.selectFirst("div.info p")!!.text().replace("Ver menos", "").trim()
val episodes = Jsoup.parse(
app.get(
"$mainUrl/ajax/anime/servers?ep=1&id=${animeid}&vrf=$animeidencoded&ep=8&episode=&token="
).parsed<Response>().html
).select("ul.episodes li a").mapNotNull {
val link = it?.attr("href") ?: return@mapNotNull null
val name = "Episode ${it.text()}"
Episode(link, name)
}
val recommendations = val meta = doc.selectFirst("#w-info") ?: throw ErrorLoadingException("Could not find info")
doc.select("div.container aside.main section div.body ul.anime-list li") val ratingElement = meta.selectFirst(".brating > #w-rating")
.mapNotNull { element -> val id = ratingElement?.attr("data-id") ?: throw ErrorLoadingException("Could not find id")
val recTitle = element.select("a.name").text() ?: return@mapNotNull null val binfo =
val image = element.select("a.poster img").attr("src") meta.selectFirst(".binfo") ?: throw ErrorLoadingException("Could not find binfo")
val recUrl = fixUrl(element.select("a").attr("href")) val info = binfo.selectFirst(".info") ?: throw ErrorLoadingException("Could not find info")
newAnimeSearchResponse(recTitle, recUrl) {
this.posterUrl = image val title = (info.selectFirst(".title") ?: info.selectFirst(".d-title"))?.text()
addDubStatus(getDubStatus(recTitle)) ?: throw ErrorLoadingException("Could not find title")
val body =
app.get("$mainUrl/ajax/episode/list/$id?vrf=${encodeVrf(id)}").parsed<Response>().html
val subEpisodes = ArrayList<Episode>()
val dubEpisodes = ArrayList<Episode>()
//TODO RECOMMENDATIONS
Jsoup.parse(body).body().select(".episodes > ul > li > a").mapNotNull { element ->
val ids = element.attr("data-ids").split(",", limit = 2)
val epNum = element.attr("data-num")
.toIntOrNull() // might fuck up on 7.5 ect might use data-slug instead
val epTitle = element.selectFirst("span.d-title")?.text()
//val filler = element.hasClass("filler")
ids.getOrNull(1)?.let { dub ->
dubEpisodes.add(
Episode(
"$mainUrl/ajax/server/list/$dub?vrf=${encodeVrf(dub)}",
epTitle,
episode = epNum
)
)
}
ids.getOrNull(0)?.let { sub ->
subEpisodes.add(
Episode(
"$mainUrl/ajax/server/list/$sub?vrf=${encodeVrf(sub)}",
epTitle,
episode = epNum
)
)
} }
} }
val infodoc = doc.selectFirst("div.info .meta .col1")!!.text() return newAnimeLoadResponse(title, url, TvType.Anime) {
val tvType = if (infodoc.contains("Movie")) TvType.AnimeMovie else TvType.Anime addEpisodes(DubStatus.Dubbed, dubEpisodes)
val status = addEpisodes(DubStatus.Subbed, subEpisodes)
if (infodoc.contains("Completed")) ShowStatus.Completed
else if (infodoc.contains("Airing")) ShowStatus.Ongoing
else null
val tags = doc.select("div.info .meta .col1 div:contains(Genre) a").map { it.text() }
return newAnimeLoadResponse(title, validUrl, tvType) { plot = info.selectFirst(".synopsis > .shorting > .content")?.text()
this.posterUrl = poster posterUrl = binfo.selectFirst(".poster > span > img")?.attr("src")
this.plot = description rating = ratingElement.attr("data-score").toFloat().times(1000f).toInt()
this.recommendations = recommendations
this.showStatus = status info.select(".bmeta > .meta > div").forEach { element ->
this.tags = tags when (element.ownText()) {
addEpisodes(DubStatus.Subbed, episodes) "Genre: " -> {
tags = element.select("span > a").mapNotNull { it?.text() }
}
"Duration: " -> {
duration = getDurationFromString(element.selectFirst("span")?.text())
}
"Type: " -> {
type = when (element.selectFirst("span > a")?.text()) {
"ONA" -> TvType.OVA
else -> {
type
} }
} }
}
"Status: " -> {
showStatus = when (element.selectFirst("span")?.text()) {
"Releasing" -> ShowStatus.Ongoing
"Completed" -> ShowStatus.Completed
else -> {
showStatus
}
}
}
else -> {}
}
}
}
}
data class Result(
@JsonProperty("url")
val url: String? = null
)
data class Links( data class Links(
@JsonProperty("url") val url: String @JsonProperty("result")
val result: Result? = null
) )
data class Servers( //TODO 9anime outro into {"status":200,"result":{"url":"","skip_data":{"intro_begin":67,"intro_end":154,"outro_begin":1337,"outro_end":1415,"count":3}},"message":"","messages":[]}
@JsonProperty("28") val mcloud: String?, private suspend fun getEpisodeLinks(id: String): Links? {
@JsonProperty("35") val mp4upload: String?, return app.get("$mainUrl/ajax/server/$id?vrf=${encodeVrf(id)}").parsedSafe()
@JsonProperty("40") val streamtape: String?, }
@JsonProperty("41") val vidstream: String?,
@JsonProperty("43") val videovard: String?
)
override suspend fun loadLinks( override suspend fun loadLinks(
data: String, data: String,
@ -267,44 +330,29 @@ class NineAnimeProvider : MainAPI() {
subtitleCallback: (SubtitleFile) -> Unit, subtitleCallback: (SubtitleFile) -> Unit,
callback: (ExtractorLink) -> Unit callback: (ExtractorLink) -> Unit
): Boolean { ): Boolean {
val document = app.get(data).document val body = app.get(data).parsed<Response>().html
val animeid = val document = Jsoup.parse(body)
document.selectFirst("div.player-wrapper.watchpage")!!.attr("data-id") ?: return false
val animeidencoded = encode(getVrf(animeid) ?: return false)
Jsoup.parse( document.select("li").apmap {
app.get(
"$mainUrl/ajax/anime/servers?&id=${animeid}&vrf=$animeidencoded&episode=&token="
).parsed<Response>().html
).select("div.body").map { element ->
val jsonregex = Regex("(\\{.+\\}.*$data)")
val servers = jsonregex.find(element.toString())?.value?.replace(
Regex("(\".*data-base=.*href=\"$data)"),
""
)?.replace("&quot;", "\"") ?: return@map
val jsonservers = parseJson<Servers?>(servers) ?: return@map
listOfNotNull(
jsonservers.vidstream,
jsonservers.mcloud,
jsonservers.mp4upload,
jsonservers.streamtape,
jsonservers.videovard
).mapNotNull {
try { try {
val epserver = app.get("$mainUrl/ajax/anime/episode?id=$it").text val name = it.text()
(if (epserver.contains("url")) { val encodedStreamUrl =
parseJson<Links>(epserver) getEpisodeLinks(it.attr("data-link-id"))?.result?.url ?: return@apmap
} else null)?.url?.let { it1 -> getLink(it1.replace("=", "")) } val url = decodeVrf(encodedStreamUrl)
?.replace("/embed/", "/e/") if (!loadExtractor(url, callback = callback, referer = mainUrl)) {
callback(
ExtractorLink(
this.name,
name,
url,
mainUrl,
Qualities.Unknown.value,
url.contains(".m3u8")
)
)
}
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
null
}
}.apmap { url ->
loadExtractor(
url, data, callback
)
} }
} }

View file

@ -228,12 +228,13 @@ abstract class AbstractPlayerFragment(
open fun playerError(exception: Exception) { open fun playerError(exception: Exception) {
fun showToast(message: String, gotoNext: Boolean = false) { fun showToast(message: String, gotoNext: Boolean = false) {
if (!gotoNext || hasNextMirror()) { if (gotoNext && hasNextMirror()) {
showToast( showToast(
activity, activity,
message, message,
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
) )
nextMirror()
} else { } else {
showToast( showToast(
activity, activity,

View file

@ -214,7 +214,6 @@ class QuickSearchFragment : Fragment() {
when (it) { when (it) {
is Resource.Success -> { is Resource.Success -> {
it.value.let { data -> it.value.let { data ->
println("DATA: $data")
(quick_search_autofit_results?.adapter as? SearchAdapter?)?.updateList( (quick_search_autofit_results?.adapter as? SearchAdapter?)?.updateList(
data data
) )

View file

@ -169,7 +169,6 @@ suspend fun loadExtractor(
for (extractor in extractorApis) { for (extractor in extractorApis) {
if (url.startsWith(extractor.mainUrl)) { if (url.startsWith(extractor.mainUrl)) {
return extractor.getSafeUrl(url, referer) ?: emptyList() return extractor.getSafeUrl(url, referer) ?: emptyList()
} }
} }
return emptyList() return emptyList()