From 2ecee8a54e9cfa4e0e770016823b279d45409a90 Mon Sep 17 00:00:00 2001 From: alexiscn Date: Mon, 2 Aug 2021 14:22:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0API=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=BE=8E=E8=82=A1=E3=80=81=E7=A7=91=E5=88=9B=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StocksBar.xcodeproj/project.pbxproj | 8 +- StocksBar/Networking/SinaStocksAPI.swift | 94 ++++++++++++++++---- StocksBar/Storyboards/AboutWindow.storyboard | 2 +- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/StocksBar.xcodeproj/project.pbxproj b/StocksBar.xcodeproj/project.pbxproj index ee1ba72..65d14ea 100644 --- a/StocksBar.xcodeproj/project.pbxproj +++ b/StocksBar.xcodeproj/project.pbxproj @@ -579,7 +579,7 @@ CODE_SIGN_IDENTITY = "Mac Developer"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 16; + CURRENT_PROJECT_VERSION = 17; DEVELOPMENT_TEAM = LX863G777H; INFOPLIST_FILE = "$(SRCROOT)/StocksBar/Supporting Files/Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( @@ -587,7 +587,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.12; - MARKETING_VERSION = 1.6; + MARKETING_VERSION = 1.7; PRODUCT_BUNDLE_IDENTIFIER = me.shuifeng.StocksBar; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -604,7 +604,7 @@ CODE_SIGN_IDENTITY = "Mac Developer"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 16; + CURRENT_PROJECT_VERSION = 17; DEVELOPMENT_TEAM = LX863G777H; INFOPLIST_FILE = "$(SRCROOT)/StocksBar/Supporting Files/Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( @@ -612,7 +612,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.12; - MARKETING_VERSION = 1.6; + MARKETING_VERSION = 1.7; PRODUCT_BUNDLE_IDENTIFIER = me.shuifeng.StocksBar; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/StocksBar/Networking/SinaStocksAPI.swift b/StocksBar/Networking/SinaStocksAPI.swift index 84af03e..52e2f7f 100644 --- a/StocksBar/Networking/SinaStocksAPI.swift +++ b/StocksBar/Networking/SinaStocksAPI.swift @@ -13,7 +13,55 @@ class SinaStocksAPI: StocksAPI { // 沪深 private let aURL = "http://hq.sinajs.cn/list=" - private let suggestionURL = "https://suggest3.sinajs.cn/suggest/type=2&key=" + /// type 决定可以搜出来哪些数据,此处搜索 A股、B股、ETF、LOF、港股、美股,如有特殊需要自行Fork自行修改 + private let suggestionURL = "https://suggest3.sinajs.cn/suggest/type=11,12,22,23,31,41&key=" + + /* https://n.sinaimg.cn/finance/stock/hq/src/hq-suggest.js + { + "11": "A 股", + "12": "B 股", + "13": "权证", + "14": "期货", + "15": "债券", + "21": "开基", + "22": "ETF", + "23": "LOF", + "24": "货基", + "25": "QDII", + "26": "封基", + "31": "港股", + "32": "窝轮", + "33": "港指数", + "41": "美股", + "42": "外期", + "81": "债券", + "82": "债券", + "103": "英股", + "120": "债券", + "111": "A股", + "71": "外汇", + "72": "基金", //场内基金(带市场) + "85": "期货",//内盘期货 + "86": "期货",//外盘期货 + "87": "期货",//内盘期货连续,股指期货连续,50ETF期权 + "88": "期货",//内盘股指期货 + "73": "新三板", + "74": "板块", + "75": "板块", //新浪行业 + "76": "板块",//申万行业 + "77": "板块",//申万二级 + "78": "板块", //热门概念(财汇概念) + "79": "板块",//地域板块 + "80": "板块",//证监会行业 + "101": "基金", //所有基金 + "100": "指数", //全球指数 + "102": "指数",//全部板块指数(概念、地域、行业) + "103": "英股", + "104": "国债", //(目前暂时是美国国债)" + "105": "ETF", //美股ETF,国际线索组-中文站)" + "106": "ETF", //美股ETF,国际线索组-英文站)" + "107": "msci" + }*/ func request(codes: [String], completion: @escaping StocksAPICompletion) { @@ -64,18 +112,16 @@ extension SinaStocksAPI { for stockText in stockContents { let components = stockText.split(separator: "=").map { return String($0) } if components.count == 2 { - let codeText = components[0].replacingOccurrences(of: "var ", with: "") + let code = components[0].replacingOccurrences(of: "var hq_str_", with: "").replacingOccurrences(of: "\n", with: "") let value = components[1].replacingOccurrences(of: "\"", with: "") - if let last = codeText.split(separator: "_").last { - let code = String(last) - if code.hasPrefix("jj"), let stock = createFundStockFromCode(code, value: value) { - stocks.append(stock) - } else if code.hasPrefix("hk"), let stock = createHKStockFromCode(code, value: value) { - stocks.append(stock) - } else if let stock = createStockFromCode(code, value: value) { - stocks.append(stock) - } - + if code.hasPrefix("jj"), let stock = createFundStockFromCode(code, value: value) { + stocks.append(stock) + } else if code.hasPrefix("rt_hk"), let stock = createHKStockFromCode(code, value: value) { + stocks.append(stock) + } else if code.hasPrefix("gb"), let stock = createGBStockFromCode(code, value: value) { + stocks.append(stock) + } else if let stock = createStockFromCode(code, value: value) { + stocks.append(stock) } } } @@ -103,7 +149,7 @@ extension SinaStocksAPI { private func createHKStockFromCode(_ code: String, value: String) -> Stock? { let components = value.split(separator: ",").map { return String($0) } - let stock = Stock(code: code) + let stock = Stock(code: code.replacingOccurrences(of: "rt_", with: "")) stock.symbol = components[1] stock.openPrice = Float(components[2]) ?? 0.0 stock.lastClosedPrice = Float(components[3]) ?? 0.0 @@ -115,6 +161,16 @@ extension SinaStocksAPI { return stock } + private func createGBStockFromCode(_ code: String, value: String) -> Stock? { + let components = value.split(separator: ",").map { return String($0) } + let stock = Stock(code: code) + stock.symbol = components[0] + stock.openPrice = Float(components[5]) ?? 0.0 + stock.lastClosedPrice = Float(components[26]) ?? 0.0 + stock.current = Float(components[1]) ?? 0.0 + return stock + } + private func parseSuggestionString(_ content: String?) -> [Stock] { guard let content = content else { return [] @@ -126,9 +182,17 @@ extension SinaStocksAPI { var stocks: [Stock] = [] for item in components { let array = item.split(separator: ",").map { return String($0) } - let stock = Stock(code: array[0]) + let type = array[1] + var code = array[3] + let symbol = array[0] + if type == "31" { + code = "hk" + code + } else if type == "41" { + code = "gb_" + code + } + let stock = Stock(code: code) stock.isFavorited = StockDataSource.shared.contains(stock) - stock.symbol = array[4] + stock.symbol = symbol stocks.append(stock) } return stocks diff --git a/StocksBar/Storyboards/AboutWindow.storyboard b/StocksBar/Storyboards/AboutWindow.storyboard index 1548a6c..fc0397d 100644 --- a/StocksBar/Storyboards/AboutWindow.storyboard +++ b/StocksBar/Storyboards/AboutWindow.storyboard @@ -49,7 +49,7 @@ - +