|
| 1 | +package com.yenaly.han1meviewer.logic |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import com.yenaly.han1meviewer.EMPTY_STRING |
| 5 | +import com.yenaly.han1meviewer.logic.NetworkRepo.handleException |
| 6 | +import com.yenaly.han1meviewer.logic.NetworkRepo.throwRequestException |
| 7 | +import com.yenaly.han1meviewer.logic.network.HanimeNetwork |
| 8 | +import com.yenaly.han1meviewer.logic.state.WebsiteState |
| 9 | +import kotlinx.coroutines.Dispatchers |
| 10 | +import kotlinx.coroutines.flow.catch |
| 11 | +import kotlinx.coroutines.flow.flow |
| 12 | +import kotlinx.coroutines.flow.flowOn |
| 13 | +import okhttp3.ResponseBody |
| 14 | +import retrofit2.Response |
| 15 | +import java.nio.charset.Charset |
| 16 | +import kotlin.runCatching |
| 17 | + |
| 18 | +object GetchuNetworkRepo { |
| 19 | + val GETCHU_CHARSET: Charset = Charset.forName("EUC-JP") |
| 20 | + fun getGetchuPreview(date: String) = websiteIOFlow( |
| 21 | + request = { |
| 22 | + HanimeNetwork.getchuService.getPreviewList( |
| 23 | + year = date.take(4), |
| 24 | + month = date.takeLast(2), |
| 25 | + ) |
| 26 | + }, |
| 27 | + bodyToString = { it.getchuString() }, |
| 28 | + action = { GetchuParser.getchuPreview(it, date) } |
| 29 | + ) |
| 30 | + fun getGetchuPreviewDetail(id: String) = websiteIOFlow( |
| 31 | + request = { HanimeNetwork.getchuService.getPreviewDetail(id) }, |
| 32 | + bodyToString = { it.getchuString() }, |
| 33 | + ) { body -> |
| 34 | + val detailState = GetchuParser.getchuPreviewDetail(body, id) |
| 35 | + if (detailState !is WebsiteState.Success) return@websiteIOFlow detailState |
| 36 | + |
| 37 | + val parentId = body.extractGetchuSeriesParentId() ?: return@websiteIOFlow detailState |
| 38 | + runCatching { |
| 39 | + val response = HanimeNetwork.getchuService.getSeriesItems(parentIdArray = parentId) |
| 40 | + if (!response.isSuccessful) return@runCatching emptyList() |
| 41 | + response.body()?.getchuString()?.let(GetchuParser::getchuSeriesItems).orEmpty() |
| 42 | + }.getOrDefault(emptyList()).let { seriesItems -> |
| 43 | + Log.d( |
| 44 | + "GetchuPreviewParser", |
| 45 | + "series ajax id=$id parentId=$parentId items=${seriesItems.size}" |
| 46 | + ) |
| 47 | + if (seriesItems.isEmpty()) { |
| 48 | + detailState |
| 49 | + } else { |
| 50 | + val detail = detailState.info |
| 51 | + val mergedSeriesItems = (detail.seriesItems + seriesItems) |
| 52 | + .distinctBy { it.id } |
| 53 | + .filterNot { it.id == id } |
| 54 | + WebsiteState.Success( |
| 55 | + detail.copy( |
| 56 | + seriesItems = mergedSeriesItems, |
| 57 | + relatedItems = mergedSeriesItems, |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + private fun <T> websiteIOFlow( |
| 64 | + request: suspend () -> Response<ResponseBody>, |
| 65 | + permittedSuccessCode: IntArray? = null, |
| 66 | + bodyToString: (ResponseBody) -> String = ResponseBody::string, |
| 67 | + action: suspend (String) -> WebsiteState<T>, |
| 68 | + ) = flow { |
| 69 | + val requestResult = request.invoke() |
| 70 | + val resultBody = requestResult.body()?.let(bodyToString) |
| 71 | + val permitted = permittedSuccessCode?.contains(requestResult.code()) == true |
| 72 | + if ((permitted || requestResult.isSuccessful)) { |
| 73 | + emit(action.invoke(resultBody ?: EMPTY_STRING)) |
| 74 | + } else { |
| 75 | + requestResult.throwRequestException() |
| 76 | + } |
| 77 | + }.catch { e -> |
| 78 | + emit(WebsiteState.Error(handleException(e))) |
| 79 | + }.flowOn(Dispatchers.IO) |
| 80 | + |
| 81 | + private fun ResponseBody.getchuString(): String { |
| 82 | + return bytes().toString(GETCHU_CHARSET) |
| 83 | + } |
| 84 | + |
| 85 | + private fun String.extractGetchuSeriesParentId(): String? { |
| 86 | + return Regex("[\"']parent_id_array[\"']\\s*:\\s*[\"']([^\"']+)[\"']", RegexOption.IGNORE_CASE) |
| 87 | + .find(this) |
| 88 | + ?.groupValues |
| 89 | + ?.getOrNull(1) |
| 90 | + ?.takeIf { it.isNotBlank() } |
| 91 | + } |
| 92 | +} |
0 commit comments