package network import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIOEngineConfig import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.plugins.logging.DEFAULT import io.ktor.client.plugins.logging.LogLevel import io.ktor.client.plugins.logging.Logger import io.ktor.client.plugins.logging.Logging import io.ktor.client.request.get import io.ktor.client.request.header import io.ktor.client.request.parameter import io.ktor.http.ContentType.Application.Json import io.ktor.serialization.kotlinx.json.json import kotlinx.serialization.json.Json import model.NaverNewsResponse object NewsService { private val client = HttpClient(CIO) { install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) install(Logging) { logger = Logger.DEFAULT level = LogLevel.ALL } } } suspend fun fetchAndIngestNews(query: String) { val clientId = "CqXQXHO3h0kqtYsXkePY" // 설정에서 가져오도록 수정 필요 val clientSecret = "DODCxb1M4Z" try { val response: NaverNewsResponse = client.get("https://openapi.naver.com/v1/search/news.json") { parameter("query", query) parameter("display", 10) // 최근 10개 뉴스 parameter("sort", "sim") // 유사도 순 (또는 date 발간순) header("X-Naver-Client-Id", clientId) header("X-Naver-Client-Secret", clientSecret) }.body() response.items.forEach { item -> // HTML 태그 제거 및 텍스트 정제 val cleanTitle = item.title.replace(Regex("<[^>]*>"), "") val cleanDesc = item.description.replace(Regex("<[^>]*>"), "") val fullText = "[$cleanTitle] $cleanDesc" println(fullText) // RAG 서비스에 학습(Ingest) 시키기 RagService.ingest( text = fullText, meta = "{\"link\": \"${item.originallink}\", \"date\": \"${item.pubDate}\"}" ) } println("📰 '${query}' 관련 뉴스 10개 학습 완료") } catch (e: Exception) { println("❌ 뉴스 가져오기 실패: ${e.message}") } } }