From 0dd66ffdeabd00404c0642ac67a53912ea42a86c Mon Sep 17 00:00:00 2001 From: Jace <54625750+Jacekun@users.noreply.github.com> Date: Wed, 12 Oct 2022 12:31:23 +0800 Subject: [PATCH] Cleanups on Hanime --- Hanime/src/main/kotlin/com/jacekun/Hanime.kt | 69 ++++++++++++++------ 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/Hanime/src/main/kotlin/com/jacekun/Hanime.kt b/Hanime/src/main/kotlin/com/jacekun/Hanime.kt index b6b07a7..a3e85f6 100644 --- a/Hanime/src/main/kotlin/com/jacekun/Hanime.kt +++ b/Hanime/src/main/kotlin/com/jacekun/Hanime.kt @@ -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() + 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__=(.*?);""").find(data)!!.destructured.component1() - val json = mapper.readValue(jsonText) + val requestGet = app.get("https://hanime.tv/") + globalHeaders = requestGet.headers.toMap() + val data = requestGet.text + val jsonText = Regex("""window\.__NUXT__=(.*?);""").find(data)?.destructured?.component1() val titles = ArrayList() val items = ArrayList() - try { + tryParseJson(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?, - @JsonProperty("cover_url") val coverUrl : String, - @JsonProperty("tags") val tags : List, + @JsonProperty("cover_url") val coverUrl : String?, + @JsonProperty("tags") val tags : List?, @JsonProperty("released_at") val releasedAt : Int ) override suspend fun search(query: String): ArrayList { val link = "https://search.htv-services.com/" - val data = mapOf("search_text" to query, "tags" to listOf(), "tags_mode" to "AND", "brands" to listOf(), "blacklist" to listOf(), "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>(it) } + val data = mapOf( + "search_text" to query, + "tags" to emptyList(), + "tags_mode" to "AND", + "brands" to emptyList(), + "blacklist" to emptyList(), + "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() val searchResults = ArrayList() - response.reversed().forEach { - val title = getTitle(it.name) + Log.i(DEV, "Response => (${response.code}) ${responseText}") + tryParseJson?>(responseText)?.reversed()?.forEach { + val rawName = it?.name ?: return@forEach + val title = getTitle(rawName) if (!titles.contains(title)) { titles.add(title) searchResults.add(