160 lines
5.3 KiB
Swift
160 lines
5.3 KiB
Swift
//
|
|
// KissConsole+Test.swift
|
|
// KissMeConsole
|
|
//
|
|
// Created by ened-book-m1 on 2023/06/22.
|
|
//
|
|
|
|
import Foundation
|
|
import KissMe
|
|
|
|
|
|
extension KissConsole {
|
|
|
|
func onTest(_ args: [String]) {
|
|
guard args.count == 1 else {
|
|
return
|
|
}
|
|
switch args[0] {
|
|
case "001": onTest_001()
|
|
default: break
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: 모든 prices-yyyyMM01.csv 에 컴마 데이터를 보정하는 임시코드
|
|
extension KissConsole {
|
|
|
|
private func onTest_001() {
|
|
let all = getAllProducts()
|
|
for item in all {
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
Task {
|
|
let productNo = item.shortCode
|
|
|
|
let priceUrls = getAllPrices(productNo: productNo)
|
|
for (month, url) in priceUrls {
|
|
|
|
do {
|
|
let _ = try [CapturePrice].readCsv(fromFile: url)
|
|
semaphore.signal()
|
|
return
|
|
} catch {
|
|
print(error)
|
|
print("TRYING RECOVER \(item.shortCode)")
|
|
}
|
|
|
|
do {
|
|
//print("cur: \(productNo)")
|
|
let result = try await account!.getCurrentPrice(productNo: productNo)
|
|
guard let output = result.output else {
|
|
print("Invalid result's output for \(productNo)")
|
|
semaphore.signal()
|
|
return
|
|
}
|
|
|
|
if output.capitalCurrency.hasComma || output.facePriceCurrency.hasComma {
|
|
print("COMMA: \(productNo)")
|
|
await adjustComma(productNo: productNo, day: month, output: output)
|
|
}
|
|
|
|
try await Task.sleep(nanoseconds: 1_000_000_000 / 10)
|
|
} catch {
|
|
print(error)
|
|
semaphore.signal()
|
|
return
|
|
}
|
|
}
|
|
|
|
semaphore.signal()
|
|
}
|
|
semaphore.wait()
|
|
}
|
|
print("FINISHED")
|
|
}
|
|
|
|
|
|
private func getAllPrices(productNo: String) -> [String: URL] {
|
|
guard let enumerator = FileManager.subPathFiles("data/\(productNo)") else {
|
|
return [:]
|
|
}
|
|
var urlMap = [String: URL]()
|
|
for case let fileUrl as URL in enumerator {
|
|
guard fileUrl.pathExtension == "csv" else {
|
|
continue
|
|
}
|
|
|
|
let fileNameFrag = fileUrl.lastPathComponent.split(separator: ".")
|
|
guard fileNameFrag.count == 2 else {
|
|
continue
|
|
}
|
|
|
|
let monthFrag = String(fileNameFrag[0].suffix(8))
|
|
if fileNameFrag[0].prefix(7) == "prices-", let _ = monthFrag.yyyyMMdd {
|
|
urlMap[monthFrag] = fileUrl
|
|
}
|
|
}
|
|
return urlMap
|
|
}
|
|
|
|
|
|
private func adjustComma(productNo: String, day: String, output: Domestic.CurrentPrice) async {
|
|
let fileUrl = KissConsole.priceFileUrl(productNo: productNo, day: day)
|
|
guard let stringCsv = try? String(contentsOfFile: fileUrl.path) else {
|
|
return
|
|
}
|
|
|
|
var newCsv = ""
|
|
var modified = false
|
|
|
|
let rows = stringCsv.split(separator: "\n")
|
|
for (index, row) in rows.enumerated() {
|
|
if index == 0 {
|
|
newCsv += row
|
|
newCsv += "\n"
|
|
continue
|
|
}
|
|
var newRow = row
|
|
if output.facePriceCurrency.hasComma {
|
|
let r = newRow.ranges(of: output.facePriceCurrency)
|
|
if r.count == 1 {
|
|
let commaRemoved = output.facePriceCurrency.replacingOccurrences(of: ",", with: "")
|
|
//print("newRow: \(newRow)")
|
|
//print("commaRemoved: \(commaRemoved)")
|
|
if let s = r.first {
|
|
newRow.replaceSubrange(s, with: commaRemoved)
|
|
//print("afterRow: \(newRow)")
|
|
modified = true
|
|
}
|
|
}
|
|
}
|
|
|
|
if output.capitalCurrency.hasComma {
|
|
let r = newRow.ranges(of: output.capitalCurrency)
|
|
if r.count == 1 {
|
|
let commaRemoved = output.capitalCurrency.replacingOccurrences(of: ",", with: "")
|
|
//print("newRow: \(newRow)")
|
|
//print("commaRemoved: \(commaRemoved)")
|
|
if let s = r.first {
|
|
newRow.replaceSubrange(s, with: commaRemoved)
|
|
//print("afterRow: \(newRow)")
|
|
modified = true
|
|
}
|
|
}
|
|
}
|
|
newCsv += newRow
|
|
newCsv += "\n"
|
|
}
|
|
|
|
if modified {
|
|
do {
|
|
try newCsv.write(toFile: fileUrl.path, atomically: true, encoding: .utf8)
|
|
print("REWRITTEN \(productNo) at \(fileUrl.path)")
|
|
} catch {
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
}
|