in src/main/scala/ophan/google/index/checker/GoogleSearchService.scala [48:81]
def contentAvailabilityInGoogleIndex(content: ContentSummary): Future[CheckReport] = Future {
blocking {
def performSearch(searchTerm: String): Boolean = {
val requestBody = ujson.Obj(
"query" -> searchTerm,
"pageSize" -> 10
)
val request = HttpRequest.newBuilder()
.uri(URI.create(s"$baseUrl?key=$apiKey"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
.build()
val response = httpClient.send(request, java.net.http.HttpResponse.BodyHandlers.ofString())
val searchResponse = read[SearchResponse](response.body())
searchResponse.results.exists { result =>
resultMatches(content.webUrl, result)
}
}
CheckReport(Instant.now, accessGoogleIndex = Try {
val initialResult = performSearch(content.reliableSearchTerm)
if (!initialResult) {
println(s"executing fallback for ${content.id}")
performSearch(content.webTitle)
} else {
initialResult
}
})
}
}