diff --git a/quotation_test.go b/quotation_test.go index a47eea1..ee24de0 100644 --- a/quotation_test.go +++ b/quotation_test.go @@ -14,13 +14,17 @@ func TestInit(t *testing.T) { c := colly.NewCollector() SinaStock_spider := sina.NewSinaStock(c) - SinaStock_spider.Start() + SinaStock_spider.Start("") - tt := time.NewTicker(time.Second * 3) + tt := time.NewTicker(time.Second * 2) for { select { case <- tt.C: - fmt.Println(stock.G_STOCK_MANAGER.StockList["sh600000"]) + currentTime := time.Now() + timeFormatted := currentTime.Format("2006-01-02 15:04:05") + fmt.Println("Current time:", timeFormatted) + fmt.Println(stock.G_STOCK_MANAGER.StockList["sz002215"]) + fmt.Println(stock.G_STOCK_MANAGER.StockList["sh000001"]) } } } diff --git a/sina/sinastock.go b/sina/sinastock.go index 40af42f..796a914 100644 --- a/sina/sinastock.go +++ b/sina/sinastock.go @@ -8,6 +8,8 @@ import ( "reflect" "strings" "time" + "io/ioutil" + "encoding/json" ) type SinaStock struct { @@ -29,7 +31,8 @@ func (s *SinaStock) Url() string { } func (s *SinaStock) OnRequest(r *colly.Request) { - // fmt.Println(r.URL) + //fmt.Println(r.URL) + r.Headers.Set("Referer", "http://finance.sina.com.cn/") } func (s *SinaStock) Split(r rune) bool { @@ -66,7 +69,11 @@ func (s *SinaStock) DecodeMarket(str string) { a.Date = marketList[30] a.Time = marketList[31] a.Flag = marketList[32] - stock.G_STOCK_MANAGER.StockList[symbol].Market = a + if _, ok := stock.G_STOCK_MANAGER.StockList[symbol]; ok { + stock.G_STOCK_MANAGER.StockList[symbol].Market = a + }else{ + fmt.Println("not found", symbol) + } } func (s *SinaStock) OnResponse(res *colly.Response) { @@ -76,7 +83,17 @@ func (s *SinaStock) OnResponse(res *colly.Response) { } } -func (s *SinaStock) Start() { +//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) { go func() { for { select { @@ -84,15 +101,25 @@ func (s *SinaStock) Start() { ipos := 1 params := make([]string, 0) param := "" - for k, _ := range stock.G_STOCK_MANAGER.StockList { - if ipos%800 == 0 { - params = append(params, param) - 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++ } - param += k - param += "," - ipos++ + }else { + var stocks []string + err = json.Unmarshal([]byte(data), &stocks) + param = strings.Join(stocks, ",") } + //fmt.Println(param) params = append(params, param) for _, str := range params { url := s.Url() + str diff --git a/stock/stockmanager.go b/stock/stockmanager.go index f8bed1b..cb85589 100644 --- a/stock/stockmanager.go +++ b/stock/stockmanager.go @@ -49,6 +49,9 @@ func (s *StockManager) Load() { eleList[0] = fmt.Sprintf("sz%s", eleList[0]) } s.IndexList[eleList[0]] = s.SetBase(eleList) + if utils.EndSwitch(eleList[1], []string{"上证指数.沪指数", "深证成指.深指数", "创业板指.深指数"}) { + s.StockList[eleList[0]] = s.SetBase(eleList) + } } else if strings.HasSuffix(eleList[1], ".板块") { s.BKkList[eleList[0]] = s.SetBase(eleList) } else { @@ -62,5 +65,7 @@ func (s *StockManager) Load() { } } } + //fmt.Println(s.IndexList) + //fmt.Println(s.StockList) } } diff --git a/utils/helper.go b/utils/helper.go index 331cce4..d8ec32a 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -1,10 +1,10 @@ package utils import ( - "io/ioutil" + //"io/ioutil" "math/rand" - "net/http" - "os" + //"net/http" + //"os" "reflect" "strconv" "strings" @@ -13,7 +13,7 @@ import ( "errors" ) -func UpdateStockCodes() { +func UpdateStockCodes() {/* path, err := os.Getwd() if err != nil { path = "./" @@ -47,7 +47,7 @@ func UpdateStockCodes() { } } - } + }*/ } func StartSwitch(s string, prefixs []string) bool {