Skip to content

Commit

Permalink
fix: ignore blank metadata fields
Browse files Browse the repository at this point in the history
should address gotson#288
  • Loading branch information
gotson committed Aug 27, 2020
1 parent d0815dd commit c8c5df2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,29 @@ class ComicInfoProvider(
}

val authors = mutableListOf<Author>()
comicInfo.writer?.let { authors += it.splitWithRole("writer") }
comicInfo.penciller?.let { authors += it.splitWithRole("penciller") }
comicInfo.inker?.let { authors += it.splitWithRole("inker") }
comicInfo.colorist?.let { authors += it.splitWithRole("colorist") }
comicInfo.letterer?.let { authors += it.splitWithRole("letterer") }
comicInfo.coverArtist?.let { authors += it.splitWithRole("cover") }
comicInfo.editor?.let { authors += it.splitWithRole("editor") }
comicInfo.writer?.splitWithRole("writer")?.let { authors += it }
comicInfo.penciller?.splitWithRole("penciller")?.let { authors += it }
comicInfo.inker?.splitWithRole("inker")?.let { authors += it }
comicInfo.colorist?.splitWithRole("colorist")?.let { authors += it }
comicInfo.letterer?.splitWithRole("letterer")?.let { authors += it }
comicInfo.coverArtist?.splitWithRole("cover")?.let { authors += it }
comicInfo.editor?.splitWithRole("editor")?.let { authors += it }

val readLists = mutableListOf<BookMetadataPatch.ReadListEntry>()
comicInfo.alternateSeries?.let { readLists.add(BookMetadataPatch.ReadListEntry(it, comicInfo.alternateNumber?.toIntOrNull())) }
if (!comicInfo.alternateSeries.isNullOrBlank()) {
readLists.add(BookMetadataPatch.ReadListEntry(comicInfo.alternateSeries!!, comicInfo.alternateNumber?.toIntOrNull()))
}

comicInfo.storyArc?.let { value ->
val arcs = value.split(",").map { it.trim() }
val arcs = value.split(",").mapNotNull { it.trim().ifBlank { null } }
readLists.addAll(arcs.map { BookMetadataPatch.ReadListEntry(it) })
}


return BookMetadataPatch(
title = comicInfo.title,
summary = comicInfo.summary,
number = comicInfo.number,
title = comicInfo.title?.ifBlank { null },
summary = comicInfo.summary?.ifBlank { null },
number = comicInfo.number?.ifBlank { null },
numberSort = comicInfo.number?.toFloatOrNull(),
releaseDate = releaseDate,
authors = authors.ifEmpty { null },
Expand All @@ -73,19 +75,19 @@ class ComicInfoProvider(
else -> null
}

val genres = comicInfo.genre?.split(',')?.map { it.trim() }?.toSet()
val genres = comicInfo.genre?.split(',')?.mapNotNull { it.trim().ifBlank { null } }

return SeriesMetadataPatch(
title = comicInfo.series,
titleSort = comicInfo.series,
title = comicInfo.series?.ifBlank { null },
titleSort = comicInfo.series?.ifBlank { null },
status = null,
summary = null,
readingDirection = readingDirection,
publisher = comicInfo.publisher,
publisher = comicInfo.publisher?.ifBlank { null },
ageRating = comicInfo.ageRating?.ageRating,
language = if(comicInfo.languageISO != null && BCP47TagValidator.isValid(comicInfo.languageISO!!)) comicInfo.languageISO else null,
genres = genres,
collections = listOfNotNull(comicInfo.seriesGroup)
language = if (comicInfo.languageISO != null && BCP47TagValidator.isValid(comicInfo.languageISO!!)) comicInfo.languageISO else null,
genres = if (!genres.isNullOrEmpty()) genres.toSet() else null,
collections = listOfNotNull(comicInfo.seriesGroup?.ifBlank { null })
)
}
return null
Expand All @@ -107,5 +109,7 @@ class ComicInfoProvider(
}

