atrade/src/main/kotlin/network/NewsService.kt

93 lines
3.7 KiB
Kotlin
Raw Normal View History

2026-01-21 18:59:55 +09:00
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
2026-01-22 16:21:18 +09:00
import model.DartFinancialResponse
2026-01-21 18:59:55 +09:00
import model.NaverNewsResponse
2026-01-23 17:05:09 +09:00
import service.DynamicNewsScraper
import service.SafeScraper
import service.UrlCacheManager
2026-01-21 18:59:55 +09:00
object NewsService {
private val client = HttpClient<CIOEngineConfig>(CIO) {
install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true })
2026-01-23 17:05:09 +09:00
}
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.ALL
2026-01-21 18:59:55 +09:00
}
}
2026-01-23 17:05:09 +09:00
suspend fun fetchAndIngestNews(corpInfo: CorpInfo) {
2026-01-21 18:59:55 +09:00
val clientId = "CqXQXHO3h0kqtYsXkePY" // 설정에서 가져오도록 수정 필요
val clientSecret = "DODCxb1M4Z"
2026-01-23 17:05:09 +09:00
var qlist = listOf<String>("${corpInfo.stockName} 분석","${corpInfo.stockName}[${corpInfo.stockCode}]", "${corpInfo.cName} 최근 동향", "${corpInfo.cName}")
qlist.forEach { query ->
try {
val response: NaverNewsResponse = client.get("https://openapi.naver.com/v1/search/news.json") {
parameter("query", query)
parameter("display", 3) // 최근 10개 뉴스
parameter("sort", "sim") // 유사도 순 (또는 date 발간순)
header("X-Naver-Client-Id", clientId)
header("X-Naver-Client-Secret", clientSecret)
}.body()
SafeScraper.scrapeParallel(corpInfo,response.items)
} catch (e: Exception) {
println("❌ 뉴스 가져오기 실패: ${e.message}")
2026-01-21 18:59:55 +09:00
}
}
}
2026-01-22 16:21:18 +09:00
2026-01-23 17:05:09 +09:00
// suspend fun fetchCorpInfo(corpCode: String): String {
// val apiKey = "61143d2af0759f6c28ce372d9e339d1e01687abc"
// val url = "https://opendart.fss.or.kr/api/company.json?crtfc_key=$apiKey&corp_code=$corpCode"
//
// return try {
// val response = client.get(url).body<CorpInfo>()
// "기업명: ${response.corp_name}, 주요사업: ${response.main_business}"
// } catch (e: Exception) {
// "기업 정보 로드 실패"
// }
// }
2026-01-22 16:21:18 +09:00
suspend fun fetchFinancialGrowth(corpCode: String?): String {
if (corpCode != null) {
val apiKey = "61143d2af0759f6c28ce372d9e339d1e01687abc"
// 단일회사 주요계정 API (재무상태표, 손익계산서 주요 항목)
val url = "https://opendart.fss.or.kr/api/fnlttSinglAcnt.json?crtfc_key=$apiKey&corp_code=$corpCode&bsns_year=2024&reprt_code=11011"
return try {
val response = client.get(url).body<DartFinancialResponse>()
val accounts = response.list ?: return "재무 데이터 없음"
2026-01-23 17:05:09 +09:00
var buffer : StringBuffer = StringBuffer()
buffer.append("[재무 분석 데이터]")
response.list.forEach { it
buffer.append("${it.account_nm} (당기)${it?.thstrm_amount}, (전기)${it?.frmtrm_amount}").append("\n")
}
return buffer.toString()
2026-01-22 16:21:18 +09:00
} catch (e: Exception) {
"재무 API 연동 실패: ${e.message}"
}
} else {
return ""
}
}
2026-01-21 18:59:55 +09:00
}