dsh-shlr-bilitoolkit 0.1.9 → 0.1.10
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/README.md +1 -1
- package/dist/apis/message.d.ts +7 -0
- package/dist/apis/message.js +35 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +2 -2
- package/dist/dsh.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ const [ups, _] = await client.space.videos(574115689, 1, 10);
|
|
|
75
75
|
- 写操作:`bili_like` / `bili_follow` / `bili_favorite` / `bili_send_comment` / `bili_send_message` / `bili_triple` / `bili_dynamic_publish` / `bili_dynamic_repost` / `bili_coin` / `bili_unfavorite` / `bili_fav_folder_create` / `bili_fav_folder_delete` / `bili_dynamic_like` / `bili_dynamic_delete` / `bili_comment_send` / `bili_comment_reply` / `bili_comment_delete` / `bili_message_recall` / `bili_profile_update_sign` / `bili_profile_update`(均会真实修改账号状态,需登录)
|
|
76
76
|
- 辅助:`bili_login_status`(查登录态)/ `bili_gen_qr`(生成扫码登录二维码)/ `bili_cache_clear`(清空本地缓存)
|
|
77
77
|
- 工具读取 `BILI_COOKIE_FILE`(默认 `bili_data/bili_cookies.json`)作为登录态。
|
|
78
|
-
- **数据目录**:Cookie / 缓存 /
|
|
78
|
+
- **数据目录**:Cookie / 缓存 / 二维码 / 设备号统一放在一个目录里,默认 `bili_data/`(相对 dsh 工作目录),避免散落污染工作目录。可用 `BILI_DATA_DIR` 改目录;`BILI_COOKIE_FILE` / `BILI_CACHE_FILE` / `BILI_QR_FILE` / `BILI_DEVICE_FILE` 可精确指定单个文件。设备号(私信 `dev_id`)会落盘到 `bili_data/bili_device.json`,跨重启保持稳定,避免被 B 站当成新设备触发风控。兼容旧版:若新目录还没有 Cookie、而旧位置(`./bili_cookies.json`)有,会继续沿用旧的。
|
|
79
79
|
- **扫码登录(独立获取 Cookie)**:运行 `npm run login`,终端会打印 ASCII 二维码,用「哔哩哔哩」App 扫码并确认后,Cookie 自动写入 `bili_data/bili_cookies.json`。也可在对话里先调 `bili_login_status` 查登录态、调 `bili_gen_qr` 拿二维码链接与可直接展示的 `ascii` 二维码。写操作(点赞 / 收藏 / 投币 / 关注 / 评论 / 私信 / 三连)都依赖该 Cookie 中的 `SESSDATA` / `bili_jct`。
|
|
80
80
|
- **缓存持久化**:除 Cookie 外,客户端还会把 WBI 签名密钥(`B 站每天轮换`)、`bvid→aid` 映射这类稳定 / 带 TTL 的数据写入本地 JSON 缓存(`BILI_CACHE_FILE`,默认 `bili_data/bili_cache.json`),用临时文件 + 原子替换落盘、文件损坏会自动自愈。冷启动或跨进程时直接复用,避免每次都请求 `nav` 接口;密钥过期或失效时 `refresh()` 会重新拉取并覆盖缓存。若签名反复报 `-403/-352`,可调用 `bili_cache_clear` 强制清空缓存后重试(不影响 Cookie / 登录态)。
|
|
81
81
|
|
package/dist/apis/message.d.ts
CHANGED
|
@@ -6,6 +6,13 @@ web 端私信要求带一个 UUID 形式的 msg[dev_id];同一 client 实例
|
|
|
6
6
|
import type { BiliClient } from "../client.js";
|
|
7
7
|
import { PrivateMessage } from "../models.js";
|
|
8
8
|
export declare function generateDevId(): string;
|
|
9
|
+
/** 读取或生成一个稳定的设备号(dev_id)并落盘。
|
|
10
|
+
*
|
|
11
|
+
* B 站私信要求带设备号;同一设备应保持稳定,频繁变化易被判定为异常 / 触发风控。
|
|
12
|
+
* 传入文件路径时:存在则读取(JSON `{ devId }` 或 JSON 字符串),否则生成并写入;
|
|
13
|
+
* 读取 / 解析失败则重新生成并覆盖。不传路径时退化为内存生成(不落盘)。
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveDevId(file?: string): string;
|
|
9
16
|
export declare class MessageAPI {
|
|
10
17
|
private client;
|
|
11
18
|
constructor(client: BiliClient, devId?: string);
|
package/dist/apis/message.js
CHANGED
|
@@ -4,6 +4,8 @@ web 端私信要求带一个 UUID 形式的 msg[dev_id];同一 client 实例
|
|
|
4
4
|
避免每次都被当成新设备。dev_id 生成本次会话内稳定即可(不落盘)。
|
|
5
5
|
*/
|
|
6
6
|
import { randomInt } from "node:crypto";
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { dirname, resolve } from "node:path";
|
|
7
9
|
import * as endpoints from "../endpoints.js";
|
|
8
10
|
import { BiliApiError, CookieError } from "../errors.js";
|
|
9
11
|
import { PrivateMessage } from "../models.js";
|
|
@@ -28,6 +30,39 @@ export function generateDevId() {
|
|
|
28
30
|
}
|
|
29
31
|
return out;
|
|
30
32
|
}
|
|
33
|
+
/** 读取或生成一个稳定的设备号(dev_id)并落盘。
|
|
34
|
+
*
|
|
35
|
+
* B 站私信要求带设备号;同一设备应保持稳定,频繁变化易被判定为异常 / 触发风控。
|
|
36
|
+
* 传入文件路径时:存在则读取(JSON `{ devId }` 或 JSON 字符串),否则生成并写入;
|
|
37
|
+
* 读取 / 解析失败则重新生成并覆盖。不传路径时退化为内存生成(不落盘)。
|
|
38
|
+
*/
|
|
39
|
+
export function resolveDevId(file) {
|
|
40
|
+
if (!file)
|
|
41
|
+
return generateDevId();
|
|
42
|
+
try {
|
|
43
|
+
if (existsSync(file)) {
|
|
44
|
+
const text = readFileSync(file, "utf-8").trim();
|
|
45
|
+
if (text) {
|
|
46
|
+
const parsed = JSON.parse(text);
|
|
47
|
+
const id = typeof parsed === "string" ? parsed : parsed?.devId;
|
|
48
|
+
if (typeof id === "string" && id.trim())
|
|
49
|
+
return id.trim();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// 读取 / 解析失败 → 重新生成并覆盖
|
|
55
|
+
}
|
|
56
|
+
const id = generateDevId();
|
|
57
|
+
try {
|
|
58
|
+
mkdirSync(dirname(resolve(file)), { recursive: true });
|
|
59
|
+
writeFileSync(file, JSON.stringify({ devId: id }, null, 2), "utf-8");
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// 只读 / 无权限:忽略,仍返回内存 id
|
|
63
|
+
}
|
|
64
|
+
return id;
|
|
65
|
+
}
|
|
31
66
|
export class MessageAPI {
|
|
32
67
|
client;
|
|
33
68
|
constructor(client, devId) {
|
package/dist/client.d.ts
CHANGED
package/dist/client.js
CHANGED
|
@@ -18,7 +18,7 @@ import { VideoAPI } from "./apis/video.js";
|
|
|
18
18
|
import { SearchAPI } from "./apis/search.js";
|
|
19
19
|
import { SpaceAPI } from "./apis/space.js";
|
|
20
20
|
import { CommentAPI } from "./apis/comment.js";
|
|
21
|
-
import { MessageAPI } from "./apis/message.js";
|
|
21
|
+
import { MessageAPI, resolveDevId } from "./apis/message.js";
|
|
22
22
|
import { NotifyAPI } from "./apis/notify.js";
|
|
23
23
|
import { InteractAPI } from "./apis/interact.js";
|
|
24
24
|
import { RelationAPI } from "./apis/relation.js";
|
|
@@ -77,7 +77,7 @@ export class BiliClient {
|
|
|
77
77
|
this.search = new SearchAPI(this);
|
|
78
78
|
this.space = new SpaceAPI(this);
|
|
79
79
|
this.comment = new CommentAPI(this);
|
|
80
|
-
this.message = new MessageAPI(this);
|
|
80
|
+
this.message = new MessageAPI(this, resolveDevId(opts.deviceFile ?? "bili_device.json"));
|
|
81
81
|
this.notify = new NotifyAPI(this);
|
|
82
82
|
this.interact = new InteractAPI(this);
|
|
83
83
|
this.relation = new RelationAPI(this);
|
package/dist/dsh.js
CHANGED
|
@@ -43,6 +43,7 @@ async function getClient() {
|
|
|
43
43
|
cached = new BiliClient({
|
|
44
44
|
cookieFile: resolveCookieFile(),
|
|
45
45
|
cacheFile: process.env.BILI_CACHE_FILE ?? resolve(dataDir(), "bili_cache.json"),
|
|
46
|
+
deviceFile: process.env.BILI_DEVICE_FILE ?? resolve(dataDir(), "bili_device.json"),
|
|
46
47
|
});
|
|
47
48
|
// 没有 Cookie 文件(或为空 / 损坏)= 未登录(匿名)。
|
|
48
49
|
// 不能因此让所有工具崩掉——公开接口、以及最关键的扫码登录入口都依赖这里。
|