Skip to content

Commit

Permalink
更新API支持美股、科创板
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiscn committed Aug 2, 2021
1 parent c8a2a42 commit 2ecee8a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 20 deletions.
8 changes: 4 additions & 4 deletions StocksBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,15 @@
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 = (
"$(inherited)",
"@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 = "";
Expand All @@ -604,15 +604,15 @@
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 = (
"$(inherited)",
"@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 = "";
Expand Down
94 changes: 79 additions & 15 deletions StocksBar/Networking/SinaStocksAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 []
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion StocksBar/Storyboards/AboutWindow.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="G7Y-Oi-afQ">
<rect key="frame" x="133" y="61" width="19" height="14"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="1.6" id="fwc-EO-rwS">
<textFieldCell key="cell" lineBreakMode="clipping" title="1.7" id="fwc-EO-rwS">
<font key="font" metaFont="controlContent" size="11"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
Expand Down

0 comments on commit 2ecee8a

Please sign in to comment.