@floating-live/bilibili 0.15.3 → 0.16.1

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/plugin.d.ts CHANGED
@@ -1,29 +1,42 @@
1
- import type { FloatingLive } from "floating-live";
2
- interface BilibiliLoginInfo {
3
- credentials: string;
4
- tokens: {
5
- /** 用户uid */
6
- userId: number;
7
- /** buvid */
8
- buvid: string;
9
- };
10
- userId: number;
11
- }
12
- declare module "floating-live" {
13
- interface FloatingCommandMap {
14
- "bilibili.credentials.check": (credentials: string) => Promise<BilibiliLoginInfo>;
15
- "bilibili.login.qrcode.get": () => Promise<{
16
- key: string;
17
- url: string;
18
- }>;
19
- "bilibili.login.qrcode.poll": (key: string) => Promise<{
20
- status: number;
21
- credentials?: string;
22
- }>;
23
- }
24
- }
25
- export declare class PluginBilibili {
26
- static pluginName: string;
27
- constructor(main: FloatingLive);
28
- }
29
- export default PluginBilibili;
1
+ import { AppPluginExposesMap, BasePlugin, PluginContext, UserInfo } from "floating-live";
2
+ import { BilibiliApiClientEx } from "./utils/request";
3
+ interface BilibiliLoginInfo {
4
+ credentials: string;
5
+ isLogin: boolean;
6
+ user: UserInfo | null;
7
+ }
8
+ declare module "floating-live" {
9
+ interface AppCommandMap {
10
+ /** 检测并更新登录cookie有效性 */
11
+ "bilibili.credentials.check": (credentials: string) => Promise<BilibiliLoginInfo>;
12
+ /** 设置UA */
13
+ "bilibili.client.userAgent": (credentials: string) => string;
14
+ /** 使用cookie登录 */
15
+ "bilibili.credentials.set": (credentials: string) => string;
16
+ /** 获取二维码 */
17
+ "bilibili.login.qrcode.get": () => Promise<{
18
+ key: string;
19
+ url: string;
20
+ }>;
21
+ /** 轮询二维码登录状态 */
22
+ "bilibili.login.qrcode.poll": (key: string) => Promise<{
23
+ status: number;
24
+ message?: string;
25
+ credentials?: string;
26
+ }>;
27
+ }
28
+ }
29
+ export interface PluginOptions {
30
+ userAgent: string;
31
+ }
32
+ export declare class PluginBilibili extends BasePlugin {
33
+ static pluginName: string;
34
+ apiClient: BilibiliApiClientEx;
35
+ room: AppPluginExposesMap["room"] | null;
36
+ init(ctx: PluginContext): void;
37
+ /** 设置登录凭证 */
38
+ setCredentials(cookie: string): string;
39
+ /** 设置UA */
40
+ setUserAgent(userAgent: string): string;
41
+ }
42
+ export default PluginBilibili;
package/plugin.js CHANGED
@@ -1,67 +1,163 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PluginBilibili = void 0;
4
- const room_1 = require("./room");
5
- const utils_1 = require("./utils");
6
- const parser_1 = require("./parser");
7
- const platformInfo = {
8
- name: "bilibili",
9
- membership: {
10
- id: "guard",
11
- name: "大航海",
12
- level: ["", "总督", "提督", "舰长"],
13
- },
14
- gift: {
15
- action: "投喂",
16
- },
17
- currency: {
18
- silver: {
19
- name: "银瓜子",
20
- ratio: 1,
21
- },
22
- gold: {
23
- name: "电池",
24
- ratio: 100,
25
- money: 1000,
26
- },
27
- },
28
- stats: {
29
- view: "看过",
30
- online: "在线",
31
- like: "点赞",
32
- },
33
- };
34
- class PluginBilibili {
35
- constructor(main) {
36
- main.command.register("bilibili.room.create", (id, options) => {
37
- return new room_1.RoomBilibili(Number(id), options);
38
- });
39
- main.command.register("bilibili.room.info", async (id) => {
40
- const rawInfo = await (0, utils_1.getInfoByRoom)(parseInt("" + id));
41
- return (0, parser_1.parseInfo)(rawInfo);
42
- });
43
- main.command.register("bilibili.credentials.check", async (credentials) => {
44
- const buvid = (await (0, utils_1.getBuvid)(credentials)) || "";
45
- const userId = await (0, utils_1.getLoginUid)(credentials);
46
- return {
47
- credentials,
48
- tokens: {
49
- userId,
50
- buvid,
51
- },
52
- userId,
53
- };
54
- });
55
- main.manifest.register("platform", "bilibili", platformInfo);
56
- main.command.register("bilibili.login.qrcode.get", () => {
57
- return (0, utils_1.generateLoginQRcode)();
58
- });
59
- main.command.register("bilibili.login.qrcode.poll", async (key) => {
60
- const [status, credentials] = await (0, utils_1.checkLoginQRcode)(key);
61
- return { status, credentials };
62
- });
63
- }
64
- }
65
- exports.PluginBilibili = PluginBilibili;
66
- PluginBilibili.pluginName = "bilibili";
67
- exports.default = PluginBilibili;
1
+ import { BasePlugin, } from "floating-live";
2
+ import { RoomBilibili } from "./room";
3
+ import { BilibiliApiClientEx, fetchLiveRoomData } from "./utils/request";
4
+ import { RequestError } from "bilibili-live-danmaku";
5
+ const platformInfo = {
6
+ name: "bilibili",
7
+ membership: {
8
+ id: "guard",
9
+ name: "大航海",
10
+ level: ["", "总督", "提督", "舰长"],
11
+ },
12
+ gift: {
13
+ action: "投喂",
14
+ },
15
+ currency: {
16
+ silver: {
17
+ name: "银瓜子",
18
+ ratio: 1,
19
+ },
20
+ gold: {
21
+ name: "电池",
22
+ ratio: 100,
23
+ money: 1000,
24
+ },
25
+ },
26
+ stats: {
27
+ view: "看过",
28
+ online: "在线",
29
+ like: "点赞",
30
+ },
31
+ };
32
+ export class PluginBilibili extends BasePlugin {
33
+ constructor() {
34
+ super(...arguments);
35
+ this.apiClient = new BilibiliApiClientEx();
36
+ this.room = null;
37
+ }
38
+ init(ctx) {
39
+ ctx.whenRegister("platform", (platform) => {
40
+ platform.register("bilibili", platformInfo, ctx.signal);
41
+ });
42
+ ctx.whenRegister("room", (room) => {
43
+ this.room = room;
44
+ return () => {
45
+ this.room = null;
46
+ };
47
+ });
48
+ ctx.registerCommand("bilibili.room.create", (e, id, options) => {
49
+ return new RoomBilibili(Number(id), {
50
+ credentials: this.apiClient.cookie,
51
+ userAgent: this.apiClient.userAgent,
52
+ fetch: this.apiClient.fetch,
53
+ ...options,
54
+ });
55
+ });
56
+ ctx.registerCommand("bilibili.room.data", async (e, id) => {
57
+ return await fetchLiveRoomData(this.apiClient, parseInt(id.toString()));
58
+ });
59
+ ctx.registerCommand("bilibili.credentials.check", async (e, credentials) => {
60
+ const apiClient = new BilibiliApiClientEx({
61
+ cookie: credentials,
62
+ userAgent: this.apiClient.userAgent,
63
+ fetch: this.apiClient.fetch,
64
+ });
65
+ try {
66
+ // 检测cookie是否需要更新
67
+ const { refresh, timestamp } = (await apiClient.xpassportCookieInfo())
68
+ .data;
69
+ if (refresh) {
70
+ const refresh_token = apiClient.cookies.get("refresh_token");
71
+ // 更新cookie
72
+ const { refresh_token: new_refresh_token } = await apiClient.refreshCookie({
73
+ timestamp,
74
+ refresh_token,
75
+ });
76
+ apiClient.cookies.set("refresh_token", new_refresh_token);
77
+ }
78
+ // 检测登录状态
79
+ const data = (await apiClient.xapiNav()).data;
80
+ return {
81
+ credentials,
82
+ isLogin: true,
83
+ user: {
84
+ id: data.mid,
85
+ name: data.uname,
86
+ avatar: data.face,
87
+ },
88
+ };
89
+ }
90
+ catch (e) {
91
+ if (e.ok && e.code == -101) {
92
+ return {
93
+ credentials,
94
+ data: {},
95
+ isLogin: false,
96
+ user: null,
97
+ };
98
+ }
99
+ else {
100
+ throw e;
101
+ }
102
+ }
103
+ });
104
+ ctx.registerCommand("bilibili.credentials.set", (e, credentials) => {
105
+ return this.setCredentials(credentials);
106
+ });
107
+ ctx.registerCommand("bilibili.client.userAgent", (e, userAgent) => {
108
+ return this.setUserAgent(userAgent);
109
+ });
110
+ ctx.registerCommand("bilibili.login.qrcode.get", async () => {
111
+ const { qrcode_key, url } = (await this.apiClient.xpassportQrcodeGenerate()).data;
112
+ return { key: qrcode_key, url };
113
+ });
114
+ ctx.registerCommand("bilibili.login.qrcode.poll", async (e, key) => {
115
+ const data = await this.apiClient.xpassportQrcodePoll({
116
+ qrcode_key: key,
117
+ });
118
+ const { code, message, refresh_token } = data.data;
119
+ if (code == 86101) {
120
+ // 未扫码
121
+ return { status: 1, message };
122
+ }
123
+ else if (code == 86090) {
124
+ // 等待确认
125
+ return { status: 2, message };
126
+ }
127
+ else if (code == 0) {
128
+ // 扫码成功
129
+ this.apiClient.cookies.set("refresh_token", refresh_token);
130
+ return { status: 0, message, credentials: this.apiClient.cookie };
131
+ }
132
+ else if (code == 86038) {
133
+ // 二维码失效
134
+ return { status: -1, message };
135
+ }
136
+ else {
137
+ throw new RequestError({ ok: true, code, message });
138
+ }
139
+ });
140
+ }
141
+ /** 设置登录凭证 */
142
+ setCredentials(cookie) {
143
+ this.apiClient.setCookie(cookie);
144
+ this.room?.getList()
145
+ ?.filter((r) => r.platform == "bilibili")
146
+ .forEach((r) => {
147
+ r.setCredentials(cookie);
148
+ });
149
+ return cookie;
150
+ }
151
+ /** 设置UA */
152
+ setUserAgent(userAgent) {
153
+ this.apiClient.userAgent = userAgent;
154
+ this.room?.getList()
155
+ ?.filter((r) => r.platform == "bilibili")
156
+ .forEach((r) => {
157
+ r.userAgent = userAgent;
158
+ });
159
+ return userAgent;
160
+ }
161
+ }
162
+ PluginBilibili.pluginName = "bilibili";
163
+ export default PluginBilibili;
package/room.d.ts CHANGED
@@ -1,74 +1,105 @@
1
- import { BilibiliRoomInfo, ConnectTokens } from "./types";
2
- import { LiveRoom } from "floating-live";
3
- import { UserInfo, RoomStatsInfo, RoomDetail } from "floating-live";
4
- import { KeepLiveWS } from "bilibili-live-ws";
5
- /** 配置项 */
6
- export interface RoomOptions {
7
- /** 生成房间后是否打开 */
8
- open?: boolean;
9
- /** 登录凭据 */
10
- credentials?: string;
11
- /** 连接凭证 */
12
- tokens?: ConnectTokens;
13
- /** 房间信息 */
14
- info?: BilibiliRoomInfo;
15
- /** 断线重连间隔(默认值为100ms) */
16
- connectInterval?: number;
17
- }
18
- export declare class RoomBilibili extends LiveRoom implements BilibiliRoomInfo {
19
- /** 平台id */
20
- readonly platform: string;
21
- /** 直播间号 */
22
- readonly id: number;
23
- /** 直播间room_id */
24
- roomId: number;
25
- /** 直播间短房号 */
26
- shortId: number;
27
- /** 房间信息 */
28
- detail: RoomDetail;
29
- stats: RoomStatsInfo;
30
- /** 房间是否可用 */
31
- available: boolean;
32
- /** 是否连接上房间 */
33
- connected: boolean;
34
- /** 主播信息 */
35
- anchor: UserInfo;
36
- /** 直播间弹幕api模块 */
37
- client: KeepLiveWS | null;
38
- /** 断线重连间隔 */
39
- connectInterval: number;
40
- /** 连接凭证 */
41
- private tokens;
42
- /** 是否处于正在打开的状态 */
43
- private opening;
44
- /** 是否处于重连状态 */
45
- private reconnecting;
46
- constructor(
47
- /** 房间id */
48
- id: number, options?: RoomOptions);
49
- /** 初始化 */
50
- protected init(options?: RoomOptions): Promise<void>;
51
- /** 获取房间信息 */
52
- getInfo(): Promise<void>;
53
- /** 请求房间信息 */
54
- fetchInfo(): Promise<BilibiliRoomInfo>;
55
- /** 更新直播间信息 */
56
- updateInfo({ roomId, shortId, status, timestamp, detail, stats, anchor, liveId, available, }: BilibiliRoomInfo): void;
57
- /** 开启直播间监听 */
58
- open(): Promise<void>;
59
- private openWS;
60
- /** 设置登录凭据 */
61
- setCredentials(credentials: string, userToken?: ConnectTokens): Promise<void>;
62
- /** 请求连接tokens */
63
- fetchTokens(credentials?: string, userToken?: ConnectTokens): Promise<ConnectTokens>;
64
- /** 设置直播连接tokens */
65
- setTokens(tokens: ConnectTokens): void;
66
- /** 重连 */
67
- reconnect(): void;
68
- private emitConnention;
69
- /** 创建直播弹幕客户端 */
70
- private initClient;
71
- /** 关闭直播间监听 */
72
- close(): void;
73
- }
74
- export default RoomBilibili;
1
+ import { BilibiliRoomData, ConnectTokens } from "./types";
2
+ import { LiveWS } from "bilibili-live-danmaku";
3
+ import { UserInfo, LiveRoomStatsInfo, LiveRoomDetailInfo, LiveRoom } from "floating-live";
4
+ /** 配置项 */
5
+ export interface RoomOptions {
6
+ /** 是否自动初始化(默认为true) */
7
+ init?: boolean;
8
+ /** 生成房间后是否立即打开 */
9
+ open?: boolean;
10
+ /** 自动重连(默认为false) */
11
+ autoReconnect?: boolean;
12
+ /** 登录凭据,若为空,则初始化时自动生成 */
13
+ credentials?: string;
14
+ /** 连接凭证,若为空,则初始化时自动生成 */
15
+ tokens?: ConnectTokens;
16
+ /** 用户代理,若为空,则使用默认值 */
17
+ userAgent?: string;
18
+ /** 预设置的房间数据,若设置,将不会自动更新直播间数据 */
19
+ data?: BilibiliRoomData;
20
+ /** 断线重连间隔(默认值为500ms) */
21
+ connectInterval?: number;
22
+ /** 连接超时阈值(默认值为45000ms) */
23
+ connectTimeout?: number;
24
+ /** 自定义fetchData */
25
+ customFetchData?: (id: number | string) => Promise<BilibiliRoomData>;
26
+ /** 自定义fetchTokens */
27
+ customFetchTokens?: (roomId: number) => Promise<ConnectTokens>;
28
+ /** 自定义fetch函数 */
29
+ fetch?: (input: string | URL, init?: RequestInit) => Promise<Response>;
30
+ }
31
+ declare const symbolAbortController: unique symbol;
32
+ interface ClientWithAbortController extends LiveWS {
33
+ [symbolAbortController]: AbortController;
34
+ }
35
+ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
36
+ #private;
37
+ /** 平台id */
38
+ readonly platform: string;
39
+ /** 直播间号 */
40
+ readonly id: number;
41
+ /** 直播间room_id */
42
+ roomId: number;
43
+ /** 直播间短房号 */
44
+ shortId: number;
45
+ /** 房间信息 */
46
+ detail: LiveRoomDetailInfo;
47
+ stats: LiveRoomStatsInfo;
48
+ /** 是否连接上房间 */
49
+ connected: boolean;
50
+ /** 主播信息 */
51
+ anchor: UserInfo;
52
+ /** 直播间弹幕api客户端 */
53
+ client: ClientWithAbortController | null;
54
+ /** 断线重连间隔 */
55
+ connectInterval: number;
56
+ /** 连接超时阈值 */
57
+ connectTimeout: number;
58
+ /** 自动重连 */
59
+ autoReconnect: boolean;
60
+ /** 是否初始化 */
61
+ whenInit: Promise<RoomBilibili>;
62
+ get userAgent(): string;
63
+ set userAgent(ua: string);
64
+ /** 是否处于重连状态 */
65
+ private reconnecting;
66
+ /** 最后一次连接时间 */
67
+ private lastConnectionTime;
68
+ /** 自定义fetchData */
69
+ customFetchData?: (id: number | string) => BilibiliRoomData;
70
+ /** 自定义fetchTokens */
71
+ customFetchTokens?: (roomId: number, credentials: string) => Promise<ConnectTokens>;
72
+ constructor(
73
+ /** 房间id */
74
+ id: number, options?: RoomOptions);
75
+ /** 初始化
76
+ * 自动获取并设置 `credentials` ,自动获取房间信息和连接 `token`
77
+ */
78
+ protected init(options?: RoomOptions): Promise<void>;
79
+ /** 更新房间信息 */
80
+ update(): Promise<BilibiliRoomData>;
81
+ /** 获取房间信息 */
82
+ fetchData(): Promise<BilibiliRoomData>;
83
+ /** 设置直播间数据 */
84
+ setData({ roomId, shortId, status, timestamp, detail, stats, anchor, liveId, available, }: BilibiliRoomData): import("floating-live").LiveRoomData;
85
+ /** 开启直播间监听 */
86
+ open(): Promise<void>;
87
+ /** 设置登录凭据 */
88
+ setCredentials(credentials: string, updateTokens?: boolean): Promise<void>;
89
+ /** 更新连接tokens */
90
+ updateTokens(): Promise<void>;
91
+ /** 请求连接tokens */
92
+ fetchTokens(): Promise<ConnectTokens>;
93
+ /** 设置直播连接tokens */
94
+ setTokens(tokens: ConnectTokens): void;
95
+ /** 无缝重连 */
96
+ reconnect(): void;
97
+ private emitConnention;
98
+ /** 打开直播弹幕客户端 */
99
+ private openWebsocketClient;
100
+ /** 创建直播弹幕客户端 */
101
+ private initClient;
102
+ /** 关闭直播间监听 */
103
+ close(): void;
104
+ }
105
+ export default RoomBilibili;