Skip to content

Commit

Permalink
✨ Add thumbnail fetching functionality to SongFinder
Browse files Browse the repository at this point in the history
This commit introduces the capability of fetching thumbnails for each song in the SongFinder application. This feature is realized through the addition of a new "FindThumbnailService" and supportive configurations, including a dedicated HTTP client "thumbnail-fetcher". This new functionality allows more detailed results to be presented to the users.
  • Loading branch information
CXwudi committed Dec 6, 2023
1 parent 2b72696 commit b4e99eb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mikufan.cx.songfinder.backend.config

import com.fasterxml.jackson.databind.ObjectMapper
import io.ktor.client.*
import io.ktor.client.engine.java.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.http.*
import io.ktor.serialization.jackson.*
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class KtorClientConfig {

@Bean("thumbnail-fetcher")
fun thumbnailFetcher(mapper: ObjectMapper) = HttpClient(Java) {
install(ContentNegotiation) {
// use spring jackson mapper
register(ContentType.Application.Json, JacksonConverter(mapper))
}
install(UserAgent) {
agent = "Song Finder by VocaDB @ https://github.com/CXwudi/song-finder-vocadb"
}
// BrowserUserAgent()

followRedirects = true

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mikufan.cx.songfinder.backend.controller.mainpage

import mikufan.cx.inlinelogging.KInlineLogging
import mikufan.cx.songfinder.backend.model.PVInfo
import mikufan.cx.songfinder.backend.model.SongSearchResult
import mikufan.cx.songfinder.backend.model.ThumbnailInfo
import mikufan.cx.songfinder.backend.service.FindThumbnailService
import mikufan.cx.songfinder.backend.service.InputFileLineReader
import mikufan.cx.songfinder.backend.service.OutputCsvLineWriter
import mikufan.cx.songfinder.backend.statemodel.*
Expand All @@ -16,6 +19,7 @@ class ResultCellController(
private val searchOptionsStateModel: SearchOptionsStateModel,
private val searchResultStateModel: SearchResultStateModel,
private val songSearchIntermediateController: SongSearchIntermediateController,
private val findThumbnailService: FindThumbnailService,
) {

suspend fun handleRecord(chosenSong: SongSearchResult) {
Expand All @@ -34,6 +38,10 @@ class ResultCellController(
songSearchIntermediateController.triggerSearch(0)
}
}

suspend fun tryGetThumbnail(pv: PVInfo): Result<ThumbnailInfo> {
return findThumbnailService.tryGetThumbnail(pv)
}
}

private val log = KInlineLogging.logger()
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mikufan.cx.songfinder.backend.service

import mikufan.cx.songfinder.backend.component.thumbnailfinder.ThumbnailFinder
import mikufan.cx.songfinder.backend.model.PVInfo
import mikufan.cx.songfinder.backend.model.ThumbnailInfo
import org.springframework.stereotype.Service

@Service
class FindThumbnailService(
thumbnailFinders: List<ThumbnailFinder>,
) {

private val finderMap = thumbnailFinders.associateBy { it.matchedPvService }

suspend fun tryGetThumbnail(pv: PVInfo): Result<ThumbnailInfo> {
val finder = finderMap[pv.pvService]
return if (finder == null) {
Result.failure(IllegalArgumentException("No thumbnail finder for pv service ${pv.pvService}"))
} else {
try {
Result.success(finder.findThumbnail(pv))
} catch (e: Exception) {
Result.failure(e)
}
}
}

fun evictCache() {
TODO("return back here once spring cache is added")
}
}

0 comments on commit b4e99eb

Please sign in to comment.