-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (134 loc) · 2.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/urfave/cli/v2"
)
const (
BINANCE_STREAM_URL = "wss://stream.binance.com:9443/ws"
)
type Limiter chan struct{}
func NewLimiter() Limiter {
return make(Limiter, 5)
}
func (l Limiter) Get() {
l <- struct{}{}
go func() {
time.Sleep(time.Second * 1)
<-l
}()
}
func run(ctx *cli.Context) error {
streams := ctx.StringSlice("stream")
rfc3339 := ctx.Bool("rfc3339-timestamp")
if ctx.Path("stream-file") != "" {
file, err := os.OpenFile(ctx.Path("stream-file"), os.O_RDONLY, 0)
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadBytes('\n')
if err == io.EOF {
break
} else if err != nil {
return err
}
streams = append(streams, strings.TrimSuffix(string(line), "\n"))
}
}
c, _, err := websocket.DefaultDialer.Dial(BINANCE_STREAM_URL, nil)
if err != nil {
return err
}
defer c.Close()
done := make(chan struct{})
go func() {
defer close(done)
for {
messageType, message, err := c.ReadMessage()
if err != nil {
return
}
var data map[string]interface{}
err = json.Unmarshal(message, &data)
if err != nil {
panic(err)
}
if _, ok := data["result"]; ok {
continue
}
if messageType != websocket.TextMessage {
continue
}
if rfc3339 {
data["E"] = time.Unix(0, int64(data["E"].(float64))*int64(time.Millisecond))
data["T"] = time.Unix(0, int64(data["T"].(float64))*int64(time.Millisecond))
message, err = json.Marshal(data)
if err != nil {
panic(err)
}
}
os.Stdout.Write(append(message, '\r', '\n'))
}
}()
go func() {
limiter := NewLimiter()
ticker := time.NewTicker(time.Second * 60)
for i := 0; i < len(streams); i += 32 {
end := i + 32
if end > len(streams) {
end = len(streams)
}
limiter.Get()
c.WriteJSON(map[string]interface{}{
"method": "SUBSCRIBE",
"params": streams[i:end],
"id": 1,
})
}
for {
select {
case <-done:
return
case <-ticker.C:
limiter.Get()
c.WriteMessage(websocket.PingMessage, nil)
}
}
}()
<-done
return nil
}
func main() {
app := &cli.App{
Name: "binance-trade-tail",
Usage: "tail JSON crypto trade data from the binance streaming api",
Action: run,
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "stream",
Usage: "provide the name of a stream to subscribe too",
},
&cli.PathFlag{
Name: "stream-file",
Usage: "a newline-delimitated file of streams that will be subscribed too",
},
&cli.BoolFlag{
Name: "rfc3339-timestamp",
Usage: "rewrite timestamps in RFC 3339 format",
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf("error: %v\n", err)
}
}