Quotation from Sina source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
easyquotation/sina/sinastock.go

132 lines
2.9 KiB

4 years ago
package sina
import (
"easyquotation/stock"
"easyquotation/utils"
"fmt"
"github.com/gocolly/colly"
"reflect"
"strings"
"time"
"io/ioutil"
"encoding/json"
4 years ago
)
type SinaStock struct {
Spider
}
func NewSinaStock(c *colly.Collector) *SinaStock {
s := &SinaStock{Spider{Timer: time.NewTicker(time.Second * 3)}}
s.Collector(c)
s.C.OnRequest(s.OnRequest)
s.C.OnResponse(s.OnResponse)
return s
}
func (s *SinaStock) Collector(c *colly.Collector) {
s.Spider.C = c.Clone()
}
func (s *SinaStock) Url() string {
return fmt.Sprintf("http://hq.sinajs.cn/rn=%d&list=", time.Now().Unix())
}
func (s *SinaStock) OnRequest(r *colly.Request) {
//fmt.Println(r.URL)
r.Headers.Set("Referer", "http://finance.sina.com.cn/")
4 years ago
}
func (s *SinaStock) Split(r rune) bool {
return r == ' ' || r == '_' || r == '='
}
func (s *SinaStock) DecodeMarket(str string) {
l := utils.SplitString(str, s.Split)
if (len(l) < 5){
return
}
symbol := l[3] // 代码
market := l[4][1 : len(l[4])-1] // 去除左右" "
marketList := strings.Split(market, ",")
if (len(marketList) < 33) {
return
}
a := stock.Market{}
eleVals := reflect.ValueOf(&a).Elem()
utils.DecodeStock(marketList[:10], eleVals)
// 买五档
for i := 0; i < 10; i += 2 {
bEntry := stock.Entry{}
vals := reflect.ValueOf(&bEntry).Elem()
utils.DecodeStock(marketList[10+i:10+i+2], vals)
a.BuyEntryList = append(a.BuyEntryList, bEntry)
}
// 卖五档
for i := 0; i < 10; i += 2 {
bEntry := stock.Entry{}
vals := reflect.ValueOf(&bEntry).Elem()
utils.DecodeStock(marketList[20+i:20+i+2], vals)
a.SellEntryList = append(a.SellEntryList, bEntry)
}
a.Date = marketList[30]
a.Time = marketList[31]
a.Flag = marketList[32]
if _, ok := stock.G_STOCK_MANAGER.StockList[symbol]; ok {
stock.G_STOCK_MANAGER.StockList[symbol].Market = a
}else{
fmt.Println("not found", symbol)
}
4 years ago
}
func (s *SinaStock) OnResponse(res *colly.Response) {
l := strings.Split(string(res.Body), ";")
for _, str := range l {
s.DecodeMarket(str)
}
}
//contains
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (s *SinaStock) Start(fn string) {
4 years ago
go func() {
for {
select {
case <-s.Spider.Timer.C:
ipos := 1
params := make([]string, 0)
param := ""
//fmt.Println(stock.G_STOCK_MANAGER.IndexList)
data, err := ioutil.ReadFile(fn)
if err != nil {
//fmt.Println(err)
for k, _ := range stock.G_STOCK_MANAGER.StockList {
if ipos%800 == 0 {
params = append(params, param)
param = ""
}
param += k
param += ","
ipos++
4 years ago
}
}else {
var stocks []string
err = json.Unmarshal([]byte(data), &stocks)
param = strings.Join(stocks, ",")
4 years ago
}
//fmt.Println(param)
4 years ago
params = append(params, param)
for _, str := range params {
url := s.Url() + str
go s.C.Visit(url)
}
}
}
}()
}