From 36a8479aa4ed1f97aa9ae77467262e558983f23e Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Wed, 26 Nov 2025 17:00:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96codes-server,=E5=BE=85?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/codes-server.go | 142 ++++++++++++++++++++++++++++++++++------- 1 file changed, 119 insertions(+), 23 deletions(-) diff --git a/extend/codes-server.go b/extend/codes-server.go index 61e9cdc..a6f9e68 100644 --- a/extend/codes-server.go +++ b/extend/codes-server.go @@ -3,43 +3,147 @@ package extend import ( "encoding/json" "fmt" - "github.com/injoyai/conv" - "github.com/injoyai/tdx" "io" + "iter" "net/http" - "path/filepath" + + "github.com/injoyai/base/maps" + "github.com/injoyai/conv" + "github.com/injoyai/logs" + "github.com/injoyai/tdx" + "github.com/robfig/cron/v3" ) -func ListenCodesHTTP(port int, filename ...string) error { - code, err := tdx.DialCodes(conv.Default(filepath.Join(tdx.DefaultDatabaseDir, "codes.db"), filename...)) +func ListenCodesHTTP(port int, op ...tdx.Codes2Option) error { + code, err := tdx.NewCodes2(op...) if err != nil { return nil } + succ := func(w http.ResponseWriter, data any) { + w.WriteHeader(http.StatusOK) + w.Write(conv.Bytes(data)) + } return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.RequestURI { + case "/all": + case "/stocks": - ls := code.GetStocks() - w.WriteHeader(http.StatusOK) - w.Write(conv.Bytes(ls)) + succ(w, code.GetStocks()) case "/etfs": - ls := code.GetETFs() - w.WriteHeader(http.StatusOK) - w.Write(conv.Bytes(ls)) + succ(w, code.GetETFs()) + case "/indexes": + succ(w, code.GetIndexes()) default: http.NotFound(w, r) } })) } -func DialCodesHTTP(address string) *CodesHTTP { - return &CodesHTTP{address: address} +func DialCodesHTTP(address string) (c *CodesHTTP, err error) { + c = &CodesHTTP{address: address} + cr := cron.New(cron.WithSeconds()) + _, err = cr.AddFunc("0 20 9 * * *", func() { logs.PrintErr(c.Update()) }) + if err != nil { + return + } + err = c.Update() + if err != nil { + return + } + cr.Start() + return c, nil } type CodesHTTP struct { address string + stocks tdx.CodeModels + etfs tdx.CodeModels + indexes tdx.CodeModels + m maps.Generic[string, *tdx.CodeModel] } -func (this *CodesHTTP) getList(path string) ([]string, error) { +func (this *CodesHTTP) Iter() iter.Seq2[string, *tdx.CodeModel] { + return func(yield func(string, *tdx.CodeModel) bool) { + for _, v := range this.stocks { + if !yield(v.FullCode(), v) { + return + } + } + for _, v := range this.etfs { + if !yield(v.FullCode(), v) { + return + } + } + for _, v := range this.indexes { + if !yield(v.FullCode(), v) { + return + } + } + } +} + +func (this *CodesHTTP) Get(code string) *tdx.CodeModel { + return this.m.MustGet(code) +} + +func (this *CodesHTTP) GetName(code string) string { + v := this.m.MustGet(code) + if v != nil { + return v.Name + } + return "" +} + +func (this *CodesHTTP) GetStocks(limit ...int) tdx.CodeModels { + return this.stocks +} + +func (this *CodesHTTP) GetStockCodes(limit ...int) []string { + return this.stocks.Codes() +} + +func (this *CodesHTTP) GetETFs(limit ...int) tdx.CodeModels { + return this.etfs +} + +func (this *CodesHTTP) GetETFCodes(limit ...int) []string { + return this.etfs.Codes() +} + +func (this *CodesHTTP) GetIndexes(limits ...int) tdx.CodeModels { + return this.indexes +} + +func (this *CodesHTTP) GetIndexCodes(limits ...int) []string { + return this.indexes.Codes() +} + +func (this *CodesHTTP) Update() (err error) { + this.stocks, err = this.getList("/stocks") + if err != nil { + return + } + for _, v := range this.stocks { + this.m.Set(v.FullCode(), v) + } + this.etfs, err = this.getList("/etfs") + if err != nil { + return + } + for _, v := range this.etfs { + this.m.Set(v.FullCode(), v) + } + this.indexes, err = this.getList("/indexes") + if err != nil { + return + } + for _, v := range this.indexes { + this.m.Set(v.FullCode(), v) + } + return +} + +func (this *CodesHTTP) getList(path string) (tdx.CodeModels, error) { resp, err := http.DefaultClient.Get(this.address + path) if err != nil { return nil, err @@ -52,15 +156,7 @@ func (this *CodesHTTP) getList(path string) ([]string, error) { if err != nil { return nil, err } - ls := []string(nil) + ls := tdx.CodeModels{} err = json.Unmarshal(bs, &ls) return ls, err } - -func (this *CodesHTTP) GetStocks() ([]string, error) { - return this.getList("/stocks") -} - -func (this *CodesHTTP) GetETFs() ([]string, error) { - return this.getList("/etfs") -}