diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/config/KtorClientConfig.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/config/KtorClientConfig.kt new file mode 100644 index 0000000..727dbe5 --- /dev/null +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/config/KtorClientConfig.kt @@ -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 + + } +} \ No newline at end of file diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/controller/mainpage/ResultCellController.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/controller/mainpage/ResultCellController.kt index 5c21ae6..fbd2d0e 100644 --- a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/controller/mainpage/ResultCellController.kt +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/controller/mainpage/ResultCellController.kt @@ -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.* @@ -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) { @@ -34,6 +38,10 @@ class ResultCellController( songSearchIntermediateController.triggerSearch(0) } } + + suspend fun tryGetThumbnail(pv: PVInfo): Result { + return findThumbnailService.tryGetThumbnail(pv) + } } private val log = KInlineLogging.logger() \ No newline at end of file diff --git a/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/service/FindThumbnailService.kt b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/service/FindThumbnailService.kt new file mode 100644 index 0000000..d9fd206 --- /dev/null +++ b/songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/service/FindThumbnailService.kt @@ -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, +) { + + private val finderMap = thumbnailFinders.associateBy { it.matchedPvService } + + suspend fun tryGetThumbnail(pv: PVInfo): Result { + 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") + } +} \ No newline at end of file