Skip to content

Commit

Permalink
Feature/improve automatic chapter downloads (#680)
Browse files Browse the repository at this point in the history
* Rename "newChapters" to "updatedChapterList"

* Do not auto download new chapters of entries with unread chapters

Makes it possible to prevent unnecessary chapter downloads in case the entry hasn't yet been caught up

* Optionally limit auto new chapter downloads

* Prevent downloading new chapters for mangas not in the library
  • Loading branch information
schroda authored Sep 16, 2023
1 parent c910026 commit 553b35d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
38 changes: 29 additions & 9 deletions server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,48 @@ object Chapter {
}
}

downloadNewChapters(mangaId, numberOfCurrentChapters, newChapters)
if (manga.inLibrary) {
downloadNewChapters(mangaId, numberOfCurrentChapters, newChapters)
}

return chapterList
}

private fun downloadNewChapters(mangaId: Int, prevNumberOfChapters: Int, newChapters: List<ResultRow>) {
private fun downloadNewChapters(mangaId: Int, prevNumberOfChapters: Int, updatedChapterList: List<ResultRow>) {
// convert numbers to be index based
val currentNumberOfChapters = (prevNumberOfChapters - 1).coerceAtLeast(0)
val updatedNumberOfChapters = (newChapters.size - 1).coerceAtLeast(0)
val updatedNumberOfChapters = (updatedChapterList.size - 1).coerceAtLeast(0)
val numberOfNewChapters = updatedNumberOfChapters - currentNumberOfChapters

val areNewChaptersAvailable = currentNumberOfChapters < updatedNumberOfChapters
val areNewChaptersAvailable = numberOfNewChapters > 0
val wasInitialFetch = currentNumberOfChapters == 0

// make sure to ignore initial fetch
val downloadNewChapters = serverConfig.autoDownloadNewChapters.value && !wasInitialFetch && areNewChaptersAvailable
if (!downloadNewChapters) {
val isDownloadPossible =
serverConfig.autoDownloadNewChapters.value && areNewChaptersAvailable && !wasInitialFetch
if (!isDownloadPossible) {
return
}

val numberOfNewChapters = updatedNumberOfChapters - currentNumberOfChapters
val chapterIdsToDownload = newChapters.subList(0, numberOfNewChapters)
.filter { !it[ChapterTable.isRead] && !it[ChapterTable.isDownloaded] }.map { it[ChapterTable.id].value }
val newChapters = updatedChapterList.subList(0, numberOfNewChapters)

// make sure to only consider the latest chapters. e.g. old unread chapters should be ignored
val latestReadChapterIndex =
updatedChapterList.indexOfFirst { it[ChapterTable.isRead] }.takeIf { it > -1 } ?: return
val unreadChapters = updatedChapterList.subList(numberOfNewChapters, latestReadChapterIndex)
.filter { !it[ChapterTable.isRead] }

val skipDueToUnreadChapters = serverConfig.excludeEntryWithUnreadChapters.value && unreadChapters.isNotEmpty()
if (skipDueToUnreadChapters) {
return
}

val firstChapterToDownloadIndex =
(numberOfNewChapters - serverConfig.autoDownloadAheadLimit.value).coerceAtLeast(0)

val chapterIdsToDownload = newChapters.subList(firstChapterToDownloadIndex, numberOfNewChapters)
.filter { !it[ChapterTable.isRead] && !it[ChapterTable.isDownloaded] }
.map { it[ChapterTable.id].value }

if (chapterIdsToDownload.isEmpty()) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ServerConfig(getConfig: () -> Config, val moduleName: String = SERVER_CONF
val downloadAsCbz: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
val downloadsPath: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter)
val autoDownloadNewChapters: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
val excludeEntryWithUnreadChapters: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
val autoDownloadAheadLimit: MutableStateFlow<Int> by OverrideConfigValue(IntConfigAdapter)

// requests
val maxSourcesInParallel: MutableStateFlow<Int> by OverrideConfigValue(IntConfigAdapter)
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/resources/server-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ server.webUIUpdateCheckInterval = 23 # time in hours - 0 to disable auto update
server.downloadAsCbz = false
server.downloadsPath = ""
server.autoDownloadNewChapters = false # if new chapters that have been retrieved should get automatically downloaded
server.excludeEntryWithUnreadChapters = true # ignore automatic chapter downloads of entries with unread chapters
server.autoDownloadAheadLimit = 5 # 0 to disable it - how many unread downloaded chapters should be available - if the limit is reached, new chapters won't be downloaded automatically

# requests
server.maxSourcesInParallel = 6 # range: 1 <= n <= 20 - default: 6 - sets how many sources can do requests (updates, downloads) in parallel. updates/downloads are grouped by source and all mangas of a source are updated/downloaded synchronously
Expand Down
2 changes: 2 additions & 0 deletions server/src/test/resources/server-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ server.socksProxyPort = ""
# downloader
server.downloadAsCbz = false
server.autoDownloadNewChapters = false
server.excludeEntryWithUnreadChapters = true
server.autoDownloadAheadLimit = 5

# requests
server.maxSourcesInParallel = 10
Expand Down

0 comments on commit 553b35d

Please sign in to comment.