105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
package usmarket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type JSONData struct {
|
|
QueryID string `json:"QueryID"`
|
|
ResultCode string `json:"ResultCode"`
|
|
Result Result `json:"Result"`
|
|
}
|
|
type StockDataIndex struct {
|
|
P string `json:"p"`
|
|
LastPrice string `json:"lastPrice"`
|
|
Status string `json:"status"`
|
|
Ratio string `json:"ratio"`
|
|
Increase string `json:"increase"`
|
|
Code string `json:"code"`
|
|
Name string `json:"name"`
|
|
Market string `json:"market"`
|
|
}
|
|
type Tabs struct {
|
|
Text string `json:"text"`
|
|
Market string `json:"market"`
|
|
}
|
|
type Result struct {
|
|
List []StockDataIndex `json:"list"`
|
|
Tabs []Tabs `json:"tabs"`
|
|
Curtab string `json:"curtab"`
|
|
}
|
|
|
|
func Baidu_UsIndex() (map[string]*StockDataIndex, error) {
|
|
body, err := Baidu_Http("https://finance.pae.baidu.com/api/getbanner?market=america&finClientType=pc")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var data JSONData
|
|
err = json.Unmarshal(body, &data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if data.ResultCode != "0" {
|
|
return nil, errors.New("Baidue ResultCode:" + data.ResultCode)
|
|
}
|
|
|
|
indexMap := make(map[string]*StockDataIndex)
|
|
|
|
for _, v := range data.Result.List {
|
|
if v.Code == "IXIC" { // IXIC 纳斯达克
|
|
indexMap[v.Code] = &v
|
|
}
|
|
|
|
if v.Code == "DJI" { // DJI 道琼斯
|
|
indexMap[v.Code] = &v
|
|
}
|
|
|
|
if v.Code == "SPX" { // SPX 标普500
|
|
indexMap[v.Code] = &v
|
|
}
|
|
|
|
}
|
|
|
|
return indexMap, nil
|
|
|
|
}
|
|
|
|
func Baidu_Http(url string) ([]byte, error) {
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
req.Header.Set("accept", "application/json, text/plain, */*")
|
|
req.Header.Set("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
|
|
req.Header.Set("origin", "https://gushitong.baidu.com")
|
|
req.Header.Set("priority", "u=1, i")
|
|
req.Header.Set("referer", "https://gushitong.baidu.com/")
|
|
req.Header.Set("sec-ch-ua", `"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"`)
|
|
req.Header.Set("sec-ch-ua-mobile", "?0")
|
|
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
|
|
req.Header.Set("sec-fetch-dest", "empty")
|
|
req.Header.Set("sec-fetch-mode", "cors")
|
|
req.Header.Set("sec-fetch-site", "cross-site")
|
|
req.Header.Set("site-channel", "001")
|
|
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
bodyText, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bodyText, nil
|
|
}
|