195 lines
5.8 KiB
Swift
195 lines
5.8 KiB
Swift
//
|
|
// test.swift
|
|
// KissMeConsole
|
|
//
|
|
// Created by ened-book-m1 on 2023/05/16.
|
|
//
|
|
|
|
import Foundation
|
|
import KissMe
|
|
|
|
|
|
private func test_array_to_csv() {
|
|
|
|
}
|
|
|
|
|
|
private func test_login_get_volume_ranking() async {
|
|
let isMock = false
|
|
let credential: Credential
|
|
|
|
do {
|
|
/// isMock 이 true 이면, mock-server.json 에서 로딩하여 생성
|
|
/// isMock 이 false 이면, real-server.json 에서 로딩하여 생성
|
|
/// server.json 의 구조
|
|
/// {
|
|
/// "isMock": true,
|
|
/// "accountNo": "12345678-90"
|
|
/// "appKey": "xxxxxxxx",
|
|
/// "appSecret": "yyyyyyyyyyyyyyyyyy"
|
|
/// }
|
|
///
|
|
credential = try KissCredential(isMock: isMock)
|
|
} catch {
|
|
print("\(error)")
|
|
return
|
|
}
|
|
|
|
|
|
let account = KissAccount(credential: credential)
|
|
do {
|
|
if try await account.login() {
|
|
let option = RankingOption(divisionClass: .equity, belongClass: .averageVolume)
|
|
let result = try await account.getVolumeRanking(option: option)
|
|
print("\(result)")
|
|
}
|
|
} catch {
|
|
print("\(error)")
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
private func test_json_result() {
|
|
let str = "{\"rt_cd\":\"1\",\"msg_cd\":\"EGW00205\",\"msg1\":\"credentials_type이 유효하지 않습니다.(Bearer)\"}"
|
|
|
|
do {
|
|
let decoder = JSONDecoder()
|
|
decoder.keyDecodingStrategy = .useDefaultKeys
|
|
|
|
let data = Data(str.utf8)
|
|
let result = try decoder.decode(VolumeRankResult.self, from: data)
|
|
print("\(result)")
|
|
}
|
|
catch {
|
|
print("\(error)")
|
|
}
|
|
}
|
|
|
|
|
|
private func test_xml_result() {
|
|
/*
|
|
let str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><response><header><resultCode>00</resultCode><resultMsg>NORMAL SERVICE.</resultMsg></header><body><items><item><korSecnNm>이스트아시아홀딩스인베스트먼트리미티드</korSecnNm><shotnIsin>900110</shotnIsin></item><item><korSecnNm>삼천당제약</korSecnNm><shotnIsin>000250</shotnIsin></item><item><korSecnNm>중앙에너비스</korSecnNm><shotnIsin>000440</shotnIsin></item><item><korSecnNm>신라섬유</korSecnNm><shotnIsin>001000</shotnIsin></item><item><korSecnNm>안국약품</korSecnNm><shotnIsin>001540</shotnIsin></item><item><korSecnNm>무림에스피</korSecnNm><shotnIsin>001810</shotnIsin></item><item><korSecnNm>이화공영</korSecnNm><shotnIsin>001840</shotnIsin></item><item><korSecnNm>피에스텍</korSecnNm><shotnIsin>002230</shotnIsin></item><item><korSecnNm>삼일기업공사</korSecnNm><shotnIsin>002290</shotnIsin></item><item><korSecnNm>한일사료</korSecnNm><shotnIsin>005860</shotnIsin></item></items><numOfRows>10</numOfRows><pageNo>1</pageNo><totalCount>1637</totalCount></body></response>"
|
|
|
|
let data = Data(str.utf8)
|
|
|
|
class ResultHelper: NSObject, XMLParserDelegate {
|
|
|
|
struct Result: Codable {
|
|
|
|
}
|
|
|
|
var result: Result?
|
|
|
|
func parserDidStartDocument(_ parser: XMLParser) {
|
|
}
|
|
|
|
func parserDidEndDocument(_ parser: XMLParser) {
|
|
}
|
|
}
|
|
|
|
|
|
do {
|
|
let helper = ResultHelper()
|
|
let parser = XMLParser(data: data)
|
|
parser.shouldProcessNamespaces = true
|
|
parser.delegate = helper
|
|
if parser.parse() {
|
|
print(helper.result)
|
|
}
|
|
}
|
|
*/
|
|
}
|
|
|
|
|
|
func subPathFiles(_ subpath: String) -> FileManager.DirectoryEnumerator? {
|
|
let baseUrl = URL.currentDirectory().appending(path: subpath)
|
|
let manager = FileManager.default
|
|
let resourceKeys : [URLResourceKey] = []
|
|
let enumerator = manager.enumerator(at: baseUrl, includingPropertiesForKeys: resourceKeys, options: [.skipsHiddenFiles]) { (url, error) -> Bool in
|
|
print("directoryEnumerator error at \(url): ", error)
|
|
return true
|
|
}
|
|
return enumerator
|
|
}
|
|
|
|
|
|
private func fix_first_csv_header_field() {
|
|
guard let enumerator = subPathFiles("data") else {
|
|
return
|
|
}
|
|
for case let fileUrl as URL in enumerator {
|
|
guard fileUrl.pathExtension == "csv" else {
|
|
continue
|
|
}
|
|
guard var stringCsv = try? String(contentsOfFile: fileUrl.path) else {
|
|
print("Cannot load \(fileUrl)")
|
|
continue
|
|
}
|
|
|
|
guard let range = stringCsv.range(of: ",\n") else {
|
|
continue
|
|
}
|
|
stringCsv.remove(at: range.lowerBound)
|
|
|
|
do {
|
|
try stringCsv.write(toFile: fileUrl.path, atomically: true, encoding: .utf8)
|
|
print("wrote \(fileUrl.lastPathComponent)")
|
|
} catch {
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private func check_candle_csv() {
|
|
guard let enumerator = subPathFiles("data") else {
|
|
return
|
|
}
|
|
|
|
for case let fileUrl as URL in enumerator {
|
|
let r = KissConsole.validateCandle(fileUrl)
|
|
switch r {
|
|
case .ok, .invalidFileName:
|
|
print("OK \(fileUrl)")
|
|
continue
|
|
default:
|
|
assertionFailure()
|
|
print("\(r) at \(fileUrl)")
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func move_candles_to_min_subdir() {
|
|
guard let enumerator = subPathFiles("data") else {
|
|
return
|
|
}
|
|
|
|
var urls = [URL]()
|
|
for case let fileUrl as URL in enumerator {
|
|
guard fileUrl.pathExtension == "csv" else {
|
|
continue
|
|
}
|
|
urls.append(fileUrl)
|
|
}
|
|
|
|
for fileUrl in urls {
|
|
let fileName = fileUrl.lastPathComponent
|
|
let upper = fileUrl.deletingLastPathComponent()
|
|
|
|
let newPath = upper.appending(path: "min")
|
|
let newUrl = newPath.appending(path: fileName)
|
|
|
|
//print("file: \(fileUrl) -> \(newUrl)")
|
|
do {
|
|
try FileManager.default.createDirectory(at: upper, withIntermediateDirectories: true)
|
|
try FileManager.default.moveItem(at: fileUrl, to: newUrl)
|
|
}
|
|
catch {
|
|
print(error)
|
|
exit(1)
|
|
}
|
|
}
|
|
}
|