easyquotation优化提交

master
yg070520@sina.com 2 years ago
parent cb0dfd9b56
commit 1bdf4cfaab
  1. 10
      quotation_test.go
  2. 47
      sina/sinastock.go
  3. 5
      stock/stockmanager.go
  4. 10
      utils/helper.go

@ -14,13 +14,17 @@ func TestInit(t *testing.T) {
c := colly.NewCollector() c := colly.NewCollector()
SinaStock_spider := sina.NewSinaStock(c) SinaStock_spider := sina.NewSinaStock(c)
SinaStock_spider.Start() SinaStock_spider.Start("")
tt := time.NewTicker(time.Second * 3) tt := time.NewTicker(time.Second * 2)
for { for {
select { select {
case <- tt.C: 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"])
} }
} }
} }

@ -8,6 +8,8 @@ import (
"reflect" "reflect"
"strings" "strings"
"time" "time"
"io/ioutil"
"encoding/json"
) )
type SinaStock struct { type SinaStock struct {
@ -29,7 +31,8 @@ func (s *SinaStock) Url() string {
} }
func (s *SinaStock) OnRequest(r *colly.Request) { 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 { func (s *SinaStock) Split(r rune) bool {
@ -66,7 +69,11 @@ func (s *SinaStock) DecodeMarket(str string) {
a.Date = marketList[30] a.Date = marketList[30]
a.Time = marketList[31] a.Time = marketList[31]
a.Flag = marketList[32] 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) { 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() { go func() {
for { for {
select { select {
@ -84,15 +101,25 @@ func (s *SinaStock) Start() {
ipos := 1 ipos := 1
params := make([]string, 0) params := make([]string, 0)
param := "" param := ""
for k, _ := range stock.G_STOCK_MANAGER.StockList { //fmt.Println(stock.G_STOCK_MANAGER.IndexList)
if ipos%800 == 0 { data, err := ioutil.ReadFile(fn)
params = append(params, param) if err != nil {
param = "" //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 }else {
param += "," var stocks []string
ipos++ err = json.Unmarshal([]byte(data), &stocks)
param = strings.Join(stocks, ",")
} }
//fmt.Println(param)
params = append(params, param) params = append(params, param)
for _, str := range params { for _, str := range params {
url := s.Url() + str url := s.Url() + str

@ -49,6 +49,9 @@ func (s *StockManager) Load() {
eleList[0] = fmt.Sprintf("sz%s", eleList[0]) eleList[0] = fmt.Sprintf("sz%s", eleList[0])
} }
s.IndexList[eleList[0]] = s.SetBase(eleList) 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], ".板块") { } else if strings.HasSuffix(eleList[1], ".板块") {
s.BKkList[eleList[0]] = s.SetBase(eleList) s.BKkList[eleList[0]] = s.SetBase(eleList)
} else { } else {
@ -62,5 +65,7 @@ func (s *StockManager) Load() {
} }
} }
} }
//fmt.Println(s.IndexList)
//fmt.Println(s.StockList)
} }
} }

@ -1,10 +1,10 @@
package utils package utils
import ( import (
"io/ioutil" //"io/ioutil"
"math/rand" "math/rand"
"net/http" //"net/http"
"os" //"os"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -13,7 +13,7 @@ import (
"errors" "errors"
) )
func UpdateStockCodes() { func UpdateStockCodes() {/*
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
path = "./" path = "./"
@ -47,7 +47,7 @@ func UpdateStockCodes() {
} }
} }
} }*/
} }
func StartSwitch(s string, prefixs []string) bool { func StartSwitch(s string, prefixs []string) bool {

Loading…
Cancel
Save