initial
This commit is contained in:
104
usmarket/baidu.go
Normal file
104
usmarket/baidu.go
Normal file
@@ -0,0 +1,104 @@
|
||||
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
|
||||
}
|
||||
102
usmarket/watch.go
Normal file
102
usmarket/watch.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package usmarket
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
UsMarket *MarketIndex
|
||||
)
|
||||
|
||||
// lock
|
||||
type MarketIndex struct {
|
||||
sync.RWMutex
|
||||
LastIndex map[string]*StockDataIndex
|
||||
Stats int // -1:未开市,0:开市,数据不正常,1: 开市,数据正常
|
||||
}
|
||||
|
||||
func NewMarket() {
|
||||
UsMarket = &MarketIndex{
|
||||
LastIndex: make(map[string]*StockDataIndex),
|
||||
Stats: -1,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MarketIndex) Set(stats int, index map[string]*StockDataIndex) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
m.Stats = stats
|
||||
m.LastIndex = index
|
||||
}
|
||||
|
||||
func (m *MarketIndex) Get() (int, map[string]*StockDataIndex) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
return m.Stats, m.LastIndex
|
||||
}
|
||||
|
||||
func CheckUsMarketOpen() int {
|
||||
var err error
|
||||
if !IsUSMarketOpen() {
|
||||
UsMarket.Set(-1, nil)
|
||||
return -1
|
||||
}
|
||||
|
||||
newIndex, err := Baidu_UsIndex()
|
||||
if err != nil {
|
||||
log.Println("Baidu_UsIndex Error:", err)
|
||||
UsMarket.Set(-1, nil)
|
||||
return -1
|
||||
}
|
||||
|
||||
_, LastStockIndex := UsMarket.Get()
|
||||
if len(LastStockIndex) == 0 {
|
||||
UsMarket.Set(0, newIndex)
|
||||
return 0
|
||||
}
|
||||
|
||||
// IXIC 纳斯达克,DJI 道琼斯,SPX 标普500
|
||||
if LastStockIndex["IXIC"].LastPrice == newIndex["IXIC"].LastPrice && LastStockIndex["DJI"].LastPrice == newIndex["DJI"].LastPrice && LastStockIndex["SPX"].LastPrice == newIndex["SPX"].LastPrice {
|
||||
// log.Println("ERROR #103, IXIC,DJI,SPX LastPrice no change")
|
||||
UsMarket.Set(-1, LastStockIndex)
|
||||
return 0
|
||||
}
|
||||
|
||||
if newIndex["IXIC"].Increase == "0.00" && newIndex["DJI"].Increase == "0.00" && newIndex["SPX"].Increase == "0.00" {
|
||||
// log.Println("ERROR #104, IXIC,DJI,SPX Increase is 0")
|
||||
UsMarket.Set(-1, LastStockIndex)
|
||||
return 0
|
||||
}
|
||||
|
||||
// jsonBytes, _ := json.Marshal(newIndex)
|
||||
// log.Println("USMarket:", string(jsonBytes))
|
||||
UsMarket.Set(1, newIndex)
|
||||
return 1
|
||||
}
|
||||
|
||||
// 判断美股是否开市
|
||||
func IsUSMarketOpen() bool {
|
||||
// 获取当前时间
|
||||
now := time.Now()
|
||||
|
||||
// 设置美国东部时间
|
||||
loc, _ := time.LoadLocation("America/New_York")
|
||||
|
||||
// 将当前时间转换为美国东部时间
|
||||
nowInNY := now.In(loc)
|
||||
|
||||
// 判断是否为周末
|
||||
if nowInNY.Weekday() == time.Saturday || nowInNY.Weekday() == time.Sunday {
|
||||
return false
|
||||
}
|
||||
|
||||
// 判断是否在交易时间内
|
||||
openTime := time.Date(nowInNY.Year(), nowInNY.Month(), nowInNY.Day(), 9, 30, 0, 0, loc)
|
||||
closeTime := time.Date(nowInNY.Year(), nowInNY.Month(), nowInNY.Day(), 16, 0, 0, 0, loc)
|
||||
|
||||
return nowInNY.After(openTime) && nowInNY.Before(closeTime)
|
||||
}
|
||||
Reference in New Issue
Block a user