@floating-live/bilibili 0.15.3 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/room.ts DELETED
@@ -1,272 +0,0 @@
1
- import { BilibiliRoomInfo, ConnectTokens } from "./types";
2
- import { LiveRoom } from "floating-live";
3
- import { UserInfo, RoomStatsInfo, RoomDetail, RoomInfo } from "floating-live";
4
- import { KeepLiveWS } from "bilibili-live-ws";
5
- import { parseInfo, parseMessage } from "./parser";
6
- import {
7
- getBuvid,
8
- getInfoByRoom,
9
- getLoginUid,
10
- getToken,
11
- parseCookieString,
12
- } from "./utils";
13
- import { ConnectStatus } from "floating-live";
14
-
15
- /** 配置项 */
16
- export interface RoomOptions {
17
- /** 生成房间后是否打开 */
18
- open?: boolean;
19
- /** 登录凭据 */
20
- credentials?: string;
21
- /** 连接凭证 */
22
- tokens?: ConnectTokens;
23
- /** 房间信息 */
24
- info?: BilibiliRoomInfo;
25
- /** 断线重连间隔(默认值为100ms) */
26
- connectInterval?: number;
27
- }
28
-
29
- export class RoomBilibili extends LiveRoom implements BilibiliRoomInfo {
30
- /** 平台id */
31
- readonly platform: string = "bilibili";
32
- /** 直播间号 */
33
- readonly id: number;
34
- /** 直播间room_id */
35
- public roomId: number = 0;
36
- /** 直播间短房号 */
37
- public shortId: number = 0;
38
- /** 房间信息 */
39
- public detail: RoomDetail = {
40
- /** 直播标题 */
41
- title: "",
42
- /** 分区 */
43
- area: [],
44
- /** 封面url */
45
- cover: "",
46
- };
47
- public stats: RoomStatsInfo = {
48
- /** 观看数 */
49
- view: 0,
50
- /** 点赞数 */
51
- like: 0,
52
- };
53
- /** 房间是否可用 */
54
- public available: boolean = false;
55
- /** 是否连接上房间 */
56
- public connected: boolean = false;
57
- /** 主播信息 */
58
- public anchor: UserInfo = { name: "", id: 0 };
59
- /** 直播间弹幕api模块 */
60
- public client: KeepLiveWS | null = null;
61
- /** 断线重连间隔 */
62
- public connectInterval: number;
63
-
64
- /** 连接凭证 */
65
- private tokens: ConnectTokens = {
66
- userId: 0,
67
- buvid: "",
68
- token: "",
69
- };
70
-
71
- /** 是否处于正在打开的状态 */
72
- private opening: boolean = false;
73
- /** 是否处于重连状态 */
74
- private reconnecting: boolean = false;
75
-
76
- constructor(
77
- /** 房间id */
78
- id: number,
79
- options?: RoomOptions
80
- ) {
81
- super();
82
- this.id = id;
83
- this.connectInterval = options?.connectInterval || 100;
84
- this.init(options);
85
- }
86
- /** 初始化 */
87
- protected async init(options: RoomOptions = {}) {
88
- const { info, tokens, credentials, open } = options;
89
- if (open) this.opening = true;
90
- if (info) {
91
- this.updateInfo(info);
92
- } else {
93
- await this.getInfo();
94
- }
95
- if (tokens?.userId && tokens?.buvid && tokens?.token) {
96
- this.setTokens(tokens as ConnectTokens);
97
- } else {
98
- await this.setCredentials(credentials || "", tokens);
99
- }
100
- if (open) {
101
- this.opening = false;
102
- this.openWS();
103
- }
104
- this.emit("init");
105
- }
106
-
107
- /** 获取房间信息 */
108
- public async getInfo() {
109
- await this.fetchInfo()
110
- .then((info) => {
111
- this.roomId = info.id as number;
112
- this.updateInfo(info);
113
- })
114
- .catch((error) => {
115
- this.emit("info_error", error);
116
- });
117
- }
118
- /** 请求房间信息 */
119
- async fetchInfo(): Promise<BilibiliRoomInfo> {
120
- const rawInfo = await getInfoByRoom(this.id);
121
- return parseInfo(rawInfo);
122
- }
123
- /** 更新直播间信息 */
124
- updateInfo({
125
- roomId,
126
- shortId,
127
- status,
128
- timestamp,
129
- detail,
130
- stats,
131
- anchor,
132
- liveId,
133
- available,
134
- }: BilibiliRoomInfo) {
135
- this.roomId = roomId;
136
- this.shortId = shortId;
137
- this.status = status;
138
- this.timestamp = timestamp;
139
- this.detail = detail;
140
- this.stats = stats!;
141
- this.anchor = anchor;
142
- this.liveId = liveId;
143
- this.available = available;
144
- this.emit("info", this.info);
145
- }
146
-
147
- /** 开启直播间监听 */
148
- public async open() {
149
- // 如果直播间监听已打开或处于正在打开状态,则返回
150
- if (this.opened || this.opening) return;
151
- this.opening = true;
152
- await this.getInfo();
153
- this.opening = false;
154
- this.openWS();
155
- }
156
- private openWS() {
157
- // 如果直播间不可用,则返回
158
- if (!this.available) return;
159
- this.opened = true;
160
- this.initClient();
161
- this.emit("open");
162
- }
163
-
164
- /** 设置登录凭据 */
165
- async setCredentials(credentials: string, userToken?: ConnectTokens) {
166
- this.setTokens(await this.fetchTokens(credentials, userToken));
167
- }
168
- /** 请求连接tokens */
169
- async fetchTokens(
170
- credentials: string = "",
171
- userToken?: ConnectTokens
172
- ): Promise<ConnectTokens> {
173
- const cookie = parseCookieString(credentials);
174
- const buvid =
175
- userToken?.buvid ||
176
- cookie["buvid3"] ||
177
- (await getBuvid(credentials)) ||
178
- "";
179
- const userId =
180
- userToken?.userId ||
181
- parseInt(cookie["DedeUserID"]) ||
182
- (await getLoginUid(credentials));
183
- const token = await getToken(this.roomId, credentials);
184
- return {
185
- userId,
186
- buvid,
187
- token,
188
- };
189
- }
190
- /** 设置直播连接tokens */
191
- setTokens(tokens: ConnectTokens) {
192
- this.tokens = tokens;
193
- if (this.opened) {
194
- // 如果已打开,则重连
195
- this.reconnect();
196
- }
197
- }
198
- /** 重连 */
199
- reconnect() {
200
- if (this.reconnecting) return;
201
- this.reconnecting = true;
202
- const lastClient = this.client;
203
- if (!lastClient) {
204
- this.reconnecting = false;
205
- return;
206
- }
207
- this.initClient();
208
- this.client?.once("open", () => {
209
- lastClient.removeAllListeners();
210
- lastClient.close();
211
- this.reconnecting = false;
212
- });
213
- }
214
-
215
- private emitConnention(status: ConnectStatus) {
216
- this.connection = status;
217
- switch (status) {
218
- case ConnectStatus.connecting:
219
- this.emit("connecting");
220
- break;
221
- case ConnectStatus.connected:
222
- this.emit("connected");
223
- break;
224
- case ConnectStatus.entered:
225
- this.emit("enter");
226
- break;
227
- case ConnectStatus.disconnected:
228
- this.emit("disconnect");
229
- break;
230
- }
231
- }
232
- /** 创建直播弹幕客户端 */
233
- private initClient() {
234
- this.emitConnention(ConnectStatus.connecting);
235
- // 与Websocket服务器连接
236
- const client = new KeepLiveWS(this.roomId || this.id, {
237
- uid: this.tokens.userId,
238
- buvid: this.tokens.buvid,
239
- key: this.tokens.token,
240
- });
241
- client.interval = this.connectInterval;
242
-
243
- client.on("open", () => {
244
- // 连接到服务器
245
- this.opened && this.emitConnention(ConnectStatus.connected);
246
- });
247
- client.on("close", () => {
248
- // 断开连接
249
- this.opened && this.emitConnention(ConnectStatus.disconnected);
250
- });
251
- client.on("live", () => {
252
- // 连接到直播间
253
- this.emitConnention(ConnectStatus.entered);
254
- });
255
- client.on("msg", (data: any) => {
256
- const m = parseMessage(data, this);
257
- m && this.emitMessage(m);
258
- this.emitRaw(data);
259
- });
260
- this.client = client;
261
- }
262
- /** 关闭直播间监听 */
263
- close() {
264
- if (!this.opened) return;
265
- this.opened = false;
266
- this.client?.close();
267
- this.emitConnention(ConnectStatus.off);
268
- this.emit("close");
269
- }
270
- }
271
-
272
- export default RoomBilibili;
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2020",
4
- "lib": ["es2020", "DOM"],
5
- "module": "Node16",
6
- "noImplicitAny": true,
7
- "declaration": true,
8
- "moduleResolution": "Node16",
9
- "esModuleInterop": true,
10
- "strict": true,
11
- "rootDir": "."
12
- },
13
- "files": ["index.ts"]
14
- }