Skip to content

Commit

Permalink
feat: detect if isbn field contains ASIN for metadata search #113
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Apr 25, 2024
2 parents 4544cde + aa7f6b7 commit 41d80f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ class CalibreMetadataProvider(
if (!metadataRequestDto.isbn.isNullOrBlank()) {
bookFileName += metadataRequestDto.isbn
fileNameComplete = true
commandArray.add("-i")
commandArray.add(metadataRequestDto.isbn)
if (determineCodeType(metadataRequestDto.isbn) == "ASIN") {
commandArray.add("-I")
commandArray.add("asin:" + metadataRequestDto.isbn)
} else {
commandArray.add("-i")
commandArray.add(metadataRequestDto.isbn)
}
}
if (!metadataRequestDto.title.isNullOrBlank()) {
commandArray.add("-t")
Expand Down Expand Up @@ -169,4 +174,23 @@ class CalibreMetadataProvider(
}
return trimmed
}

internal fun determineCodeType(code: String): String {
// Regex for ISBN-10: Either 10 digits, or 9 digits followed by 'X'
val isbn10Regex = Regex("^(?=[0-9X]{10}\$)([0-9]{9}[X]|[0-9]{10})\$")

// Regex for ISBN-13: Starts with 978 or 979 followed by 10 digits
val isbn13Regex = Regex("^(978|979)[0-9]{10}\$")

// Regex for ASIN: 10 alphanumeric characters
val asinRegex = Regex("^[A-Z0-9]{10}\$")

return when {
// Strip all hyphens and spaces from the code before matching
isbn10Regex.matches(code.replace("-", "").replace(" ", "")) -> "ISBN-10"
isbn13Regex.matches(code.replace("-", "").replace(" ", "")) -> "ISBN-13"
asinRegex.matches(code) -> "ASIN"
else -> "Unknown format"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,31 @@ class CalibreMetadataProviderTest(
Assertions.assertEquals(1, metadata.authors.size)
Assertions.assertEquals("J. R. R. Tolkien", metadata.authors.first())
}

@Test
fun testASINMatch() {
// https://www.amazon.com/Fellowship-Ring-Being-First-Rings-ebook/dp/B007978NPG -- Kindle Edition
var input = "B007978NPG"
val resultingCodeType = calibreMetadataProvider.determineCodeType(input)
Assertions.assertEquals("ASIN", resultingCodeType)
}

@Test
fun testISBN10Match() {
// https://www.amazon.com/Fellowship-Ring-Being-First-Rings/dp/0547928211 -- Paperback ISBN 10
var input = "0547928211"
val resultingCodeType = calibreMetadataProvider.determineCodeType(input)
Assertions.assertEquals("ISBN-10", resultingCodeType)
}

@Test
fun testISB13match() {
// // https://www.amazon.com/Fellowship-Ring-Being-First-Rings/dp/0547928211 -- Paperback ISBN 13
var plainInput = "9780547928210"
val plainResultingCodeType = calibreMetadataProvider.determineCodeType(plainInput)
var dashedInput = "978-0547928210"
val dashedResultingCodeType = calibreMetadataProvider.determineCodeType(dashedInput)
Assertions.assertEquals("ISBN-13", plainResultingCodeType)
Assertions.assertEquals("ISBN-13", dashedResultingCodeType)
}
}

0 comments on commit 41d80f1

Please sign in to comment.