This commit is contained in:
lunaticbum 2026-05-04 11:19:52 +09:00
parent 23d505eabe
commit 0413fa3e2e
2 changed files with 25 additions and 3 deletions

View File

@ -17,6 +17,8 @@ import java.util.zip.ZipInputStream
import javax.xml.parsers.DocumentBuilderFactory
import kotlinx.serialization.encodeToString // 추가 필요
import java.nio.charset.Charset
@Serializable
data class StockItem(
val code: String,
@ -29,6 +31,25 @@ object StockUniverseLoader {
private val json = Json { ignoreUnknownKeys = true; prettyPrint = true }
private const val DEFAULT_FILE_PATH = "stocks_universe.json"
fun readSafeLines(file: File): List<String> {
val eucKr = Charset.forName("EUC-KR")
val utf8 = Charsets.UTF_8
// 우선 EUC-KR로 읽어봄
val lines = file.readLines(eucKr)
// 첫 줄에서 한글이 깨졌는지 검사 (정규식 활용)
// 한글이 하나도 없고 깨진 특수문자만 있다면 UTF-8로 재시도
val hasKorean = lines.firstOrNull()?.any { it in '\uAC00'..'\uD7A3' } ?: false
return if (hasKorean) {
lines
} else {
println("⚠️ EUC-KR에서 한글 미검출. UTF-8로 재시도합니다.")
file.readLines(utf8)
}
}
fun loadUniverse(filePath: String = DEFAULT_FILE_PATH): List<Pair<String, String>> {
return try {
val file = File(filePath)
@ -50,7 +71,8 @@ object StockUniverseLoader {
try {
val stockItems = items.map { StockItem(it.first, it.second) }
val jsonString = json.encodeToString(stockItems)
File(filePath).writeText(jsonString)
// File(filePath).writeText(jsonString)
File(filePath).writeText(jsonString, Charsets.UTF_8)
println("💾 [System] 유니버스 영구 저장 완료: 총 ${items.size}종목")
} catch (e: Exception) {
println("❌ 유니버스 저장 실패: ${e.message}")
@ -61,7 +83,7 @@ object StockUniverseLoader {
fun parseAndMergeCsv(file: File, targetJsonPath: String = DEFAULT_FILE_PATH): List<Pair<String, String>> {
val newItems = mutableListOf<Pair<String, String>>()
try {
val lines = file.readLines()
val lines = readSafeLines(file)
if (lines.isEmpty()) return loadUniverse(targetJsonPath)
// 헤더 자동 추적

View File

@ -707,7 +707,7 @@ object AutoTradingManager {
println("⏳ [Cycle Timeout] 사이클이 너무 길어져 초기화 후 재시작합니다.")
} catch (e: Exception) {
println("⚠️ [Loop Error] ${e.message}")
delay(1500)
delay(1000)
}
waitForNextCycle(waitTime)
}