From deda67a070ea3387decce2f2a618551efafea201 Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Wed, 1 May 2024 20:12:05 -0500 Subject: [PATCH] fix: more network stuffs --- .../ani/dantotsu/media/SubtitleDownloader.kt | 40 +++++++++++-------- .../UncaughtExceptionInterceptor.kt | 5 +++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/media/SubtitleDownloader.kt b/app/src/main/java/ani/dantotsu/media/SubtitleDownloader.kt index 503f1732..2519afb8 100644 --- a/app/src/main/java/ani/dantotsu/media/SubtitleDownloader.kt +++ b/app/src/main/java/ani/dantotsu/media/SubtitleDownloader.kt @@ -5,6 +5,7 @@ import ani.dantotsu.download.DownloadedType import ani.dantotsu.download.DownloadsManager import ani.dantotsu.parsers.SubtitleType import ani.dantotsu.snackString +import ani.dantotsu.util.Logger import com.anggrayudi.storage.file.openOutputStream import eu.kanade.tachiyomi.network.NetworkHelper import kotlinx.coroutines.Dispatchers @@ -19,28 +20,33 @@ class SubtitleDownloader { //doesn't really download the subtitles -\_(o_o)_/- suspend fun loadSubtitleType(url: String): SubtitleType = withContext(Dispatchers.IO) { - // Initialize the NetworkHelper instance. Replace this line based on how you usually initialize it - val networkHelper = Injekt.get() - val request = Request.Builder() - .url(url) - .build() + return@withContext try { + // Initialize the NetworkHelper instance. Replace this line based on how you usually initialize it + val networkHelper = Injekt.get() + val request = Request.Builder() + .url(url) + .build() - val response = networkHelper.client.newCall(request).execute() + val response = networkHelper.client.newCall(request).execute() - // Check if response is successful - if (response.isSuccessful) { - val responseBody = response.body.string() + // Check if response is successful + if (response.isSuccessful) { + val responseBody = response.body.string() - val subtitleType = when { - responseBody.contains("[Script Info]") -> SubtitleType.ASS - responseBody.contains("WEBVTT") -> SubtitleType.VTT - else -> SubtitleType.SRT + val subtitleType = when { + responseBody.contains("[Script Info]") -> SubtitleType.ASS + responseBody.contains("WEBVTT") -> SubtitleType.VTT + else -> SubtitleType.SRT + } + + subtitleType + } else { + SubtitleType.UNKNOWN } - - return@withContext subtitleType - } else { - return@withContext SubtitleType.UNKNOWN + } catch (e: Exception) { + Logger.log(e) + SubtitleType.UNKNOWN } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UncaughtExceptionInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UncaughtExceptionInterceptor.kt index 1de82438..dccfd469 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UncaughtExceptionInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UncaughtExceptionInterceptor.kt @@ -1,8 +1,10 @@ package eu.kanade.tachiyomi.network.interceptor +import ani.dantotsu.util.Logger import okhttp3.Interceptor import okhttp3.Response import java.io.IOException +import java.net.SocketTimeoutException /** * Catches any uncaught exceptions from later in the chain and rethrows as a non-fatal @@ -17,6 +19,9 @@ class UncaughtExceptionInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { return try { chain.proceed(chain.request()) + } catch (e: SocketTimeoutException) { + Logger.log(e) + throw IOException("Request timed out") // there's some odd behavior throwing a SocketTimeoutException } catch (e: Exception) { if (e is IOException) { throw e