2020-08-21 02:50:37 +02:00
|
|
|
package exh.md.handlers
|
|
|
|
|
|
2021-02-11 22:41:23 +01:00
|
|
|
import eu.kanade.tachiyomi.data.database.models.Track
|
2020-08-21 02:50:37 +02:00
|
|
|
import eu.kanade.tachiyomi.source.model.SChapter
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.SManga
|
2021-09-12 19:07:18 +02:00
|
|
|
import exh.md.dto.ChapterDataDto
|
2021-07-06 00:31:30 +02:00
|
|
|
import exh.md.service.MangaDexService
|
|
|
|
|
import exh.md.utils.MdConstants
|
2020-08-21 02:50:37 +02:00
|
|
|
import exh.md.utils.MdUtil
|
2021-05-11 19:06:55 +02:00
|
|
|
import exh.md.utils.mdListCall
|
2021-02-11 22:41:23 +01:00
|
|
|
import exh.metadata.metadata.MangaDexSearchMetadata
|
2021-11-14 04:46:03 +01:00
|
|
|
import kotlinx.coroutines.CancellationException
|
2021-12-27 22:18:02 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2021-03-10 23:19:38 +01:00
|
|
|
import kotlinx.coroutines.async
|
2021-12-27 22:18:02 +01:00
|
|
|
import kotlinx.coroutines.coroutineScope
|
2020-08-21 02:50:37 +02:00
|
|
|
import rx.Observable
|
2023-01-28 04:31:12 +01:00
|
|
|
import tachiyomi.core.util.lang.runAsObservable
|
|
|
|
|
import tachiyomi.core.util.lang.withIOContext
|
2020-08-21 02:50:37 +02:00
|
|
|
|
2021-05-17 18:45:33 +02:00
|
|
|
class MangaHandler(
|
|
|
|
|
private val lang: String,
|
2021-07-06 00:31:30 +02:00
|
|
|
private val service: MangaDexService,
|
2021-05-17 18:45:33 +02:00
|
|
|
private val apiMangaParser: ApiMangaParser,
|
2022-04-08 21:30:30 +02:00
|
|
|
private val followsHandler: FollowsHandler,
|
2021-05-17 18:45:33 +02:00
|
|
|
) {
|
2022-08-18 20:07:13 +02:00
|
|
|
suspend fun getMangaDetails(manga: SManga, sourceId: Long): SManga {
|
2021-12-27 22:18:02 +01:00
|
|
|
return coroutineScope {
|
2022-08-18 20:07:13 +02:00
|
|
|
val mangaId = MdUtil.getMangaId(manga.url)
|
2021-12-27 22:18:02 +01:00
|
|
|
val response = async(Dispatchers.IO) { service.viewManga(mangaId) }
|
|
|
|
|
val simpleChapters = async(Dispatchers.IO) { getSimpleChapters(manga) }
|
|
|
|
|
val statistics = async(Dispatchers.IO) { service.mangasRating(mangaId).statistics[mangaId] }
|
|
|
|
|
apiMangaParser.parseToManga(
|
|
|
|
|
manga,
|
|
|
|
|
sourceId,
|
|
|
|
|
response.await(),
|
|
|
|
|
simpleChapters.await(),
|
2022-04-08 21:30:30 +02:00
|
|
|
statistics.await(),
|
2021-12-27 22:18:02 +01:00
|
|
|
)
|
|
|
|
|
}
|
2021-01-02 09:11:20 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-03 02:54:54 +01:00
|
|
|
fun fetchMangaDetailsObservable(manga: SManga, sourceId: Long): Observable<SManga> {
|
2021-12-03 02:44:05 +01:00
|
|
|
return runAsObservable {
|
2022-08-18 20:07:13 +02:00
|
|
|
getMangaDetails(manga, sourceId)
|
2021-12-03 02:44:05 +01:00
|
|
|
}
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-01 23:09:20 +01:00
|
|
|
fun fetchChapterListObservable(manga: SManga, blockedGroups: String, blockedUploaders: String): Observable<List<SChapter>> = runAsObservable {
|
2022-08-18 20:07:13 +02:00
|
|
|
getChapterList(manga, blockedGroups, blockedUploaders)
|
2021-12-03 02:44:05 +01:00
|
|
|
}
|
2020-08-21 02:50:37 +02:00
|
|
|
|
2022-08-18 20:07:13 +02:00
|
|
|
suspend fun getChapterList(manga: SManga, blockedGroups: String, blockedUploaders: String): List<SChapter> {
|
2021-01-11 01:36:24 +01:00
|
|
|
return withIOContext {
|
2021-07-06 00:31:30 +02:00
|
|
|
val results = mdListCall {
|
2022-02-01 23:09:20 +01:00
|
|
|
service.viewChapters(
|
2022-08-18 20:07:13 +02:00
|
|
|
MdUtil.getMangaId(manga.url),
|
2022-02-01 23:09:20 +01:00
|
|
|
lang,
|
|
|
|
|
it,
|
|
|
|
|
blockedGroups,
|
2022-04-08 21:30:30 +02:00
|
|
|
blockedUploaders,
|
2022-02-01 23:09:20 +01:00
|
|
|
)
|
2021-05-07 03:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val groupMap = getGroupMap(results)
|
|
|
|
|
|
2021-05-17 18:45:33 +02:00
|
|
|
apiMangaParser.chapterListParse(results, groupMap)
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 19:07:18 +02:00
|
|
|
private fun getGroupMap(results: List<ChapterDataDto>): Map<String, String> {
|
|
|
|
|
return results.map { chapter -> chapter.relationships }
|
2021-07-06 00:31:30 +02:00
|
|
|
.flatten()
|
|
|
|
|
.filter { it.type == MdConstants.Types.scanlator }
|
|
|
|
|
.map { it.id to it.attributes!!.name!! }
|
|
|
|
|
.toMap()
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-26 20:22:55 +01:00
|
|
|
suspend fun fetchRandomMangaId(): String {
|
2021-01-11 01:36:24 +01:00
|
|
|
return withIOContext {
|
2021-07-06 00:31:30 +02:00
|
|
|
service.randomManga().data.id
|
2020-09-12 05:12:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 05:34:56 +02:00
|
|
|
suspend fun getTrackingInfo(track: Track): Pair<Track, MangaDexSearchMetadata?> {
|
2021-03-10 23:19:38 +01:00
|
|
|
return withIOContext {
|
2021-06-02 20:09:19 +02:00
|
|
|
/*val metadata = async {
|
2021-05-17 18:45:33 +02:00
|
|
|
val mangaUrl = MdUtil.buildMangaUrl(MdUtil.getMangaId(track.tracking_url))
|
2021-03-10 23:19:38 +01:00
|
|
|
val manga = MangaInfo(mangaUrl, track.title)
|
2021-05-07 03:19:30 +02:00
|
|
|
val response = client.newCall(mangaRequest(manga)).await()
|
2021-03-10 23:19:38 +01:00
|
|
|
val metadata = MangaDexSearchMetadata()
|
2021-05-17 18:45:33 +02:00
|
|
|
apiMangaParser.parseIntoMetadata(metadata, response, emptyList())
|
2021-03-10 23:19:38 +01:00
|
|
|
metadata
|
2021-06-02 20:09:19 +02:00
|
|
|
}*/
|
2021-05-07 03:19:30 +02:00
|
|
|
val remoteTrack = async {
|
2021-05-17 18:45:33 +02:00
|
|
|
followsHandler.fetchTrackingInfo(track.tracking_url)
|
2021-05-07 03:19:30 +02:00
|
|
|
}
|
2021-05-07 19:41:33 +02:00
|
|
|
remoteTrack.await() to null
|
2021-03-10 23:19:38 +01:00
|
|
|
}
|
2021-02-11 22:41:23 +01:00
|
|
|
}
|
2021-07-06 22:57:26 +02:00
|
|
|
|
|
|
|
|
suspend fun getMangaFromChapterId(chapterId: String): String? {
|
|
|
|
|
return withIOContext {
|
|
|
|
|
apiMangaParser.chapterParseForMangaId(service.viewChapter(chapterId))
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-14 04:46:03 +01:00
|
|
|
|
2022-08-18 20:07:13 +02:00
|
|
|
private suspend fun getSimpleChapters(manga: SManga): List<String> {
|
|
|
|
|
return runCatching { service.aggregateChapters(MdUtil.getMangaId(manga.url), lang) }
|
2021-11-14 04:46:03 +01:00
|
|
|
.onFailure {
|
|
|
|
|
if (it is CancellationException) throw it
|
|
|
|
|
}
|
|
|
|
|
.map { dto ->
|
|
|
|
|
dto.volumes.values
|
|
|
|
|
.flatMap { it.chapters.values }
|
|
|
|
|
.map { it.chapter }
|
|
|
|
|
}
|
2021-12-11 20:58:23 +01:00
|
|
|
.getOrElse { emptyList() }
|
2021-11-14 04:46:03 +01:00
|
|
|
}
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|