// // 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 = "
00NORMAL SERVICE.
이스트아시아홀딩스인베스트먼트리미티드900110삼천당제약000250중앙에너비스000440신라섬유001000안국약품001540무림에스피001810이화공영001840피에스텍002230삼일기업공사002290한일사료0058601011637
" 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) } } */ } private func fix_first_csv_header_field() { guard let enumerator = FileManager.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 = FileManager.subPathFiles("data") else { return } for case let fileUrl as URL in enumerator { let r = KissConsole.validateCandleMinute(fileUrl) switch r { case .ok, .invalidFileName: print("OK \(fileUrl)") continue default: assertionFailure() print("\(r) at \(fileUrl)") } } } func check_today_candle_exist() { guard let enumerator = FileManager.subPathFiles("data") else { return } var productUrls = [String: URL]() var candleUrls = [String: URL]() for case let fileUrl as URL in enumerator { guard true != fileUrl.isDirectoryExists else { let productNo = fileUrl.lastPathComponent if let _ = Int(productNo) { productUrls[productNo] = fileUrl } continue } guard fileUrl.pathExtension == "csv" else { continue } let fileName = fileUrl.lastPathComponent let periodDir = fileUrl.deletingLastPathComponent() let period = periodDir.lastPathComponent let productNoDir = periodDir.deletingLastPathComponent() let productNo = productNoDir.lastPathComponent guard let _ = Int(productNo), period == "min" else { continue } if Date().yyyyMMdd == fileName.parseCandleDate() { candleUrls[productNo] = fileUrl } } for productNo in productUrls.keys { guard nil == candleUrls[productNo] else { continue } print("\(productNo) have no today candle!!") } print("all scan finished") } private func move_candles_to_min_subdir() { // Move all of csv into min sub-directory // ex) data/000020/candle-20230530.csv --> /data/000020/min/candle-20230530.csv // guard let enumerator = FileManager.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 productNo = upper.lastPathComponent guard let _ = Int(productNo) else { continue } let newPath = upper.appending(path: "min") let newUrl = newPath.appending(path: fileName) //print("file: \(fileUrl) -> \(newUrl)") do { try FileManager.default.createDirectory(at: newPath, withIntermediateDirectories: true) try FileManager.default.moveItem(at: fileUrl, to: newUrl) } catch { print(error) exit(1) } } }