Imported implementation for updating library by next expected update from Neko (#5436)

* Imported implementation for updating library by next expected update from Neko. This sort uses the last 4 updates for a manga to compute an average time between updates and then extrapolates when the next update should occur.

Currently seems to work perfectly. However, I may have silently messed something up along the way.

All code and algorithms are credited to kyjibo on GitHub. The original commit adding this functionality is here: 681003926a

* Imported implementation for updating library by next expected update from Neko. This sort uses the last 4 updates for a manga to compute an average time between updates and then extrapolates when the next update should occur.

Currently seems to work perfectly. However, I may have silently messed something up along the way.

All code and algorithms are credited to kyjibo on GitHub. The original commit adding this functionality is here: 681003926a

* Remove commented-out line from LibraryUpdateRanker

I missed removing this when first committing. The removed line is a holdover from Neko, which requires 7+, but I removed the function that requires this.
This commit is contained in:
stinky-lizard
2021-07-01 18:11:21 -04:00
committed by GitHub
parent 3c67a36b60
commit 70ed49e478
11 changed files with 115 additions and 10 deletions

View File

@@ -96,6 +96,24 @@ fun syncChaptersWithSource(
// Return if there's nothing to add, delete or change, avoiding unnecessary db transactions.
if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) {
val topChapters = dbChapters.sortedByDescending { it.date_upload }.take(4)
val newestDate = topChapters.getOrNull(0)?.date_upload ?: 0L
// Recalculate update rate if unset and enough chapters are present
if (manga.next_update == 0L && topChapters.size > 1) {
var delta = 0L
for (i in 0 until topChapters.size - 1) {
delta += (topChapters[i].date_upload - topChapters[i + 1].date_upload)
}
delta /= topChapters.size - 1
manga.next_update = newestDate + delta
db.updateNextUpdated(manga).executeAsBlocking()
}
if (newestDate != 0L && newestDate != manga.last_update) {
manga.last_update = newestDate
db.updateLastUpdated(manga).executeAsBlocking()
}
return Pair(emptyList(), emptyList())
}
@@ -140,11 +158,29 @@ fun syncChaptersWithSource(
db.insertChapters(toChange).executeAsBlocking()
}
val topChapters = db.getChapters(manga).executeAsBlocking().sortedByDescending { it.date_upload }.take(4)
// Recalculate next update since chapters were changed
if (topChapters.size > 1) {
var delta = 0L
for (i in 0 until topChapters.size - 1) {
delta += (topChapters[i].date_upload - topChapters[i + 1].date_upload)
}
delta /= topChapters.size - 1
manga.next_update = topChapters[0].date_upload + delta
db.updateNextUpdated(manga).executeAsBlocking()
}
// Fix order in source.
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
// Set this manga as updated since chapters were changed
manga.last_update = Date().time
val newestChapter = topChapters.getOrNull(0)
val dateFetch = newestChapter?.date_upload ?: manga.last_update
if (dateFetch == 0L) {
if (toAdd.isNotEmpty()) {
manga.last_update = Date().time
}
} else manga.last_update = dateFetch
db.updateLastUpdated(manga).executeAsBlocking()
}