-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
175 lines (152 loc) · 3.71 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package go_bili_chat
import (
"errors"
"net/http"
"sync"
"github.com/FishZe/go-bili-chat/client"
"github.com/FishZe/go-bili-chat/handler"
log "github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
)
const DefaultClientPriority = 1 << 0
const DelayClientPriority = 1 << 1
const NoCDNClientPriority = 1 << 2
var ClientPriorityMode = DefaultClientPriority
var GetRoomFailed = errors.New("get room failed")
var RoomAlreadyExist = errors.New("room already exist")
var RoomNotExist = errors.New("room not exist")
type Handler struct {
Handler handler.Handler
rooms sync.Map
}
type LiveRoom struct {
RoomId int
Client client.Client
}
func init() {
log.SetFormatter(&easy.Formatter{
TimestampFormat: "01-02 15:04:05",
LogFormat: "[bili-live][%time%][%lvl%]: %msg% \n",
})
ChangeLogLevel(log.ErrorLevel)
SetJsonCoder(&DefaultJson{})
SetClientPriorityMode(ClientPriorityMode)
}
func ChangeLogLevel(level log.Level) {
log.SetLevel(level)
}
func SetClientPriorityMode(mode int) {
ClientPriorityMode = mode
client.ChangeSequenceMode(mode)
}
func SetHeader(header http.Header) {
client.Header = header
}
func SetHeaderUA(ua string) {
client.Header.Set("User-Agent", ua)
}
func SetBuvid(Buvid string) {
client.Buvid = Buvid
}
func SetHeaderCookie(cookie string) {
client.Header.Set("Cookie", cookie)
}
func SetUID(uid int64) {
client.UID = uid
}
func GetNewHandler() *Handler {
h := Handler{}
h.Handler.DoFunc = make(handler.CmdTable, 0)
h.Handler.CmdChan = make(chan map[string]interface{}, 1024)
h.Handler.FuncPath = make(map[*handler.Do]handler.Path)
return &h
}
func (h *Handler) AddOption(Cmd string, RoomId int, Do handler.Do) *handler.Do {
if RoomId <= 10000 && RoomId != 0 {
RealRoomId, err := client.GetRealRoomId(RoomId)
if err != nil {
log.Error(err)
return nil
} else if RealRoomId == 0 {
log.Error(GetRoomFailed)
return nil
}
log.Debug(RoomId, " is short roomid, the real roomid is: ", RealRoomId)
RoomId = RealRoomId
}
return h.Handler.AddOption(Cmd, RoomId, Do)
}
func (h *Handler) DelOption(f *handler.Do) {
h.Handler.DelOption(f)
}
func (h *Handler) ExistRoom(RoomId int) bool {
if _, ok := h.rooms.Load(RoomId); ok {
return true
}
return false
}
func (h *Handler) AddRawRoom(RawRoom client.WsAuthBody) error {
if _, ok := h.rooms.Load(RawRoom.Roomid); ok {
return RoomAlreadyExist
}
room := LiveRoom{}
room.RoomId = RawRoom.Roomid
room.Client.RoomInfo = RawRoom
room.Client.BiliChat(h.Handler.CmdChan)
h.rooms.Store(room.RoomId, room)
return nil
}
func (h *Handler) AddRoom(RoomId int) error {
if RoomId <= 10000 {
RealRoomId, err := client.GetRealRoomId(RoomId)
if err != nil {
return err
} else if RealRoomId == 0 {
return GetRoomFailed
}
log.Debug(RoomId, " is short roomid, the real roomid is: ", RealRoomId)
RoomId = RealRoomId
}
return h.AddRawRoom(client.WsAuthBody{
Roomid: RoomId,
Protover: 3,
UID: client.UID,
Buvid: client.Buvid,
Type: 2,
Platform: "web",
Key: "",
})
}
func (h *Handler) DelRoom(RoomId int) error {
if RoomId <= 10000 {
RealRoomId, err := client.GetRealRoomId(RoomId)
if err != nil {
return err
} else if RealRoomId == 0 {
return GetRoomFailed
}
log.Debug(RoomId, " is short roomid, the real roomid is: ", RealRoomId)
RoomId = RealRoomId
}
if _, ok := h.rooms.Load(RoomId); !ok {
return RoomNotExist
}
h.Handler.DelRoomOption(RoomId)
if c, ok := h.rooms.Load(RoomId); ok {
cl := c.(LiveRoom).Client
cl.Close()
h.rooms.Delete(RoomId)
}
return nil
}
func (h *Handler) CountRoom() int {
count := 0
h.rooms.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
func (h *Handler) Run() {
h.Handler.CmdHandler()
}