Skip to content

Commit

Permalink
Animeworld 202 fix (Kohi-den#581)
Browse files Browse the repository at this point in the history
* automatic session cookie retrieval

* automatic session cookie retrieval

---------

Co-authored-by: giorgio <[email protected]>
  • Loading branch information
giorgionegro and giorgio authored Jan 22, 2025
1 parent 7e56b69 commit bf54b7c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/it/animeworld/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
extName = 'ANIMEWORLD.tv'
extClass = '.ANIMEWORLD'
extVersionCode = 41
extVersionCode = 42
}

apply from: "$rootDir/common.gradle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.util.asJsoup
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Request
import okhttp3.Response
Expand All @@ -28,6 +29,7 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.lang.Exception
import java.util.regex.Pattern

class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {

Expand All @@ -39,6 +41,9 @@ class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {

override val lang = "it"

private var token: String = ""
private var lastTokenRefresh: Long = -1

override val supportsLatest = true

private val json: Json by injectLazy()
Expand All @@ -47,10 +52,42 @@ class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}

fun extractCookie(html: String): String? {
val regex = """document\.cookie="([^"]+);"""
val pattern = Pattern.compile(regex)
val matcher = pattern.matcher(html)

return if (matcher.find()) {
matcher.group(1) // Extract the cookie value
} else {
null
}
}

fun localGet(
url: String,
headers: Headers? = null,
): Request {
if (lastTokenRefresh == -1L || System.currentTimeMillis() - lastTokenRefresh > 1000 * 60 * 60 * 24) {
val response = client.newCall(GET(baseUrl)).execute()
val body = response.body!!.string()
val cookie = extractCookie(body)
if (cookie != null) {
token = cookie
lastTokenRefresh = System.currentTimeMillis()
}
}
if (headers == null) {
return GET(url, headers = Headers.headersOf("Cookie", token))
}

return GET(url, headers = headers.newBuilder().add("Cookie", token).build())
}

// Popular Anime - Same Format as Search

override fun popularAnimeSelector(): String = searchAnimeSelector()
override fun popularAnimeRequest(page: Int): Request = GET("$baseUrl/filter?sort=6&page=$page")
override fun popularAnimeRequest(page: Int): Request = localGet("$baseUrl/filter?sort=6&page=$page")
override fun popularAnimeFromElement(element: Element): SAnime = searchAnimeFromElement(element)
override fun popularAnimeNextPageSelector(): String = searchAnimeNextPageSelector()

Expand Down Expand Up @@ -82,7 +119,7 @@ class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {

override fun videoListRequest(episode: SEpisode): Request {
val iframe = baseUrl + episode.url
return GET(iframe)
return localGet(iframe)
}

override fun videoListParse(response: Response): List<Video> {
Expand Down Expand Up @@ -122,7 +159,7 @@ class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
.add("X-Requested-With", "XMLHttpRequest")
.build()
val target = json.decodeFromString<ServerResponse>(
client.newCall(GET(apiUrl, headers = apiHeaders)).execute().body.string(),
client.newCall(localGet(apiUrl, headers = apiHeaders)).execute().body.string(),
).grabber
serverList.add(Pair(serverPair.first, target))
}
Expand Down Expand Up @@ -187,7 +224,7 @@ class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
override fun searchAnimeNextPageSelector(): String = "div.paging-wrapper a#go-next-page"

override fun searchAnimeRequest(page: Int, query: String, filters: AnimeFilterList): Request =
GET("$baseUrl/filter?${getSearchParameters(filters)}&keyword=$query&page=$page")
localGet("$baseUrl/filter?${getSearchParameters(filters)}&keyword=$query&page=$page")

// Details

Expand All @@ -213,7 +250,7 @@ class ANIMEWORLD : ConfigurableAnimeSource, ParsedAnimeHttpSource() {

// Latest - Same format as search

override fun latestUpdatesRequest(page: Int): Request = GET("$baseUrl/updated?page=$page")
override fun latestUpdatesRequest(page: Int): Request = localGet("$baseUrl/updated?page=$page")
override fun latestUpdatesSelector(): String = searchAnimeSelector()
override fun latestUpdatesNextPageSelector(): String = searchAnimeNextPageSelector()
override fun latestUpdatesFromElement(element: Element): SAnime = searchAnimeFromElement(element)
Expand Down

0 comments on commit bf54b7c

Please sign in to comment.