2020-08-21 02:50:37 +02:00
|
|
|
package exh.md.handlers
|
|
|
|
|
|
|
|
|
|
import eu.kanade.tachiyomi.data.database.models.Track
|
|
|
|
|
import eu.kanade.tachiyomi.data.track.TrackManager
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.MetadataMangasPage
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.SManga
|
2021-05-07 03:19:30 +02:00
|
|
|
import eu.kanade.tachiyomi.source.model.toSManga
|
2021-01-11 01:36:24 +01:00
|
|
|
import eu.kanade.tachiyomi.util.lang.withIOContext
|
2021-09-12 19:07:18 +02:00
|
|
|
import exh.md.dto.MangaDataDto
|
2021-07-06 00:31:30 +02:00
|
|
|
import exh.md.dto.ReadingStatusDto
|
|
|
|
|
import exh.md.service.MangaDexAuthService
|
2020-08-21 02:50:37 +02:00
|
|
|
import exh.md.utils.FollowStatus
|
|
|
|
|
import exh.md.utils.MdUtil
|
2021-05-11 19:06:55 +02:00
|
|
|
import exh.md.utils.mdListCall
|
2020-08-21 02:50:37 +02:00
|
|
|
import exh.metadata.metadata.MangaDexSearchMetadata
|
2021-05-07 03:19:30 +02:00
|
|
|
import exh.util.under
|
2021-07-06 00:31:30 +02:00
|
|
|
import kotlinx.coroutines.async
|
2021-05-07 03:19:30 +02:00
|
|
|
|
|
|
|
|
class FollowsHandler(
|
|
|
|
|
private val lang: String,
|
2021-07-06 00:31:30 +02:00
|
|
|
private val service: MangaDexAuthService
|
2021-05-07 03:19:30 +02:00
|
|
|
) {
|
2020-08-21 02:50:37 +02:00
|
|
|
|
|
|
|
|
/**
|
2021-05-09 23:37:19 +02:00
|
|
|
* fetch follows page
|
2020-08-21 02:50:37 +02:00
|
|
|
*/
|
2021-05-09 23:37:19 +02:00
|
|
|
suspend fun fetchFollows(page: Int): MetadataMangasPage {
|
2021-05-07 03:19:30 +02:00
|
|
|
return withIOContext {
|
2021-07-06 00:31:30 +02:00
|
|
|
val follows = service.userFollowList(MdUtil.mangaLimit * page)
|
2021-05-07 03:19:30 +02:00
|
|
|
|
2021-09-12 19:07:18 +02:00
|
|
|
if (follows.data.isEmpty()) {
|
2021-05-09 23:37:19 +02:00
|
|
|
return@withIOContext MetadataMangasPage(emptyList(), false, emptyList())
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
2021-05-09 23:37:19 +02:00
|
|
|
|
2021-07-06 00:31:30 +02:00
|
|
|
val hasMoreResults = follows.limit + follows.offset under follows.total
|
|
|
|
|
val statusListResponse = service.readingStatusAllManga()
|
2021-09-12 19:07:18 +02:00
|
|
|
val results = followsParseMangaPage(follows.data, statusListResponse.statuses)
|
2021-05-09 23:37:19 +02:00
|
|
|
|
|
|
|
|
MetadataMangasPage(results.map { it.first }, hasMoreResults, results.map { it.second })
|
2021-05-07 03:19:30 +02:00
|
|
|
}
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse follows api to manga page
|
|
|
|
|
* used when multiple follows
|
|
|
|
|
*/
|
2021-07-06 00:31:30 +02:00
|
|
|
private fun followsParseMangaPage(
|
2021-09-12 19:07:18 +02:00
|
|
|
response: List<MangaDataDto>,
|
2021-07-06 00:31:30 +02:00
|
|
|
statuses: Map<String, String?>
|
|
|
|
|
): List<Pair<SManga, MangaDexSearchMetadata>> {
|
2021-05-09 23:37:19 +02:00
|
|
|
val comparator = compareBy<Pair<SManga, MangaDexSearchMetadata>> { it.second.followStatus }
|
2021-05-07 03:19:30 +02:00
|
|
|
.thenBy { it.first.title }
|
2021-05-09 23:37:19 +02:00
|
|
|
|
2021-06-05 16:38:21 +02:00
|
|
|
return response.map {
|
2021-05-09 23:37:19 +02:00
|
|
|
MdUtil.createMangaEntry(
|
|
|
|
|
it,
|
2021-07-06 00:31:30 +02:00
|
|
|
lang
|
2021-05-09 23:37:19 +02:00
|
|
|
).toSManga() to MangaDexSearchMetadata().apply {
|
2021-09-12 19:07:18 +02:00
|
|
|
followStatus = FollowStatus.fromDex(statuses[it.id]).int
|
2021-05-07 03:19:30 +02:00
|
|
|
}
|
|
|
|
|
}.sortedWith(comparator)
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change the status of a manga
|
|
|
|
|
*/
|
2021-05-07 03:19:30 +02:00
|
|
|
suspend fun updateFollowStatus(mangaId: String, followStatus: FollowStatus): Boolean {
|
2021-01-11 01:36:24 +01:00
|
|
|
return withIOContext {
|
2021-05-07 03:19:30 +02:00
|
|
|
val status = when (followStatus == FollowStatus.UNFOLLOWED) {
|
|
|
|
|
true -> null
|
2021-07-06 00:31:30 +02:00
|
|
|
false -> followStatus.toDex()
|
2021-05-07 03:19:30 +02:00
|
|
|
}
|
2021-07-06 00:31:30 +02:00
|
|
|
val readingStatusDto = ReadingStatusDto(status)
|
2021-05-07 03:19:30 +02:00
|
|
|
|
2021-07-06 00:31:30 +02:00
|
|
|
if (followStatus == FollowStatus.UNFOLLOWED) {
|
|
|
|
|
service.unfollowManga(mangaId)
|
|
|
|
|
} else {
|
|
|
|
|
service.followManga(mangaId)
|
|
|
|
|
}
|
2021-05-17 18:45:33 +02:00
|
|
|
|
2021-07-06 00:31:30 +02:00
|
|
|
service.updateReadingStatusForManga(mangaId, readingStatusDto).result == "ok"
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 22:28:38 +02:00
|
|
|
/*suspend fun updateReadingProgress(track: Track): Boolean {
|
2021-05-07 03:19:30 +02:00
|
|
|
return true
|
2021-06-03 22:28:38 +02:00
|
|
|
return withIOContext {
|
2021-05-07 03:19:30 +02:00
|
|
|
val mangaID = getMangaId(track.tracking_url)
|
2020-08-21 02:50:37 +02:00
|
|
|
val formBody = FormBody.Builder()
|
2021-01-16 04:38:16 +01:00
|
|
|
.add("volume", "0")
|
2020-08-21 02:50:37 +02:00
|
|
|
.add("chapter", track.last_chapter_read.toString())
|
2021-05-07 03:19:30 +02:00
|
|
|
XLog.d("chapter to update %s", track.last_chapter_read.toString())
|
|
|
|
|
val result = runCatching {
|
|
|
|
|
client.newCall(
|
|
|
|
|
POST(
|
|
|
|
|
"$baseUrl/ajax/actions.ajax.php?function=edit_progress&id=$mangaID",
|
|
|
|
|
headers,
|
|
|
|
|
formBody.build()
|
|
|
|
|
)
|
|
|
|
|
).execute()
|
|
|
|
|
}
|
|
|
|
|
result.exceptionOrNull()?.let {
|
|
|
|
|
if (it is EOFException) {
|
|
|
|
|
return@withIOContext true
|
|
|
|
|
} else {
|
|
|
|
|
XLog.e("error updating reading progress", it)
|
|
|
|
|
return@withIOContext false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.isSuccess
|
2021-06-03 22:28:38 +02:00
|
|
|
}
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun updateRating(track: Track): Boolean {
|
2021-05-07 03:19:30 +02:00
|
|
|
return true
|
2021-06-03 22:28:38 +02:00
|
|
|
return withIOContext {
|
2021-05-07 03:19:30 +02:00
|
|
|
val mangaID = getMangaId(track.tracking_url)
|
|
|
|
|
val result = runCatching {
|
|
|
|
|
client.newCall(
|
|
|
|
|
GET(
|
|
|
|
|
"$baseUrl/ajax/actions.ajax.php?function=manga_rating&id=$mangaID&rating=${track.score.toInt()}",
|
|
|
|
|
headers
|
|
|
|
|
)
|
2020-08-21 02:50:37 +02:00
|
|
|
)
|
2021-05-07 03:19:30 +02:00
|
|
|
.execute()
|
|
|
|
|
}
|
2021-03-10 23:19:38 +01:00
|
|
|
|
2021-05-07 03:19:30 +02:00
|
|
|
result.exceptionOrNull()?.let {
|
|
|
|
|
if (it is EOFException) {
|
|
|
|
|
return@withIOContext true
|
|
|
|
|
} else {
|
|
|
|
|
XLog.e("error updating rating", it)
|
|
|
|
|
return@withIOContext false
|
2021-03-10 23:19:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-07 03:19:30 +02:00
|
|
|
result.isSuccess
|
2021-06-03 22:28:38 +02:00
|
|
|
}
|
|
|
|
|
}*/
|
2020-08-21 02:50:37 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* fetch all manga from all possible pages
|
|
|
|
|
*/
|
2021-05-07 03:19:30 +02:00
|
|
|
suspend fun fetchAllFollows(): List<Pair<SManga, MangaDexSearchMetadata>> {
|
2021-01-11 01:36:24 +01:00
|
|
|
return withIOContext {
|
2021-07-06 00:31:30 +02:00
|
|
|
val results = async {
|
|
|
|
|
mdListCall {
|
|
|
|
|
service.userFollowList(it)
|
|
|
|
|
}
|
2021-05-10 04:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-06 00:31:30 +02:00
|
|
|
val readingStatusResponse = async { service.readingStatusAllManga().statuses }
|
2021-05-11 04:01:42 +02:00
|
|
|
|
2021-07-06 00:31:30 +02:00
|
|
|
followsParseMangaPage(results.await(), readingStatusResponse.await())
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun fetchTrackingInfo(url: String): Track {
|
2021-01-11 01:36:24 +01:00
|
|
|
return withIOContext {
|
2021-05-09 23:37:19 +02:00
|
|
|
val mangaId = MdUtil.getMangaId(url)
|
2021-07-06 00:31:30 +02:00
|
|
|
val followStatus = FollowStatus.fromDex(service.readingStatusForManga(mangaId).status)
|
|
|
|
|
Track.create(TrackManager.MDLIST).apply {
|
2021-07-18 19:06:12 +02:00
|
|
|
title = ""
|
2021-07-06 00:31:30 +02:00
|
|
|
status = followStatus.int
|
2021-07-18 19:06:12 +02:00
|
|
|
tracking_url = url
|
2021-07-06 00:31:30 +02:00
|
|
|
}
|
2021-05-12 22:17:39 +02:00
|
|
|
}
|
2021-05-07 03:19:30 +02:00
|
|
|
}
|
2020-08-21 02:50:37 +02:00
|
|
|
}
|