feat: handle multiple story arcs in ComicInfoProvider

StoryArc tag will be split on commas (',')

closes #282
This commit is contained in:
Gauthier Roebroeck 2020-08-27 14:29:49 +08:00
parent 8a079cec80
commit f4451bfd41
6 changed files with 50 additions and 41 deletions

View file

@ -10,6 +10,10 @@ data class BookMetadataPatch(
val releaseDate: LocalDate?,
val authors: List<Author>?,
val readList: String?,
val readListNumber: Int?
)
val readLists: List<ReadListEntry> = emptyList()
) {
data class ReadListEntry(
val name: String,
val number: Int? = null
)
}

View file

@ -71,37 +71,32 @@ class MetadataLifecycle(
// handle read lists
if (provider is ComicInfoProvider && library.importComicInfoReadList) {
patch?.let { bPatch ->
val readList = bPatch.readList
val readListNumber = bPatch.readListNumber
patch?.readLists?.forEach { readList ->
if (readList != null) {
readListRepository.findByNameOrNull(readList).let { existing ->
if (existing != null) {
if (existing.bookIds.containsValue(book.id))
logger.debug { "Book is already in existing readlist '${existing.name}'" }
else {
val map = existing.bookIds.toSortedMap()
val key = if (readListNumber != null && existing.bookIds.containsKey(readListNumber)) {
logger.debug { "Existing readlist '${existing.name}' already contains a book at position $readListNumber, adding book '${book.name}' at the end" }
existing.bookIds.lastKey() + 1
} else {
logger.debug { "Adding book '${book.name}' to existing readlist '${existing.name}'" }
readListNumber ?: existing.bookIds.lastKey() + 1
}
map[key] = book.id
readListLifecycle.updateReadList(
existing.copy(bookIds = map)
)
readListRepository.findByNameOrNull(readList.name).let { existing ->
if (existing != null) {
if (existing.bookIds.containsValue(book.id))
logger.debug { "Book is already in existing readlist '${existing.name}'" }
else {
val map = existing.bookIds.toSortedMap()
val key = if (readList.number != null && existing.bookIds.containsKey(readList.number)) {
logger.debug { "Existing readlist '${existing.name}' already contains a book at position ${readList.number}, adding book '${book.name}' at the end" }
existing.bookIds.lastKey() + 1
} else {
logger.debug { "Adding book '${book.name}' to existing readlist '${existing.name}'" }
readList.number ?: existing.bookIds.lastKey() + 1
}
} else {
logger.debug { "Adding book '${book.name}' to new readlist '$readList'" }
readListLifecycle.addReadList(ReadList(
name = readList,
bookIds = mapOf((readListNumber ?: 0) to book.id).toSortedMap()
))
map[key] = book.id
readListLifecycle.updateReadList(
existing.copy(bookIds = map)
)
}
} else {
logger.debug { "Adding book '${book.name}' to new readlist '$readList'" }
readListLifecycle.addReadList(ReadList(
name = readList.name,
bookIds = mapOf((readList.number ?: 0) to book.id).toSortedMap()
))
}
}
}

View file

@ -43,6 +43,15 @@ class ComicInfoProvider(
comicInfo.coverArtist?.let { authors += it.splitWithRole("cover") }
comicInfo.editor?.let { authors += it.splitWithRole("editor") }
val readLists = mutableListOf<BookMetadataPatch.ReadListEntry>()
comicInfo.alternateSeries?.let { readLists.add(BookMetadataPatch.ReadListEntry(it, comicInfo.alternateNumber?.toIntOrNull())) }
comicInfo.storyArc?.let { value ->
val arcs = value.split(",").map { it.trim() }
readLists.addAll(arcs.map { BookMetadataPatch.ReadListEntry(it) })
}
return BookMetadataPatch(
title = comicInfo.title,
summary = comicInfo.summary,
@ -50,8 +59,7 @@ class ComicInfoProvider(
numberSort = comicInfo.number?.toFloatOrNull(),
releaseDate = releaseDate,
authors = authors.ifEmpty { null },
readList = comicInfo.alternateSeries ?: comicInfo.storyArc,
readListNumber = comicInfo.alternateNumber?.toIntOrNull() ?: comicInfo.storyArcNumber?.toIntOrNull()
readLists = readLists
)
}
return null

View file

@ -111,9 +111,6 @@ class ComicInfo {
@JsonProperty(value = "StoryArc")
var storyArc: String? = null
@JsonProperty(value = "StoryArcNumber")
var storyArcNumber: String? = null
@JsonProperty(value = "SeriesGroup")
var seriesGroup: String? = null

View file

@ -56,9 +56,7 @@ class EpubMetadataProvider(
number = null,
numberSort = null,
releaseDate = date,
authors = authors,
readList = null,
readListNumber = null
authors = authors
)
}
return null

View file

@ -44,6 +44,7 @@ class ComicInfoProviderTest {
month = 2
alternateSeries = "story arc"
alternateNumber = "5"
storyArc = "one, two, three"
}
every { mockMapper.readValue(any<ByteArray>(), ComicInfo::class.java) } returns comicInfo
@ -55,9 +56,15 @@ class ComicInfoProviderTest {
assertThat(summary).isEqualTo("summary")
assertThat(number).isEqualTo("010")
assertThat(numberSort).isEqualTo(10F)
assertThat(readList).isEqualTo("story arc")
assertThat(readListNumber).isEqualTo(5)
assertThat(releaseDate).isEqualTo(LocalDate.of(2020, 2, 1))
with(readLists) {
assertThat(this).hasSize(4)
assertThat(this.map { it.name }).containsExactly("story arc", "one", "two", "three")
this.first { it.number != null }.let {
assertThat(it.name).isEqualTo("story arc")
assertThat(it.number).isEqualTo(5)
}
}
}
}