private fun String.splitWithRole(role: String) =
split(',').map { Author(it, role) }
split(',').mapNotNull { it.trim().ifBlank { null } }.let { list ->
if (list.isNotEmpty()) list.map { Author(it, role) } else null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ class EpubMetadataProvider(
epubExtractor.getPackageFile(book.path())?.let { packageFile ->
val opf = Jsoup.parse(packageFile)

val title = opf.selectFirst("metadata > dc|title")?.text()
val description = opf.selectFirst("metadata > dc|description")?.text()
val title = opf.selectFirst("metadata > dc|title")?.text()?.ifBlank { null }
val description = opf.selectFirst("metadata > dc|description")?.text()?.ifBlank { null }
val date = opf.selectFirst("metadata > dc|date")?.text()?.let { parseDate(it) }

val creatorRefines = opf.select("metadata > meta[property=role][scheme=marc:relators]")
.associate { it.attr("refines").removePrefix("#") to it.text() }
val authors = opf.select("metadata > dc|creator")
.map {
val name = it.text()
val opfRole = it.attr("opf|role").ifBlank { null }
val id = it.attr("id").ifBlank { null }
val refineRole = creatorRefines[id]
val role = opfRole ?: refineRole
Author(name, relators[role] ?: "writer")
.mapNotNull {
val name = it.text()?.ifBlank { null }
if (name == null) null
else {
val opfRole = it.attr("opf|role").ifBlank { null }
val id = it.attr("id").ifBlank { null }
val refineRole = creatorRefines[id]?.ifBlank { null }
val role = opfRole ?: refineRole
Author(name, relators[role] ?: "writer")
}
}

return BookMetadataPatch(
Expand All @@ -67,10 +70,10 @@ class EpubMetadataProvider(
epubExtractor.getPackageFile(book.path())?.let { packageFile ->
val opf = Jsoup.parse(packageFile)

val series = opf.selectFirst("metadata > meta[property=belongs-to-collection]")?.text()
val publisher = opf.selectFirst("metadata > dc|publisher")?.text()
val language = opf.selectFirst("metadata > dc|language")?.text()
val genre = opf.selectFirst("metadata > dc|subject")?.text()
val series = opf.selectFirst("metadata > meta[property=belongs-to-collection]")?.text()?.ifBlank { null }
val publisher = opf.selectFirst("metadata > dc|publisher")?.text()?.ifBlank { null }
val language = opf.selectFirst("metadata > dc|language")?.text()?.ifBlank { null }
val genre = opf.selectFirst("metadata > dc|subject")?.text()?.ifBlank { null }

val direction = opf.getElementsByTag("spine").first().attr("page-progression-direction")?.let {
when (it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ class ComicInfoProviderTest {
}
}

@Test
fun `given comicInfo with blank values when getting series metadata then blank values are omitted`() {
val comicInfo = ComicInfo().apply {
title = ""
summary = ""
number = ""
alternateSeries = ""
alternateNumber = ""
storyArc = ""
penciller = ""
}

every { mockMapper.readValue(any<ByteArray>(), ComicInfo::class.java) } returns comicInfo

val patch = comicInfoProvider.getBookMetadataFromBook(book, media)

with(patch!!) {
assertThat(title).isNull()
assertThat(summary).isNull()
assertThat(number).isNull()
assertThat(numberSort).isNull()
assertThat(authors).isNull()
assertThat(readLists).isEmpty()
}
}

@Test
fun `given comicInfo without year when getting book metadata then release date is null`() {
val comicInfo = ComicInfo().apply {
Expand Down Expand Up @@ -202,5 +228,30 @@ class ComicInfoProviderTest {
}
}

@Test
fun `given comicInfo with blank values when getting series metadata then blank values are omitted`() {
val comicInfo = ComicInfo().apply {
title = ""
storyArc = ""
genre = ""
languageISO = ""
publisher = ""
seriesGroup = ""
}

every { mockMapper.readValue(any<ByteArray>(), ComicInfo::class.java) } returns comicInfo

val patch = comicInfoProvider.getSeriesMetadataFromBook(book, media)!!

with(patch) {
assertThat(title).isNull()
assertThat(titleSort).isNull()
assertThat(genres).isNull()
assertThat(language).isNull()
assertThat(publisher).isNull()
assertThat(collections).isEmpty()
}
}

}
}

0 comments on commit c8c5df2

Please sign in to comment.