Load shop products csv
This commit is contained in:
@@ -11,11 +11,11 @@ import Foundation
|
||||
extension Date {
|
||||
public var yyyyMMdd: String {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "yyyy-MM-dd"
|
||||
dateFormatter.dateFormat = "yyyyMMdd"
|
||||
return dateFormatter.string(from: self)
|
||||
}
|
||||
|
||||
public var yyyyMMdd_HHmmss: String {
|
||||
public var yyyyMMdd_HHmmss_forTime: String {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
||||
return dateFormatter.string(from: self)
|
||||
|
||||
@@ -148,7 +148,7 @@ extension DomesticShop {
|
||||
"serviceKey": openApiKey,
|
||||
]
|
||||
if let baseDate = baseDate {
|
||||
body["beginBasDt"] = baseDate.yyyyMMdd
|
||||
body["basDt"] = baseDate.yyyyMMdd
|
||||
}
|
||||
return body
|
||||
}
|
||||
@@ -170,10 +170,10 @@ extension DomesticShop {
|
||||
|
||||
extension KissShop {
|
||||
|
||||
public func getProduct(pageNo: Int) async throws -> (Int, [DomesticShop.Product]) {
|
||||
public func getProduct(baseDate: Date, pageNo: Int) async throws -> (Int, [DomesticShop.Product]) {
|
||||
return try await withUnsafeThrowingContinuation { continuation in
|
||||
|
||||
let request = DomesticShop.ProductRequest(openApiKey: self.credential.openApiKey, baseDate: Date(), pageNo: pageNo)
|
||||
let request = DomesticShop.ProductRequest(openApiKey: self.credential.openApiKey, baseDate: baseDate, pageNo: pageNo)
|
||||
request.query { result in
|
||||
switch result {
|
||||
case .success(let result):
|
||||
|
||||
@@ -54,6 +54,19 @@ class KissConsole {
|
||||
onLoadShop()
|
||||
}
|
||||
|
||||
func getCommand(_ line: String) -> (KissCommand?, [String]) {
|
||||
let args = line.split(separator: " ")
|
||||
let double = args.prefix(upTo: min(2, args.count)).joined(separator: " ")
|
||||
if let cmd = KissCommand(rawValue: double) {
|
||||
return (cmd, args.suffixStrings(from: 2))
|
||||
}
|
||||
let single = args.prefix(upTo: min(1, args.count)).joined(separator: " ")
|
||||
if let cmd = KissCommand(rawValue: single) {
|
||||
return (cmd, args.suffixStrings(from: 1))
|
||||
}
|
||||
return (nil, [])
|
||||
}
|
||||
|
||||
func run() {
|
||||
print("Enter command:")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
@@ -63,10 +76,7 @@ class KissConsole {
|
||||
guard let line = readLine(strippingNewline: true) else {
|
||||
continue
|
||||
}
|
||||
let args = line.split(separator: " ")
|
||||
let single = args.prefix(upTo: min(2, args.count)).joined(separator: " ")
|
||||
let cmd = KissCommand(rawValue: single)
|
||||
|
||||
let (cmd, args) = getCommand(line)
|
||||
if cmd?.needLogin == true {
|
||||
guard isLogined else {
|
||||
print("Need to login")
|
||||
@@ -80,14 +90,14 @@ class KissConsole {
|
||||
case .loginMock: await onLogin(isMock: true)
|
||||
case .loginReal: await onLogin(isMock: false)
|
||||
case .logout: await onLogout()
|
||||
case .search: await onSearch(args.suffixStrings(from: 1))
|
||||
case .buy: await onBuy(args.suffixStrings(from: 1))
|
||||
case .sell: await onSell(args.suffixStrings(from: 1))
|
||||
case .search: await onSearch(args)
|
||||
case .buy: await onBuy(args)
|
||||
case .sell: await onSell(args)
|
||||
case .loadShop: onLoadShop()
|
||||
case .updateShop: await onUpdateShop()
|
||||
case .look: await onLook(args.suffixStrings(from: 1))
|
||||
case .look: await onLook(args)
|
||||
default:
|
||||
print("Unknown command: \(single)")
|
||||
print("Unknown command: \(line)")
|
||||
}
|
||||
semaphore.signal()
|
||||
}
|
||||
@@ -120,7 +130,8 @@ extension KissConsole {
|
||||
defer {
|
||||
productsLock.unlock()
|
||||
}
|
||||
return products.filter { $0.key.contains(similarName) }
|
||||
// return products.filter { $0.key.contains(similarName) }
|
||||
return products.filter { $0.key.localizedStandardContains(similarName) }
|
||||
}
|
||||
|
||||
private func loadShop(_ profile: Bool = true) {
|
||||
@@ -223,29 +234,24 @@ extension KissConsole {
|
||||
}
|
||||
|
||||
private func onUpdateShop() async {
|
||||
guard let shop = shop else {
|
||||
guard let _ = shop else {
|
||||
print("Invalid shop instance")
|
||||
return
|
||||
}
|
||||
|
||||
var pageNo = 0
|
||||
var shopItems = [DomesticShop.ProductResponse.Item]()
|
||||
var baseDate = Date()
|
||||
var shopItems = [DomesticShop.Product]()
|
||||
|
||||
do {
|
||||
while true {
|
||||
pageNo += 1
|
||||
print("get pageNo: \(pageNo)")
|
||||
|
||||
let (totalCount, items) = try await shop.getProduct(pageNo: pageNo)
|
||||
shopItems.append(contentsOf: items)
|
||||
|
||||
print("got pageNo: \(pageNo) - \(shopItems.count)/\(totalCount)")
|
||||
if totalCount == 0 || shopItems.count >= totalCount {
|
||||
break
|
||||
}
|
||||
for _ in 0 ..< 7 {
|
||||
print("try to get shop at date \(baseDate.yyyyMMdd)")
|
||||
shopItems = await getAllProduct(baseDate: baseDate)
|
||||
if shopItems.isEmpty {
|
||||
let oneDaySeconds: TimeInterval = 60 * 60 * 24
|
||||
baseDate = baseDate.addingTimeInterval(-oneDaySeconds)
|
||||
}
|
||||
else {
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
print("\(error)")
|
||||
}
|
||||
|
||||
var stringCsv: String = ""
|
||||
@@ -265,13 +271,38 @@ extension KissConsole {
|
||||
}
|
||||
}
|
||||
|
||||
private func getAllProduct(baseDate: Date) async -> [DomesticShop.Product] {
|
||||
var pageNo = 0
|
||||
var shopItems = [DomesticShop.Product]()
|
||||
|
||||
do {
|
||||
while true {
|
||||
pageNo += 1
|
||||
print("get pageNo: \(pageNo)")
|
||||
|
||||
let (totalCount, items) = try await shop!.getProduct(baseDate: baseDate, pageNo: pageNo)
|
||||
shopItems.append(contentsOf: items)
|
||||
|
||||
print("got pageNo: \(pageNo) - \(shopItems.count)/\(totalCount)")
|
||||
if totalCount == 0 || shopItems.count >= totalCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
print("\(error)")
|
||||
}
|
||||
return shopItems
|
||||
}
|
||||
|
||||
private func onLook(_ args: [String]) async {
|
||||
guard args.count >= 1 else {
|
||||
print("No product name")
|
||||
print("No target name")
|
||||
return
|
||||
}
|
||||
guard let items = getProducts(similarName: args[0]) else {
|
||||
print("")
|
||||
let productName = String(args[0])
|
||||
print(args, productName, "\(productName.count)")
|
||||
guard let items = getProducts(similarName: productName), items.isEmpty == false else {
|
||||
print("No products like \(productName)")
|
||||
return
|
||||
}
|
||||
for item in items {
|
||||
@@ -286,6 +317,11 @@ extension KissConsole {
|
||||
|
||||
private extension Array {
|
||||
func suffixStrings(from: Int) -> [String] where Element == String.SubSequence {
|
||||
suffix(from: from).map { String($0) }
|
||||
/// https://ios-development.tistory.com/162
|
||||
/// Korean NFD(Normalization Form Canonical Decomposition) to NFC(Normalization Form Canonical Composition)
|
||||
guard from < count else {
|
||||
return []
|
||||
}
|
||||
return suffix(from: from).map { String($0) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,8 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
//let a = String("ㅅㅏㅁㅅㅓㅇ".utf16)
|
||||
//print(a.decomposedStringWithCanonicalMapping)
|
||||
//print(a.precomposedStringWithCanonicalMapping)
|
||||
|
||||
KissConsole().run()
|
||||
|
||||
2572
bin/data/shop-products.csv
Normal file
2572
bin/data/shop-products.csv
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user