Skip to content

Commit

Permalink
fix: google api provider #161
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Jan 17, 2025
1 parent 9061b88 commit ba3cb81
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private val logger = KotlinLogging.logger {}
class GoogleBooksIMetaDataProvider(
@Resource(name = "restClient") private val restClient: WebClient,
private val properties: JeluProperties,
private val objectMapper: ObjectMapper,
) : IMetaDataProvider {

private val _name = "google"
Expand All @@ -29,7 +30,8 @@ class GoogleBooksIMetaDataProvider(
config: Map<String, String>,
): Optional<MetadataDto> {
val googleProviderApiKey = getGoogleProviderApiKey()
if (googleProviderApiKey.isNullOrBlank() || metadataRequestDto.isbn.isNullOrBlank()) {
if (googleProviderApiKey.isNullOrBlank()) {
logger.warn { "missing google books API key" }
return Optional.empty()
}
val res = restClient.get()
Expand All @@ -38,18 +40,22 @@ class GoogleBooksIMetaDataProvider(
.scheme("https")
.host("www.googleapis.com")
.path("/books/v1/volumes")
.queryParam("q", "isbn:${metadataRequestDto.isbn}")
.queryParam("q", query(metadataRequestDto))
.queryParam("key", googleProviderApiKey)
.build()
}.exchangeToMono {
if (it.statusCode() == HttpStatus.OK) {
it.bodyToMono(String::class.java).map { bodyString ->
parseBook(
ObjectMapper()
.readTree(bodyString)
.get("items")
.get(0),
)
val r = objectMapper.readTree(bodyString).get("items")
if (r == null) {
Optional.empty()
} else {
Optional.of(
parseBook(
r.get(0),
),
)
}
}
} else {
logger.error { "error fetching metadata from google : ${it.statusCode()}" }
Expand All @@ -60,7 +66,22 @@ class GoogleBooksIMetaDataProvider(
if (res == null) {
return Optional.empty()
}
return Optional.of(res)
return res
}

private fun query(metadataRequestDto: MetadataRequestDto): String {
if (!metadataRequestDto.isbn.isNullOrBlank()) {
return "isbn:${metadataRequestDto.isbn}"
} else if (!metadataRequestDto.title.isNullOrBlank()) {
var query = "intitle:${metadataRequestDto.title}"
if (!metadataRequestDto.authors.isNullOrBlank()) {
query += "+inauthor:${metadataRequestDto.authors}"
}
return query
} else if (!metadataRequestDto.authors.isNullOrBlank()) {
return "inauthor:${metadataRequestDto.authors}"
}
return ""
}

override fun name(): String {
Expand All @@ -81,10 +102,24 @@ class GoogleBooksIMetaDataProvider(
isbn10 = identifiers.find { it.get("type").asText() == "ISBN_10" }?.get("identifier")?.asText(),
isbn13 = identifiers.find { it.get("type").asText() == "ISBN_13" }?.get("identifier")?.asText(),
authors = volumeInfo.get("authors").asIterable().map { it.asText() }.toMutableSet(),
image = volumeInfo.get("imageLinks").get("thumbnail").asText(),
image = extractImage(volumeInfo),
language = volumeInfo.get("language").asText(),
publishedDate = volumeInfo.get("publishedDate").asText(),
summary = node.get("searchInfo").get("textSnippet").asText(),
summary = summary(node),
)
}

private fun extractImage(node: JsonNode): String? {
if (node.get("imageLinks") != null && node.get("imageLinks").get("thumbnail") != null) {
return node.get("imageLinks").get("thumbnail").asText()
}
return null
}

private fun summary(node: JsonNode): String? {
if (node.get("searchInfo") != null && node.get("searchInfo").get("textSnippet") != null) {
return node.get("searchInfo").get("textSnippet").asText()
}
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.bayang.jelu.service.metadata.providers

import com.fasterxml.jackson.databind.ObjectMapper
import io.github.bayang.jelu.config.JeluProperties
import io.github.bayang.jelu.dto.MetadataDto
import io.github.bayang.jelu.dto.MetadataRequestDto
Expand Down Expand Up @@ -110,7 +111,7 @@ class GoogleBooksIMetaDataProviderTest {
JeluProperties.Auth(JeluProperties.Ldap(), JeluProperties.Proxy()),
listOf(JeluProperties.MetaDataProvider("google", true, "fake-google-api-key")),
)
val service = GoogleBooksIMetaDataProvider(webClient, jeluProperties)
val service = GoogleBooksIMetaDataProvider(webClient, jeluProperties, ObjectMapper())

// When
val result: MetadataDto = service.fetchMetadata(
Expand Down

0 comments on commit ba3cb81

Please sign in to comment.