mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加extend.ListenCodesHTTP,因为北交所代码是网页爬虫,爬被封ip,感觉好像用处也不大
This commit is contained in:
20
example/CodesHTTP/main.go
Normal file
20
example/CodesHTTP/main.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx/extend"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
go extend.ListenCodesHTTP(10033)
|
||||||
|
|
||||||
|
<-time.After(time.Second * 3)
|
||||||
|
c := extend.DialCodesHTTP("http://localhost:10033")
|
||||||
|
stocks, err := c.GetStocks()
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
|
for _, v := range stocks {
|
||||||
|
println(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
66
extend/codes-server.go
Normal file
66
extend/codes-server.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package extend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/injoyai/conv"
|
||||||
|
"github.com/injoyai/tdx"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ListenCodesHTTP(port int, filename ...string) error {
|
||||||
|
code, err := tdx.DialCodes(conv.Default(filepath.Join(tdx.DefaultDatabaseDir, "codes.db"), filename...))
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.RequestURI {
|
||||||
|
case "/stocks":
|
||||||
|
ls := code.GetStocks()
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(conv.Bytes(ls))
|
||||||
|
case "/etfs":
|
||||||
|
ls := code.GetETFs()
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(conv.Bytes(ls))
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func DialCodesHTTP(address string) *CodesHTTP {
|
||||||
|
return &CodesHTTP{address: address}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CodesHTTP struct {
|
||||||
|
address string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CodesHTTP) getList(path string) ([]string, error) {
|
||||||
|
resp, err := http.DefaultClient.Get(this.address + path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("http code:%d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
bs, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ls := []string(nil)
|
||||||
|
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")
|
||||||
|
}
|
||||||
@@ -67,11 +67,7 @@ func (this *PullTrade) PullYear(ctx context.Context, m *tdx.Manage, year int, co
|
|||||||
tss = append(tss, resp.List...)
|
tss = append(tss, resp.List...)
|
||||||
|
|
||||||
//转成分时K线
|
//转成分时K线
|
||||||
ks, err := resp.List.Klines1()
|
ks := resp.List.Klines()
|
||||||
if err != nil {
|
|
||||||
logs.Err(err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
kss1 = append(kss1, ks...)
|
kss1 = append(kss1, ks...)
|
||||||
kss5 = append(kss5, ks.Merge(5)...)
|
kss5 = append(kss5, ks.Merge(5)...)
|
||||||
|
|||||||
Reference in New Issue
Block a user