Cleanups on Hanime

This commit is contained in:
Jace 2022-10-12 12:31:23 +08:00
parent 019a3bcb2c
commit 0dd66ffdea

View file

@ -3,11 +3,13 @@ package com.jacekun
import com.lagradost.cloudstream3.MainAPI
import com.lagradost.cloudstream3.TvType
import android.annotation.SuppressLint
import android.util.Log
import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.getQualityFromName
import com.fasterxml.jackson.module.kotlin.readValue
import com.lagradost.cloudstream3.network.CloudflareKiller
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
import java.text.SimpleDateFormat
import java.util.*
@ -17,6 +19,16 @@ import kotlin.collections.ArrayList
class Hanime : MainAPI() {
private val globalTvType = TvType.NSFW
private val interceptor = CloudflareKiller()
private var globalHeaders = mapOf<String, String>()
private val DEV = "DevDebug"
override var mainUrl = "https://hanime.tv"
override var name = "Hanime"
override val hasQuickSearch = false
override val hasMainPage = true
override val hasDownloadSupport = true
override val supportedTypes = setOf(TvType.NSFW)
companion object {
@SuppressLint("SimpleDateFormat")
@ -43,13 +55,6 @@ class Hanime : MainAPI() {
}
}
override var mainUrl = "https://hanime.tv"
override var name = "Hanime"
override val hasQuickSearch = false
override val hasMainPage = true
override val hasDownloadSupport = true
override val supportedTypes = setOf(TvType.NSFW)
private data class HpHentaiVideos (
@JsonProperty("id") val id : Int,
@JsonProperty("name") val name : String,
@ -90,15 +95,18 @@ class Hanime : MainAPI() {
request: MainPageRequest
): HomePageResponse {
val data = app.get("https://hanime.tv/").text
val jsonText = Regex("""window\.__NUXT__=(.*?);</script>""").find(data)!!.destructured.component1()
val json = mapper.readValue<HpHanimeHomePage>(jsonText)
val requestGet = app.get("https://hanime.tv/")
globalHeaders = requestGet.headers.toMap()
val data = requestGet.text
val jsonText = Regex("""window\.__NUXT__=(.*?);</script>""").find(data)?.destructured?.component1()
val titles = ArrayList<String>()
val items = ArrayList<HomePageList>()
try {
tryParseJson<HpHanimeHomePage?>(jsonText)?.let { json ->
json.state.data.landing.sections.forEach { section ->
items.add(HomePageList(section.title, (section.hentaiVideoIds.map {
items.add(HomePageList(
section.title,
(section.hentaiVideoIds.map {
val hentai = getHentaiByIdFromList(it, json.state.data.landing.hentaiVideos)!!
val title = getTitle(hentai.name)
if (!titles.contains(title)) {
@ -117,8 +125,6 @@ class Hanime : MainAPI() {
}
}).filterNotNull()))
}
} catch (e: Exception) {
e.printStackTrace()
}
if (items.size <= 0) throw ErrorLoadingException()
@ -130,20 +136,43 @@ class Hanime : MainAPI() {
@JsonProperty("name") val name : String,
@JsonProperty("slug") val slug : String,
@JsonProperty("titles") val titles : List<String>?,
@JsonProperty("cover_url") val coverUrl : String,
@JsonProperty("tags") val tags : List<String>,
@JsonProperty("cover_url") val coverUrl : String?,
@JsonProperty("tags") val tags : List<String>?,
@JsonProperty("released_at") val releasedAt : Int
)
override suspend fun search(query: String): ArrayList<SearchResponse> {
val link = "https://search.htv-services.com/"
val data = mapOf("search_text" to query, "tags" to listOf<String>(), "tags_mode" to "AND", "brands" to listOf<String>(), "blacklist" to listOf<String>(), "order_by" to "created_at_unix", "ordering" to "desc", "page" to 0)
val response = khttp.post(link, json=data).jsonObject.getString("hits").let { mapper.readValue<List<HanimeSearchResult>>(it) }
val data = mapOf(
"search_text" to query,
"tags" to emptyList<String>(),
"tags_mode" to "AND",
"brands" to emptyList<String>(),
"blacklist" to emptyList<String>(),
"order_by" to "created_at_unix",
"ordering" to "desc",
"page" to 0
)
val headers = mapOf(
Pair("Origin", mainUrl),
Pair("Sec-Fetch-Mode", "cors"),
Pair("Sec-Fetch-Site", "cross-site"),
Pair("TE", "trailers"),
Pair("User-Agent", USER_AGENT),
)
val response = app.post(
url = link,
json = data,
headers = globalHeaders
)
val responseText = response.text
val titles = ArrayList<String>()
val searchResults = ArrayList<SearchResponse>()
response.reversed().forEach {
val title = getTitle(it.name)
Log.i(DEV, "Response => (${response.code}) ${responseText}")
tryParseJson<List<HanimeSearchResult?>?>(responseText)?.reversed()?.forEach {
val rawName = it?.name ?: return@forEach
val title = getTitle(rawName)
if (!titles.contains(title)) {
titles.add(title)
searchResults.add(