|
| 1 | +package com.gapps.library.api |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.util.Log |
| 5 | +import com.gapps.library.api.models.api.base.VideoInfoModel |
| 6 | +import com.gapps.library.api.models.video.VideoPreviewModel |
| 7 | +import com.gapps.library.api.models.video.base.BaseVideoResponse |
| 8 | +import com.gapps.library.cache.getCachedVideoModel |
| 9 | +import com.gapps.library.cache.insertModel |
| 10 | +import com.gapps.library.utils.errors.ERROR_2 |
| 11 | +import com.gapps.library.utils.errors.ERROR_3 |
| 12 | +import com.google.gson.GsonBuilder |
| 13 | +import com.google.gson.JsonElement |
| 14 | +import com.google.gson.JsonParser.parseString |
| 15 | +import kotlinx.coroutines.* |
| 16 | +import okhttp3.OkHttpClient |
| 17 | +import okhttp3.Request |
| 18 | +import java.lang.reflect.Type |
| 19 | +import kotlin.coroutines.CoroutineContext |
| 20 | + |
| 21 | +internal class VideoLoadHelper( |
| 22 | + private val context: Context?, |
| 23 | + private val client: OkHttpClient, |
| 24 | + private val isCacheEnabled: Boolean, |
| 25 | + private val isLogEnabled: Boolean, |
| 26 | +) : CoroutineScope { |
| 27 | + override val coroutineContext: CoroutineContext |
| 28 | + get() = SupervisorJob() + Dispatchers.Main |
| 29 | + |
| 30 | + private val gson = GsonBuilder() |
| 31 | + .setLenient() |
| 32 | + .setPrettyPrinting() |
| 33 | + .create() |
| 34 | + |
| 35 | + fun getVideoInfo( |
| 36 | + originalUrl: String?, |
| 37 | + videoInfoModel: VideoInfoModel<*>, |
| 38 | + onSuccess: (VideoPreviewModel) -> Unit, |
| 39 | + onError: (String, String) -> Unit, |
| 40 | + ) { |
| 41 | + val finalUrl = videoInfoModel.getInfoUrl(originalUrl) |
| 42 | + val videoId = videoInfoModel.parseVideoId(originalUrl) |
| 43 | + |
| 44 | + if (finalUrl == null || videoId == null) { |
| 45 | + onError.invoke(originalUrl ?: "null url", ERROR_3) |
| 46 | + |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + val playLink = videoInfoModel.getPlayLink(videoId) |
| 51 | + |
| 52 | + launch { |
| 53 | + if (isCacheEnabled) { |
| 54 | + if (context != null) { |
| 55 | + try { |
| 56 | + val model = getCachedVideoModel(context, playLink) |
| 57 | + |
| 58 | + if (model != null) { |
| 59 | + onSuccess.invoke(model) |
| 60 | + return@launch |
| 61 | + } |
| 62 | + } catch (e: Exception) { |
| 63 | + e.printStackTrace() |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + try { |
| 68 | + val jsonBody = makeCallGetBody(client, finalUrl) |
| 69 | + |
| 70 | + if (isLogEnabled) { |
| 71 | + Log.i( |
| 72 | + VideoService.TAG, |
| 73 | + "a response from $originalUrl:\n${gson.toJson(jsonBody)}" |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + if (jsonBody == null) { |
| 78 | + onSuccess.invoke( |
| 79 | + VideoPreviewModel.error( |
| 80 | + originalUrl, |
| 81 | + "$ERROR_2 \n---> Response is null" |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + return@launch |
| 86 | + } |
| 87 | + |
| 88 | + val result = fromJson(jsonBody, videoInfoModel.type) |
| 89 | + .toPreview( |
| 90 | + url = originalUrl, |
| 91 | + linkToPlay = playLink, |
| 92 | + hostingName = videoInfoModel.hostingName, |
| 93 | + videoId = videoId |
| 94 | + ) |
| 95 | + |
| 96 | + onSuccess.invoke(result) |
| 97 | + |
| 98 | + try { |
| 99 | + if (context != null && isCacheEnabled) { |
| 100 | + insertModel(context, result) |
| 101 | + } |
| 102 | + } catch (e: Exception) { |
| 103 | + onError.invoke( |
| 104 | + originalUrl |
| 105 | + ?: "null url", "$ERROR_2\n---> ${e.localizedMessage}" |
| 106 | + ) |
| 107 | + } |
| 108 | + } catch (e: Exception) { |
| 109 | + onError.invoke( |
| 110 | + originalUrl ?: "null url", |
| 111 | + "$ERROR_2 !!! \n---> ${e.localizedMessage}" |
| 112 | + ) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Suppress("BlockingMethodInNonBlockingContext") |
| 118 | + private suspend fun makeCallGetBody(client: OkHttpClient, url: String) = |
| 119 | + withContext(Dispatchers.IO) { |
| 120 | + val response = client.newCall(Request.Builder().url(url).build()).execute() |
| 121 | + val stringBody = response.body?.string() ?: return@withContext null |
| 122 | + val jsonObject = parseString(stringBody) |
| 123 | + |
| 124 | + return@withContext if (jsonObject.isJsonArray) { |
| 125 | + jsonObject.asJsonArray[0] |
| 126 | + } else { |
| 127 | + jsonObject |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private fun fromJson(json: JsonElement?, type: Type?): BaseVideoResponse { |
| 132 | + return gson.fromJson(json, type) |
| 133 | + } |
| 134 | +} |
0 commit comments