Skip to content

Commit

Permalink
Fix corupted cover image issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeyatogod committed Sep 3, 2023
1 parent 6fb8e18 commit d770b94
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.talent.animescrap.animesources

import android.content.Context
import com.talent.animescrap.animesources.sourceutils.AndroidCookieJar
import com.talent.animescrap.animesources.sourceutils.CloudflareInterceptor
import com.talent.animescrap.model.AnimeDetails
import com.talent.animescrap.model.AnimeStreamLink
import com.talent.animescrap.model.SimpleAnime
import com.talent.animescrap.utils.Utils.getJson
import com.talent.animescrap.utils.Utils.httpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.*
Expand All @@ -16,13 +16,11 @@ import kotlin.math.pow

class AnimePaheSource(context: Context) : AnimeSource {

private val client = OkHttpClient.Builder()
.cookieJar(AndroidCookieJar())
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.callTimeout(2, TimeUnit.MINUTES)
.addInterceptor(CloudflareInterceptor(context))
.build()

init {
httpClient = httpClient.newBuilder().addInterceptor(CloudflareInterceptor(context))
.build()
}

private var cookies: String = ""
private val kwikParamsRegex = Regex("""\("(\w+)",\d+,"(\w+)",(\d+),(\d+),\d+\)""")
Expand All @@ -40,7 +38,7 @@ class AnimePaheSource(context: Context) : AnimeSource {

// For Anime Details
val detailsUrl = "$mainUrl/anime/$session"
val detailsHtml = client.newCall(Request.Builder().url(detailsUrl).build())
val detailsHtml = httpClient.newCall(Request.Builder().url(detailsUrl).build())
.execute().body!!.string()
val doc = Jsoup.parse(detailsHtml)

Expand Down Expand Up @@ -135,7 +133,7 @@ class AnimePaheSource(context: Context) : AnimeSource {
extras: List<String>?
): AnimeStreamLink =
withContext(Dispatchers.IO) {
println("anime url = " + animeUrl)
println("anime url = $animeUrl")
/*val animeId =
animeUrl.replaceAfter("AnimePaheSession", "").replace("AnimePaheSession", "")
.replace("AnimePaheId=", "")*/
Expand All @@ -145,7 +143,7 @@ class AnimePaheSource(context: Context) : AnimeSource {
println(urlForLinks)
val playUrl = "$mainUrl/play/$animeSession/$animeEpCode"
println(playUrl)
val d = client.newCall(Request.Builder().url(playUrl).build())
val d = httpClient.newCall(Request.Builder().url(playUrl).build())
.execute().body!!.string()
val jDoc = Jsoup.parse(d)
val allLinks = jDoc.select("#pickDownload a").filter { source ->
Expand All @@ -165,7 +163,7 @@ class AnimePaheSource(context: Context) : AnimeSource {

private fun getStreamUrlFromKwik(paheUrl: String): String {

val noRedirects = client.newBuilder()
val noRedirects = httpClient.newBuilder()
.followRedirects(false)
.followSslRedirects(false)
.build()
Expand All @@ -174,7 +172,7 @@ class AnimePaheSource(context: Context) : AnimeSource {
.header("location")!!.substringAfterLast("https://")
println(kwikUrl)
val fContent =
client.newCall(
httpClient.newCall(
Request.Builder().url(kwikUrl).header("referer", "https://kwik.cx/").build()
).execute()
cookies += (fContent.header("set-cookie")!!)
Expand All @@ -195,7 +193,7 @@ class AnimePaheSource(context: Context) : AnimeSource {
val noRedirectClient = OkHttpClient().newBuilder()
.followRedirects(false)
.followSslRedirects(false)
.cookieJar(client.cookieJar)
.cookieJar(httpClient.cookieJar)
.build()

while (code != 302 && tries < 20) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@ package com.talent.animescrap.animesources

import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.talent.animescrap.animesources.sourceutils.AndroidCookieJar
import com.talent.animescrap.animesources.sourceutils.DdosGuardInterceptor
import com.talent.animescrap.model.AnimeDetails
import com.talent.animescrap.model.AnimeStreamLink
import com.talent.animescrap.model.SimpleAnime
import okhttp3.OkHttpClient
import com.talent.animescrap.utils.Utils.httpClient
import okhttp3.Request
import okhttp3.Response
import org.jsoup.Jsoup
import java.util.concurrent.TimeUnit

class MarinMoeSource : AnimeSource {
private val mainUrl = "https://marin.moe"

private val httpClient = OkHttpClient.Builder()
.cookieJar(AndroidCookieJar())
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.callTimeout(2, TimeUnit.MINUTES)
.build()

private val client = httpClient.newBuilder().addInterceptor(DdosGuardInterceptor(httpClient))
.build()
init {
httpClient = httpClient.newBuilder().addInterceptor(DdosGuardInterceptor(httpClient))
.build()
}

override suspend fun searchAnime(searchedText: String): ArrayList<SimpleAnime> {
return getAnimeList("$mainUrl/anime?sort=rel-d&search=$searchedText")
}

override suspend fun latestAnime(): ArrayList<SimpleAnime> {
return getAnimeList("$mainUrl/anime?sort=rel-d&page=1")
return getEpisodeList("$mainUrl/episode?sort=rel-d&page=1")
}

override suspend fun trendingAnime(): ArrayList<SimpleAnime> {
Expand All @@ -58,6 +52,26 @@ class MarinMoeSource : AnimeSource {

}

private fun getEpisodeList(url: String): ArrayList<SimpleAnime> {
val res = get(url)
val resJson = parseJson(res)
val animeList = arrayListOf<SimpleAnime>()
resJson.asJsonObject["props"].asJsonObject["episode_list"].asJsonObject["data"].asJsonArray
.forEach { e ->
val anime = e.asJsonObject["anime"].asJsonObject
animeList.add(
SimpleAnime(
anime["title"].asString,
e.asJsonObject["cover"].asString,
"/anime/${anime["slug"].asString}"
)
)

}
return animeList

}

override suspend fun animeDetails(contentLink: String): AnimeDetails {
val res = get("$mainUrl$contentLink?sort=srt-d")
val resJson = parseJson(res)
Expand Down Expand Up @@ -94,7 +108,7 @@ class MarinMoeSource : AnimeSource {
println(videoLink)
println(videoSubs)

val ddosCookies = client.cookieJar.loadForRequest(res.request.url).filter {
val ddosCookies = httpClient.cookieJar.loadForRequest(res.request.url).filter {
it.name !in arrayOf("__ddgid_", "__ddgmark_")
}.joinToString(";") { "${it.name}=${it.value}" }

Expand Down Expand Up @@ -123,13 +137,13 @@ class MarinMoeSource : AnimeSource {

private fun get(url: String): String {
val requestBuilder = Request.Builder().url(url)
return client.newCall(requestBuilder.build())
return httpClient.newCall(requestBuilder.build())
.execute().body!!.string()
}

private fun getResponse(url: String): Response {
val requestBuilder = Request.Builder().url(url)
return client.newCall(requestBuilder.build())
return httpClient.newCall(requestBuilder.build())
.execute()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.preference.PreferenceManager
import coil.ImageLoader
import coil.load
import com.facebook.shimmer.Shimmer
import com.facebook.shimmer.ShimmerDrawable
Expand All @@ -33,6 +34,7 @@ import com.talent.animescrap.model.AnimePlayingDetails
import com.talent.animescrap.model.AnimeStreamLink
import com.talent.animescrap.ui.viewmodels.AnimeDetailsViewModel
import com.talent.animescrap.ui.viewmodels.AnimeStreamViewModel
import com.talent.animescrap.utils.Utils
import dagger.hilt.android.AndroidEntryPoint


Expand Down Expand Up @@ -74,7 +76,9 @@ class AnimeFragment : Fragment() {
savedInstanceState: Bundle?
): View {
_binding = FragmentAnimeBinding.inflate(inflater, container, false)

val imageLoader = ImageLoader.Builder(requireContext())
.okHttpClient { Utils.httpClient }
.build()
settingsPreferenceManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
selectedSource = settingsPreferenceManager.getString("source", "yugen")!!
isExternalPlayerEnabled = settingsPreferenceManager.getBoolean("external_player", false)
Expand Down Expand Up @@ -138,14 +142,14 @@ class AnimeFragment : Fragment() {
binding.animeDetailsTxt.text = animeDetails.animeDesc

// load background image.
binding.backgroundImage.load(animeDetails.animeCover) {
binding.backgroundImage.load(animeDetails.animeCover, imageLoader) {
placeholder(ShimmerDrawable().apply {
setShimmer(shimmer)
})
error(R.drawable.ic_broken_image)
}
// load cover image.
binding.coverAnime.load(animeDetails.animeCover) {
binding.coverAnime.load(animeDetails.animeCover, imageLoader) {
placeholder(ShimmerDrawable().apply {
setShimmer(shimmer)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ class LatestFragment : Fragment() {
.getString("source", "yugen")
}
private val rvAdapter by lazy {
AnimeRecyclerAdapter(if (selectedSource == "animepahe" || selectedSource == "kiss_kh") "landscape card" else "portrait card")
AnimeRecyclerAdapter(
if (selectedSource in arrayListOf(
"animepahe",
"kiss_kh",
"marin_moe"
)
) "landscape card" else "portrait card"
)
}

// This property is only valid between onCreateView and
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/talent/animescrap/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package com.talent.animescrap.utils
import com.github.kittinunf.fuel.Fuel
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.talent.animescrap.animesources.sourceutils.AndroidCookieJar
import okhttp3.OkHttpClient
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.util.concurrent.TimeUnit

object Utils {

var httpClient = OkHttpClient.Builder()
.cookieJar(AndroidCookieJar())
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.callTimeout(2, TimeUnit.MINUTES)
.build()

fun getJsoup(
url: String,
mapOfHeaders: Map<String, String>? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.talent.animescrap.viewbindings

import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.databinding.BindingAdapter
import coil.ImageLoader
import coil.load
import com.facebook.shimmer.Shimmer
import com.facebook.shimmer.ShimmerDrawable
import com.talent.animescrap.R
import com.talent.animescrap.utils.Utils

private val shimmer =
Shimmer.AlphaHighlightBuilder()// The attributes for a ShimmerDrawable is set by this builder
Expand All @@ -20,8 +20,11 @@ private val shimmer =
/* A function that is called when the `image` attribute is used in the layout. */
@BindingAdapter("image")
fun ImageView.setImage(url: String?) {
val imageLoader = ImageLoader.Builder(context)
.okHttpClient { Utils.httpClient }
.build()
if (!url.isNullOrEmpty())
load(url) {
load(url, imageLoader) {
placeholder(ShimmerDrawable().apply {
setShimmer(shimmer)
})
Expand Down

0 comments on commit d770b94

Please sign in to comment.