fix: more network stuffs

This commit is contained in:
rebelonion 2024-05-01 20:12:05 -05:00
parent e32bfa0cfa
commit deda67a070
2 changed files with 28 additions and 17 deletions

View file

@ -5,6 +5,7 @@ import ani.dantotsu.download.DownloadedType
import ani.dantotsu.download.DownloadsManager import ani.dantotsu.download.DownloadsManager
import ani.dantotsu.parsers.SubtitleType import ani.dantotsu.parsers.SubtitleType
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.util.Logger
import com.anggrayudi.storage.file.openOutputStream import com.anggrayudi.storage.file.openOutputStream
import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.NetworkHelper
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -19,28 +20,33 @@ class SubtitleDownloader {
//doesn't really download the subtitles -\_(o_o)_/- //doesn't really download the subtitles -\_(o_o)_/-
suspend fun loadSubtitleType(url: String): SubtitleType = suspend fun loadSubtitleType(url: String): SubtitleType =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
// Initialize the NetworkHelper instance. Replace this line based on how you usually initialize it return@withContext try {
val networkHelper = Injekt.get<NetworkHelper>() // Initialize the NetworkHelper instance. Replace this line based on how you usually initialize it
val request = Request.Builder() val networkHelper = Injekt.get<NetworkHelper>()
.url(url) val request = Request.Builder()
.build() .url(url)
.build()
val response = networkHelper.client.newCall(request).execute() val response = networkHelper.client.newCall(request).execute()
// Check if response is successful // Check if response is successful
if (response.isSuccessful) { if (response.isSuccessful) {
val responseBody = response.body.string() val responseBody = response.body.string()
val subtitleType = when { val subtitleType = when {
responseBody.contains("[Script Info]") -> SubtitleType.ASS responseBody.contains("[Script Info]") -> SubtitleType.ASS
responseBody.contains("WEBVTT") -> SubtitleType.VTT responseBody.contains("WEBVTT") -> SubtitleType.VTT
else -> SubtitleType.SRT else -> SubtitleType.SRT
}
subtitleType
} else {
SubtitleType.UNKNOWN
} }
} catch (e: Exception) {
return@withContext subtitleType Logger.log(e)
} else { SubtitleType.UNKNOWN
return@withContext SubtitleType.UNKNOWN
} }
} }

View file

@ -1,8 +1,10 @@
package eu.kanade.tachiyomi.network.interceptor package eu.kanade.tachiyomi.network.interceptor
import ani.dantotsu.util.Logger
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import java.io.IOException import java.io.IOException
import java.net.SocketTimeoutException
/** /**
* Catches any uncaught exceptions from later in the chain and rethrows as a non-fatal * 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 { override fun intercept(chain: Interceptor.Chain): Response {
return try { return try {
chain.proceed(chain.request()) 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) { } catch (e: Exception) {
if (e is IOException) { if (e is IOException) {
throw e throw e