Skip to content

Commit

Permalink
token interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
imper1aldev committed Jan 28, 2024
1 parent 66d756b commit b0ad449
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
14 changes: 13 additions & 1 deletion YomirollProvider/src/main/kotlin/com/lagradost/TokenUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import android.net.Uri
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
import com.lagradost.nicehttp.requestCreator
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.net.InetSocketAddress
import java.net.PasswordAuthentication
import java.net.*
import java.text.MessageFormat
import java.text.SimpleDateFormat
import java.util.Locale

class TokenUtils {
class TokenUtils : Interceptor {
private val crUrl = "https://beta-api.crunchyroll.com"
private val crBasicToken = "b2VkYXJteHN0bGgxanZhd2ltbnE6OWxFaHZIWkpEMzJqdVY1ZFc5Vk9TNTdkb3BkSnBnbzE="

Expand Down Expand Up @@ -105,4 +107,14 @@ class TokenUtils {
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH)
}
}

override fun intercept(chain: Interceptor.Chain): Response {
synchronized(this) {
val refreshedToken = getAccessToken()
// Retry the request
return chain.proceed(
newRequestWithAccessToken(chain.request(), refreshedToken),
)
}
}
}
23 changes: 15 additions & 8 deletions YomirollProvider/src/main/kotlin/com/lagradost/YomirollProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.M3u8Helper
import com.lagradost.nicehttp.requestCreator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import okhttp3.HttpUrl.Companion.toHttpUrl
import java.text.SimpleDateFormat
import java.util.*
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import okhttp3.Request

class YomirollProvider : MainAPI() {

Expand All @@ -25,7 +27,8 @@ class YomirollProvider : MainAPI() {
override val hasMainPage = true
override val hasChromecastSupport = true
override val hasDownloadSupport = true
private val tokenUtils = TokenUtils()
//private val tokenUtils = TokenUtils()
private val tokenInterceptor by lazy { TokenUtils() }

private fun parseDate(dateStr: String): Long {
return runCatching { DateFormatter.parse(dateStr)?.time }.getOrNull() ?: 0L
Expand Down Expand Up @@ -59,7 +62,7 @@ class YomirollProvider : MainAPI() {
val url = request.data.replace("{start}", start)
val position = request.data.toHttpUrl().queryParameter("start")?.toIntOrNull() ?: 0

val parsed = app.get(url, headers = tokenUtils.getCrunchyrollToken()).parsed<AnimeResult>()
val parsed = app.get(url, interceptor = tokenInterceptor).parsed<AnimeResult>()
val hasNextPage = position + 36 < parsed.total

val home = parsed.data.map {
Expand All @@ -82,7 +85,7 @@ class YomirollProvider : MainAPI() {

override suspend fun search(query: String): List<SearchResponse> {
val url = "$mainUrl/?s=$query"
val soup = app.get(url).document
val soup = app.get(url, interceptor = tokenInterceptor).document
return soup.select(".ml-item").map {
val title = it.select("a .mli-info h2").text()
val poster = it.select("a img").attr("data-original")
Expand All @@ -109,7 +112,7 @@ class YomirollProvider : MainAPI() {
} else {
"$crApiUrl/cms/movie_listings/${anime.id}?locale=en-US"
},
headers = tokenUtils.getCrunchyrollToken()
interceptor = tokenInterceptor
).parsed<AnimeResult>().data.first()

val title = anime.title
Expand Down Expand Up @@ -162,7 +165,7 @@ class YomirollProvider : MainAPI() {
} else {
"$crApiUrl/cms/movie_listings/${anime.id}/movies"
},
headers = tokenUtils.getCrunchyrollToken()
interceptor = tokenInterceptor
).parsed<SeasonResult>()

val chunkSize = Runtime.getRuntime().availableProcessors()
Expand Down Expand Up @@ -196,7 +199,7 @@ class YomirollProvider : MainAPI() {
private suspend fun getEpisodes(seasonData: SeasonResult.Season): List<Episode> {
val episodes = app.get(
"$crApiUrl/cms/seasons/${seasonData.id}/episodes",
headers = tokenUtils.getCrunchyrollToken()
interceptor = tokenInterceptor
).parsed<EpisodeResult>()
return episodes.data.sortedBy { it.episode_number }.mapNotNull EpisodeMap@{ ep ->
Episode(
Expand Down Expand Up @@ -264,8 +267,8 @@ class YomirollProvider : MainAPI() {
val (mediaId, audioL) = it
val streams = app.get(
"https://beta-api.crunchyroll.com/content/v2/cms/videos/$mediaId/streams",
headers = tokenUtils.getCrunchyrollToken(),
timeout = 20000
interceptor = tokenInterceptor,
timeout = 30000
).parsedSafe<CrunchyrollSourcesResponses>()

val audLang = audioL.ifBlank { streams?.meta?.audio_locale } ?: "ja-JP"
Expand Down Expand Up @@ -311,6 +314,10 @@ class YomirollProvider : MainAPI() {
return true
}

private fun getVideoRequest(mediaId: String): Request {
return requestCreator("GET", "$crUrl/cms/v2{0}/videos/$mediaId/streams?Policy={1}&Signature={2}&Key-Pair-Id={3}",)
}

companion object {
val DateFormatter by lazy {
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH)
Expand Down

0 comments on commit b0ad449

Please sign in to comment.