micloud-cli 0.0.0-stage → 0.5.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/LICENSE +661 -0
- package/README.en.md +118 -0
- package/README.md +117 -2
- package/dist/cli.js +59 -0
- package/dist/commands/account.js +76 -0
- package/dist/core/account.js +22 -0
- package/dist/core/auth.js +338 -0
- package/dist/core/config.js +75 -0
- package/dist/core/http.js +172 -0
- package/dist/core/lock.js +90 -0
- package/dist/core/output.js +137 -0
- package/dist/core/template.js +39 -0
- package/dist/core/utils.js +205 -0
- package/dist/note/client.js +298 -0
- package/dist/note/commands/create.js +44 -0
- package/dist/note/commands/delete.js +32 -0
- package/dist/note/commands/export.js +20 -0
- package/dist/note/commands/folder.js +101 -0
- package/dist/note/commands/get.js +35 -0
- package/dist/note/commands/list.js +57 -0
- package/dist/note/commands/move.js +37 -0
- package/dist/note/commands/pin.js +41 -0
- package/dist/note/commands/search.js +45 -0
- package/dist/note/commands/shared.js +37 -0
- package/dist/note/commands/sync.js +203 -0
- package/dist/note/commands/update.js +61 -0
- package/dist/note/commands/upload-image.js +27 -0
- package/dist/note/config.js +157 -0
- package/dist/note/converter.js +664 -0
- package/dist/note/extra-info.js +23 -0
- package/dist/note/images.js +100 -0
- package/dist/note/register.js +172 -0
- package/dist/note/sync-diff.js +143 -0
- package/dist/note/sync.js +744 -0
- package/dist/note/text-diff.js +71 -0
- package/dist/note/types.js +2 -0
- package/dist/recording/client.js +115 -0
- package/dist/recording/commands/config.js +73 -0
- package/dist/recording/commands/delete.js +171 -0
- package/dist/recording/commands/list.js +161 -0
- package/dist/recording/commands/shared.js +47 -0
- package/dist/recording/commands/transfer.js +192 -0
- package/dist/recording/commands/trash.js +140 -0
- package/dist/recording/config.js +104 -0
- package/dist/recording/dates.js +61 -0
- package/dist/recording/download.js +308 -0
- package/dist/recording/progress.js +77 -0
- package/dist/recording/pull.js +433 -0
- package/dist/recording/recording.js +294 -0
- package/dist/recording/register.js +202 -0
- package/dist/recording/template.js +44 -0
- package/dist/recording/types.js +2 -0
- package/package.json +53 -4
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/** 类型码 → 类型(与网页端分组一致;未知类型码网页端归入录音机) */
|
|
2
|
+
const TYPE_BY_CODE = {
|
|
3
|
+
0: "recorder",
|
|
4
|
+
1: "phonecall",
|
|
5
|
+
2: "fm",
|
|
6
|
+
3: "app",
|
|
7
|
+
};
|
|
8
|
+
export const ALL_TYPES = ["recorder", "phonecall", "fm", "app"];
|
|
9
|
+
export const TYPE_NAMES = {
|
|
10
|
+
recorder: "录音机录音",
|
|
11
|
+
phonecall: "通话录音",
|
|
12
|
+
fm: "FM录音",
|
|
13
|
+
app: "应用录音",
|
|
14
|
+
};
|
|
15
|
+
/** 名字里没有时长段时按码率(bit/s)估算时长,沿用网页端的取值 */
|
|
16
|
+
const BITRATE_BY_TYPE_EXT = {
|
|
17
|
+
"1_mp3": 8000,
|
|
18
|
+
"0_mp3": 64000,
|
|
19
|
+
"0_amr": 24000,
|
|
20
|
+
"0_3gpp": 160000,
|
|
21
|
+
"2_mp3": 32000,
|
|
22
|
+
};
|
|
23
|
+
const DEFAULT_BITRATE = 64000;
|
|
24
|
+
/** extraInfo.appType → 来源应用 */
|
|
25
|
+
const APP_TYPE_NAMES = { 1: "微信", 2: "企业微信" };
|
|
26
|
+
const DAY = 86_400_000;
|
|
27
|
+
/** 早于此时间的时间戳视为无效(2000-01-01) */
|
|
28
|
+
const MIN_VALID_TIME = 946_684_800_000;
|
|
29
|
+
/**
|
|
30
|
+
* 拆解云端 name:`<标题>.<扩展名>[_<时长秒>]_<类型码>_<文件时间>_<上传时间>`。
|
|
31
|
+
* 时长段只在扩展名之后出现;不符合格式时整串视为标题。
|
|
32
|
+
*/
|
|
33
|
+
export function parseRawName(raw) {
|
|
34
|
+
const full = raw.match(/^([\s\S]*)\.([A-Za-z0-9]{1,10})_(\d+)_(-?\d+)_(\d+)_(\d+)$/);
|
|
35
|
+
if (full) {
|
|
36
|
+
return {
|
|
37
|
+
base: full[1],
|
|
38
|
+
extension: full[2],
|
|
39
|
+
duration: Number(full[3]),
|
|
40
|
+
typeCode: Number(full[4]),
|
|
41
|
+
fileTime: validTime(Number(full[5])),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const noDuration = raw.match(/^([\s\S]*)\.([A-Za-z0-9]{1,10})_(-?\d+)_(\d+)_(\d+)$/);
|
|
45
|
+
if (noDuration) {
|
|
46
|
+
return {
|
|
47
|
+
base: noDuration[1],
|
|
48
|
+
extension: noDuration[2],
|
|
49
|
+
duration: null,
|
|
50
|
+
typeCode: Number(noDuration[3]),
|
|
51
|
+
fileTime: validTime(Number(noDuration[4])),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const plain = raw.match(/^([\s\S]+)\.([A-Za-z0-9]{1,10})$/);
|
|
55
|
+
if (plain) {
|
|
56
|
+
return { base: plain[1], extension: plain[2], duration: null, typeCode: -1, fileTime: null };
|
|
57
|
+
}
|
|
58
|
+
return { base: raw, extension: "", duration: null, typeCode: -1, fileTime: null };
|
|
59
|
+
}
|
|
60
|
+
/** 把云端原始条目解析为领域模型 */
|
|
61
|
+
export function parseRecording(raw) {
|
|
62
|
+
const id = String(raw.id);
|
|
63
|
+
const size = Number(raw.size) || 0;
|
|
64
|
+
const createTime = Number(raw.create_time) || 0;
|
|
65
|
+
const parts = parseRawName(raw.name ?? "");
|
|
66
|
+
const typeCode = Number.isFinite(parts.typeCode) ? parts.typeCode : -1;
|
|
67
|
+
const type = TYPE_BY_CODE[typeCode] ?? "recorder";
|
|
68
|
+
let duration = parts.duration;
|
|
69
|
+
const durationEstimated = duration === null;
|
|
70
|
+
if (duration === null) {
|
|
71
|
+
const bitrate = BITRATE_BY_TYPE_EXT[`${typeCode}_${parts.extension.toLowerCase()}`] ?? DEFAULT_BITRATE;
|
|
72
|
+
duration = (8 * size) / bitrate;
|
|
73
|
+
}
|
|
74
|
+
duration = Math.max(1, Math.round(duration));
|
|
75
|
+
const title = parts.base.trim() || id;
|
|
76
|
+
const extraInfo = parseExtraInfo(raw.extraInfo);
|
|
77
|
+
const peer = parsePeer(parts.base, type);
|
|
78
|
+
const appType = typeof extraInfo?.appType === "number" ? extraInfo.appType : undefined;
|
|
79
|
+
const appName = peer.appName ?? (type === "app" && appType ? APP_TYPE_NAMES[appType] : undefined);
|
|
80
|
+
const endedAt = parts.fileTime ?? createTime;
|
|
81
|
+
const recording = {
|
|
82
|
+
id,
|
|
83
|
+
title,
|
|
84
|
+
fileName: parts.extension ? `${title}.${parts.extension}` : title,
|
|
85
|
+
extension: parts.extension,
|
|
86
|
+
type,
|
|
87
|
+
typeName: TYPE_NAMES[type],
|
|
88
|
+
typeCode,
|
|
89
|
+
duration,
|
|
90
|
+
durationEstimated,
|
|
91
|
+
recordedAt: resolveRecordedAt({
|
|
92
|
+
base: parts.base,
|
|
93
|
+
duration,
|
|
94
|
+
durationEstimated,
|
|
95
|
+
fileTime: parts.fileTime,
|
|
96
|
+
fallback: createTime,
|
|
97
|
+
}),
|
|
98
|
+
endedAt,
|
|
99
|
+
size,
|
|
100
|
+
sha1: String(raw.sha1 ?? "").toLowerCase(),
|
|
101
|
+
createTime,
|
|
102
|
+
modifyTime: Number(raw.modify_time) || createTime,
|
|
103
|
+
ver: String(raw.ver ?? ""),
|
|
104
|
+
rawName: raw.name ?? "",
|
|
105
|
+
};
|
|
106
|
+
if (peer.phoneNumber)
|
|
107
|
+
recording.phoneNumber = peer.phoneNumber;
|
|
108
|
+
if (peer.contactName)
|
|
109
|
+
recording.contactName = peer.contactName;
|
|
110
|
+
if (appName)
|
|
111
|
+
recording.appName = appName;
|
|
112
|
+
if (extraInfo)
|
|
113
|
+
recording.extraInfo = extraInfo;
|
|
114
|
+
return recording;
|
|
115
|
+
}
|
|
116
|
+
/** 解析回收站条目:删除时间取 modify_time;给定保留天数时推算自动清除时间 */
|
|
117
|
+
export function parseTrashedRecording(raw, retentionDays) {
|
|
118
|
+
const recording = parseRecording(raw);
|
|
119
|
+
const deletedAt = Number(raw.modify_time) || recording.modifyTime;
|
|
120
|
+
const trashed = { ...recording, deletedAt };
|
|
121
|
+
if (retentionDays)
|
|
122
|
+
trashed.expiresAt = deletedAt + retentionDays * DAY;
|
|
123
|
+
return trashed;
|
|
124
|
+
}
|
|
125
|
+
/** 列表的人类可读类型名:应用录音优先显示来源应用 */
|
|
126
|
+
export function shortTypeName(r) {
|
|
127
|
+
switch (r.type) {
|
|
128
|
+
case "recorder":
|
|
129
|
+
return "录音机";
|
|
130
|
+
case "phonecall":
|
|
131
|
+
return "通话";
|
|
132
|
+
case "fm":
|
|
133
|
+
return "FM";
|
|
134
|
+
case "app":
|
|
135
|
+
return r.appName ?? "应用";
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** 按录音时间排序(同一时间按 id 保证稳定) */
|
|
139
|
+
export function sortByRecordedAt(list, order = "desc") {
|
|
140
|
+
const sign = order === "asc" ? 1 : -1;
|
|
141
|
+
return [...list].sort((a, b) => sign * (a.recordedAt - b.recordedAt) || sign * a.id.localeCompare(b.id));
|
|
142
|
+
}
|
|
143
|
+
export function matchesFilter(r, filter) {
|
|
144
|
+
if (filter.types?.length && !filter.types.includes(r.type))
|
|
145
|
+
return false;
|
|
146
|
+
if (filter.since !== undefined && r.recordedAt < filter.since)
|
|
147
|
+
return false;
|
|
148
|
+
if (filter.until !== undefined && r.recordedAt > filter.until)
|
|
149
|
+
return false;
|
|
150
|
+
if (filter.match?.trim()) {
|
|
151
|
+
const haystack = [r.title, r.contactName, r.phoneNumber, r.appName, r.id]
|
|
152
|
+
.filter(Boolean)
|
|
153
|
+
.join("\n")
|
|
154
|
+
.toLowerCase();
|
|
155
|
+
const keywords = filter.match.toLowerCase().split(/\s+/).filter(Boolean);
|
|
156
|
+
if (!keywords.every((k) => haystack.includes(k)))
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
/** 解析 --type:逗号分隔,取值见 ALL_TYPES */
|
|
162
|
+
export function parseTypes(raw) {
|
|
163
|
+
const types = raw
|
|
164
|
+
.split(",")
|
|
165
|
+
.map((s) => s.trim().toLowerCase())
|
|
166
|
+
.filter(Boolean);
|
|
167
|
+
for (const t of types) {
|
|
168
|
+
if (!ALL_TYPES.includes(t)) {
|
|
169
|
+
throw new Error(`未知的录音类型:${t}(可选:${ALL_TYPES.join(" / ")})`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return types;
|
|
173
|
+
}
|
|
174
|
+
// ============ 内部 ============
|
|
175
|
+
function validTime(ts) {
|
|
176
|
+
return Number.isFinite(ts) && ts > MIN_VALID_TIME ? ts : null;
|
|
177
|
+
}
|
|
178
|
+
function parseExtraInfo(raw) {
|
|
179
|
+
if (!raw || !raw.trim())
|
|
180
|
+
return undefined;
|
|
181
|
+
try {
|
|
182
|
+
const parsed = JSON.parse(raw);
|
|
183
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
184
|
+
? parsed
|
|
185
|
+
: undefined;
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* 从标题解析通话对方 / 来源应用(格式取自网页端):
|
|
193
|
+
* - 通话录音:`[xx@]联系人(号码)_yyyyMMddHHmmss` 或 `[xx@]号码_yyyyMMddHHmmss`
|
|
194
|
+
* - 应用录音:`微信录音 对方昵称_yyyyMMddHHmmss`、`企业微信录音_yyyyMMddHHmmss`
|
|
195
|
+
*/
|
|
196
|
+
function parsePeer(base, type) {
|
|
197
|
+
if (type !== "phonecall" && type !== "app")
|
|
198
|
+
return {};
|
|
199
|
+
const at = base.indexOf("@");
|
|
200
|
+
const text = at > -1 ? base.slice(at + 1) : base;
|
|
201
|
+
const m = text.match(/^([\s\S]*?)_\d{14}$/);
|
|
202
|
+
if (!m)
|
|
203
|
+
return {};
|
|
204
|
+
const label = m[1].trim();
|
|
205
|
+
if (!label)
|
|
206
|
+
return {};
|
|
207
|
+
if (type === "phonecall") {
|
|
208
|
+
const paren = label.lastIndexOf("(");
|
|
209
|
+
if (paren > 0 && label.endsWith(")")) {
|
|
210
|
+
return {
|
|
211
|
+
contactName: label.slice(0, paren).trim() || undefined,
|
|
212
|
+
phoneNumber: label.slice(paren + 1, -1).trim() || undefined,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
return { phoneNumber: label };
|
|
216
|
+
}
|
|
217
|
+
const app = label.match(/^(\S+?)录音(?:\s+([\s\S]+))?$/);
|
|
218
|
+
if (!app)
|
|
219
|
+
return {};
|
|
220
|
+
return { appName: app[1], contactName: app[2]?.trim() || undefined };
|
|
221
|
+
}
|
|
222
|
+
/** 标题末尾的 14 位时间戳(yyyyMMddHHmmss,本地时间),是通话 / 应用 / FM 录音的精确开始时间 */
|
|
223
|
+
function parseStamp(base) {
|
|
224
|
+
const m = base.match(/_(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/);
|
|
225
|
+
if (!m)
|
|
226
|
+
return null;
|
|
227
|
+
const [y, mo, d, h, mi, s] = m.slice(1).map(Number);
|
|
228
|
+
return buildLocalTime(y, mo, d, h, mi, s);
|
|
229
|
+
}
|
|
230
|
+
const CN_TIME = /(?:(\d{4})年)?(\d{1,2})月(\d{1,2})日\s*(凌晨|早上|上午|中午|下午|晚上)?\s*(\d{1,2})点(\d{1,2})分/;
|
|
231
|
+
/**
|
|
232
|
+
* 录音机默认标题里的中文时间,如 `2026年07月02日 16点04分`、`9月28日 14点01分`、`2025年06月27日 下午11点06分`。
|
|
233
|
+
* 缺年份时取参考时间所在年;落在参考时间一天之后则视为上一年。
|
|
234
|
+
*/
|
|
235
|
+
export function parseChineseTime(text, reference) {
|
|
236
|
+
const m = text.match(CN_TIME);
|
|
237
|
+
if (!m)
|
|
238
|
+
return null;
|
|
239
|
+
const month = Number(m[2]);
|
|
240
|
+
const day = Number(m[3]);
|
|
241
|
+
const period = m[4];
|
|
242
|
+
let hour = Number(m[5]);
|
|
243
|
+
const minute = Number(m[6]);
|
|
244
|
+
if ((period === "下午" || period === "晚上") && hour < 12)
|
|
245
|
+
hour += 12;
|
|
246
|
+
else if (period === "中午" && hour < 11)
|
|
247
|
+
hour += 12;
|
|
248
|
+
else if ((period === "凌晨" || period === "早上" || period === "上午") && hour === 12)
|
|
249
|
+
hour = 0;
|
|
250
|
+
if (m[1])
|
|
251
|
+
return buildLocalTime(Number(m[1]), month, day, hour, minute, 0);
|
|
252
|
+
const refYear = new Date(reference).getFullYear();
|
|
253
|
+
const sameYear = buildLocalTime(refYear, month, day, hour, minute, 0);
|
|
254
|
+
if (sameYear !== null && sameYear > reference + DAY) {
|
|
255
|
+
return buildLocalTime(refYear - 1, month, day, hour, minute, 0);
|
|
256
|
+
}
|
|
257
|
+
return sameYear;
|
|
258
|
+
}
|
|
259
|
+
function buildLocalTime(y, mo, d, h, mi, s) {
|
|
260
|
+
if (mo < 1 || mo > 12 || d < 1 || d > 31 || h > 23 || mi > 59 || s > 59)
|
|
261
|
+
return null;
|
|
262
|
+
const date = new Date(y, mo - 1, d, h, mi, s);
|
|
263
|
+
if (date.getMonth() !== mo - 1 || date.getDate() !== d)
|
|
264
|
+
return null;
|
|
265
|
+
return date.getTime();
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 推算录音开始时间,按可靠度依次取:
|
|
269
|
+
* 1. 标题末尾的 14 位时间戳(精确到秒);
|
|
270
|
+
* 2. 录音机默认标题里的中文时间——能覆盖暂停过的录音(文件时间减时长会偏晚);
|
|
271
|
+
* 与「文件时间 - 时长」落在同一分钟时取后者以保留秒;落在合理区间外(如异地时区录制)则不采用;
|
|
272
|
+
* 3. 文件时间 - 时长;
|
|
273
|
+
* 4. 云端创建时间。
|
|
274
|
+
*/
|
|
275
|
+
function resolveRecordedAt(input) {
|
|
276
|
+
const { base, duration, durationEstimated, fileTime, fallback } = input;
|
|
277
|
+
const stamp = parseStamp(base);
|
|
278
|
+
if (stamp !== null)
|
|
279
|
+
return stamp;
|
|
280
|
+
const computed = fileTime !== null ? fileTime - duration * 1000 : null;
|
|
281
|
+
const end = fileTime ?? fallback;
|
|
282
|
+
const titled = end ? parseChineseTime(base, end) : null;
|
|
283
|
+
if (titled !== null) {
|
|
284
|
+
const latestStart = end - (durationEstimated ? 0 : duration * 1000) + 60_000;
|
|
285
|
+
const earliestStart = fileTime !== null ? fileTime - 2 * DAY : -Infinity;
|
|
286
|
+
if (titled <= latestStart && titled >= earliestStart) {
|
|
287
|
+
if (computed !== null && !durationEstimated && computed >= titled && computed < titled + 60_000) {
|
|
288
|
+
return computed;
|
|
289
|
+
}
|
|
290
|
+
return titled;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return computed ?? fallback;
|
|
294
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { listCommand, searchCommand, infoCommand, statsCommand } from "./commands/list.js";
|
|
2
|
+
import { pullCommand, downloadCommand, urlCommand, playCommand } from "./commands/transfer.js";
|
|
3
|
+
import { deleteCommand } from "./commands/delete.js";
|
|
4
|
+
import { trashListCommand, trashRestoreCommand, trashPurgeCommand } from "./commands/trash.js";
|
|
5
|
+
import { configShowCommand, configSetCommand, configUnsetCommand } from "./commands/config.js";
|
|
6
|
+
const filterHelp = `
|
|
7
|
+
筛选(可组合):
|
|
8
|
+
-t, --type recorder,phonecall 按类型
|
|
9
|
+
--since 2026-07-01 / 7d 录音时间下限
|
|
10
|
+
--until 2026-07-31 录音时间上限(只写日期时包含当天)
|
|
11
|
+
-q, --query "关键词" 匹配标题、联系人、号码、来源应用(空格分隔、全部命中)
|
|
12
|
+
`;
|
|
13
|
+
/** 注册 `micloud recording`(别名 `rec`)命令组 */
|
|
14
|
+
export function registerRecordingCommands(program) {
|
|
15
|
+
const rec = program
|
|
16
|
+
.command("recording")
|
|
17
|
+
.alias("rec")
|
|
18
|
+
.description("小米云录音:列出、下载、增量拉取到本地,删除与回收站")
|
|
19
|
+
.addHelpText("after", `
|
|
20
|
+
录音类型(--type,逗号分隔):
|
|
21
|
+
recorder 录音机 / phonecall 通话录音 / fm FM 录音 / app 应用录音(微信、企业微信等)
|
|
22
|
+
|
|
23
|
+
时间参数(--since / --until):
|
|
24
|
+
2026-07-01、2026-07-01 18:30、20260701、7d / 12h / 2w(距今)、today、yesterday。
|
|
25
|
+
--until 只写日期时包含当天。时间均指「录音时间」,本地时区。
|
|
26
|
+
|
|
27
|
+
典型流程:
|
|
28
|
+
1) micloud rec list --json # 列出录音,拿到 <id>
|
|
29
|
+
2) micloud rec pull -o ./recordings # 增量拉取到本地(已拉取过的自动跳过)
|
|
30
|
+
3) micloud rec delete --pulled -o ./recordings # 清理已拉取的云端录音(移到回收站)
|
|
31
|
+
|
|
32
|
+
删除会同步到手机:云端删除的录音会从已登录该账号的设备上一并删除。
|
|
33
|
+
删除类操作在非交互模式下必须带 -y,否则直接报错,绝不擅自删除。
|
|
34
|
+
`);
|
|
35
|
+
// ============ 读 ============
|
|
36
|
+
rec
|
|
37
|
+
.command("list")
|
|
38
|
+
.alias("ls")
|
|
39
|
+
.description("列出云端录音(默认按录音时间倒序)")
|
|
40
|
+
.option("-t, --type <types>", "按类型筛选:recorder / phonecall / fm / app,逗号分隔")
|
|
41
|
+
.option("--since <time>", "录音时间不早于")
|
|
42
|
+
.option("--until <time>", "录音时间不晚于")
|
|
43
|
+
.option("-q, --query <keywords>", "关键词筛选")
|
|
44
|
+
.option("-n, --limit <n>", "最多返回条数")
|
|
45
|
+
.option("-s, --sort <key>", "排序字段:time(默认)/ size / duration")
|
|
46
|
+
.option("--asc", "升序")
|
|
47
|
+
.addHelpText("after", `${filterHelp}
|
|
48
|
+
--json 时每项含 id / title / type / recordedAt(毫秒)/ duration(秒)/ size(字节)/ sha1 / extension 等字段。
|
|
49
|
+
`)
|
|
50
|
+
.action((opts) => listCommand(opts));
|
|
51
|
+
rec
|
|
52
|
+
.command("search <keywords...>")
|
|
53
|
+
.description("按关键词搜索录音(标题、联系人、号码、来源应用)")
|
|
54
|
+
.option("-t, --type <types>", "按类型筛选")
|
|
55
|
+
.option("--since <time>", "录音时间不早于")
|
|
56
|
+
.option("--until <time>", "录音时间不晚于")
|
|
57
|
+
.option("-n, --limit <n>", "最多返回条数")
|
|
58
|
+
.action((keywords, opts) => searchCommand(keywords, opts));
|
|
59
|
+
rec
|
|
60
|
+
.command("info <id>")
|
|
61
|
+
.description("查看单条录音的详细信息(也会查找回收站)")
|
|
62
|
+
.action((id) => infoCommand(id));
|
|
63
|
+
rec
|
|
64
|
+
.command("stats")
|
|
65
|
+
.description("统计录音条数、时长与占用空间")
|
|
66
|
+
.option("--by <key>", "分组:type(默认)/ month / year")
|
|
67
|
+
.option("-t, --type <types>", "按类型筛选")
|
|
68
|
+
.option("--since <time>", "录音时间不早于")
|
|
69
|
+
.option("--until <time>", "录音时间不晚于")
|
|
70
|
+
.option("-q, --query <keywords>", "关键词筛选")
|
|
71
|
+
.action((opts) => statsCommand(opts));
|
|
72
|
+
// ============ 下载 ============
|
|
73
|
+
rec
|
|
74
|
+
.command("pull")
|
|
75
|
+
.description("增量拉取云端录音到本地目录(已拉取过的自动跳过,支持断点续传与校验)")
|
|
76
|
+
.option("-o, --output <dir>", "拉取目录(缺省取配置 output;再缺省为 .micloud/recording/)")
|
|
77
|
+
.option("-t, --type <types>", "只拉取指定类型")
|
|
78
|
+
.option("--since <time>", "录音时间不早于")
|
|
79
|
+
.option("--until <time>", "录音时间不晚于")
|
|
80
|
+
.option("-q, --query <keywords>", "关键词筛选")
|
|
81
|
+
.option("-n, --limit <n>", "只拉取最新的 n 条待拉取录音")
|
|
82
|
+
.option("--template <tpl>", "文件名模板(覆盖配置),见下方说明")
|
|
83
|
+
.option("-c, --concurrency <n>", "并发下载数(1-8,默认 3)")
|
|
84
|
+
.option("-f, --force", "忽略拉取记录,重新拉取所有匹配的录音")
|
|
85
|
+
.option("--dry-run", "只预览将拉取的录音,不下载")
|
|
86
|
+
.option("--delete-cloud", "拉取并校验成功后,把云端录音移到回收站(未加 --limit 时也包括之前已拉取、本地文件仍完好的)")
|
|
87
|
+
.option("--allow-moved", "配合 --delete-cloud:之前已拉取的录音即使本地文件已转移走也删除云端")
|
|
88
|
+
.option("-y, --yes", "跳过 --delete-cloud 的确认")
|
|
89
|
+
.addHelpText("after", `${filterHelp}
|
|
90
|
+
增量规则:
|
|
91
|
+
拉取目录里的 .micloud-recording.json 记录已拉取的录音,之后的 pull 一律跳过——即使你已把本地文件
|
|
92
|
+
移走或删除(例如流转到转写项目)。要重新下载用 --force,或用 download <id>。
|
|
93
|
+
每个文件先写 .part,大小与 SHA-1 都校验通过才放到正式位置;中断后重跑会断点续传。
|
|
94
|
+
目标位置已有内容不同的同名文件时,改用 <文件名>_<id> 落盘,绝不覆盖。
|
|
95
|
+
同一拉取目录同时只能运行一个 pull / download / delete --pulled。
|
|
96
|
+
|
|
97
|
+
清理云端(--delete-cloud,只移到回收站):
|
|
98
|
+
本次拉取并校验成功的录音,以及之前已拉取、拉取目录里的文件仍完好的录音,会被移到回收站;
|
|
99
|
+
带 --limit 时只清理本次拉取的录音。每条删除前都会再核对一次本地文件。
|
|
100
|
+
之前已拉取但文件已被你转移走的录音默认不删(以免云端和本地都没有);确认已妥善保存时加 --allow-moved。
|
|
101
|
+
云端内容变化、缺少 SHA-1、或曾被本工具删除后又恢复的录音一律不删,列在 data.deleteSkipped。
|
|
102
|
+
|
|
103
|
+
文件名模板(--template 或 rec config set fileNameTemplate,不含扩展名,可用 / 分子目录):
|
|
104
|
+
\${title} 手机上的录音文件名(默认模板) \${id} 录音 id
|
|
105
|
+
\${YYYY} \${YY} \${MM} \${DD} \${HH} \${mm} \${ss} 录音开始时间(本地时区)
|
|
106
|
+
\${type} recorder / phonecall / fm / app \${typeName} 录音机录音 / 通话录音 / ...
|
|
107
|
+
\${contact} 通话或应用录音的对方(名称或号码) \${phone} 通话号码
|
|
108
|
+
\${app} 来源应用(微信 / 企业微信) \${duration} 时长(秒)
|
|
109
|
+
[...] 条件段:段内变量都非空才输出,如 \${YYYY}-\${MM}-\${DD}_\${HH}\${mm}[_\${contact}]
|
|
110
|
+
|
|
111
|
+
示例:
|
|
112
|
+
micloud rec pull -o ./recordings
|
|
113
|
+
micloud rec pull --since 7d --type recorder --dry-run
|
|
114
|
+
micloud rec pull --template '\${typeName}/\${YYYY}-\${MM}/\${title}'
|
|
115
|
+
micloud rec pull --delete-cloud -y --json # 拉取后清空云端(移到回收站)
|
|
116
|
+
`)
|
|
117
|
+
.action((opts) => pullCommand(opts));
|
|
118
|
+
rec
|
|
119
|
+
.command("download <ids...>")
|
|
120
|
+
.alias("dl")
|
|
121
|
+
.description("按 id 下载指定录音(默认存到当前目录,不记录拉取状态)")
|
|
122
|
+
.option("-o, --output <dir>", "保存目录(默认当前目录)")
|
|
123
|
+
.option("--template <tpl>", "文件名模板(覆盖配置)")
|
|
124
|
+
.option("-c, --concurrency <n>", "并发下载数(1-8,默认 3)")
|
|
125
|
+
.option("--stdout", "把单条录音的音频写到标准输出(用于管道)")
|
|
126
|
+
.addHelpText("after", `
|
|
127
|
+
示例:
|
|
128
|
+
micloud rec download 10000000000000001 10000000000000002 -o ./tmp
|
|
129
|
+
micloud rec download 10000000000000001 --stdout | ffmpeg -i - -ar 16000 -ac 1 out.wav
|
|
130
|
+
`)
|
|
131
|
+
.action((ids, opts) => downloadCommand(ids, opts));
|
|
132
|
+
rec
|
|
133
|
+
.command("url <ids...>")
|
|
134
|
+
.description("获取录音的临时直链(无需登录即可访问,有效期约数小时)")
|
|
135
|
+
.action((ids) => urlCommand(ids));
|
|
136
|
+
rec
|
|
137
|
+
.command("play <id>")
|
|
138
|
+
.description("用系统默认播放器 / 浏览器在线播放录音")
|
|
139
|
+
.action((id) => playCommand(id));
|
|
140
|
+
// ============ 删除与回收站 ============
|
|
141
|
+
rec
|
|
142
|
+
.command("delete [ids...]")
|
|
143
|
+
.alias("rm")
|
|
144
|
+
.description("把云端录音移到回收站(可用 trash restore 恢复)")
|
|
145
|
+
.option("--pulled", "删除已拉取到本地、且本地文件完好、云端内容未变的录音(读取拉取目录的状态)")
|
|
146
|
+
.option("--allow-moved", "配合 --pulled:本地文件已转移走的录音也删除(以拉取记录为准)")
|
|
147
|
+
.option("-o, --output <dir>", "配合 --pulled:拉取目录(缺省同 pull)")
|
|
148
|
+
.option("-t, --type <types>", "按类型筛选")
|
|
149
|
+
.option("--since <time>", "录音时间不早于")
|
|
150
|
+
.option("--until <time>", "录音时间不晚于")
|
|
151
|
+
.option("-q, --query <keywords>", "关键词筛选")
|
|
152
|
+
.option("--all", "删除全部云端录音")
|
|
153
|
+
.option("--dry-run", "只预览将删除的录音")
|
|
154
|
+
.option("-y, --yes", "跳过确认(非交互模式下必须)")
|
|
155
|
+
.addHelpText("after", `
|
|
156
|
+
指定目标(三选一): id 列表 / --pulled(可叠加筛选) / 筛选条件或 --all。
|
|
157
|
+
小米云会同步删除已登录该账号的设备(如手机)上的录音。删除后进入回收站,按会员等级保留 30~180 天。
|
|
158
|
+
|
|
159
|
+
--pulled 只删「拉取时校验过、云端大小与 SHA-1 未变、且拉取目录里的文件仍完好」的录音;
|
|
160
|
+
已把文件转移到别处(如转写项目)时加 --allow-moved,改以拉取记录为准。
|
|
161
|
+
曾被本工具删除后又从回收站恢复的录音视为有意保留,--pulled 不再删除(可按 id 删除)。
|
|
162
|
+
|
|
163
|
+
示例:
|
|
164
|
+
micloud rec delete 10000000000000001
|
|
165
|
+
micloud rec delete --pulled -o ./recordings --dry-run
|
|
166
|
+
micloud rec delete --pulled -o ./recordings --allow-moved -y
|
|
167
|
+
micloud rec delete --type app --until 2026-03-31 -y
|
|
168
|
+
`)
|
|
169
|
+
.action((ids, opts) => deleteCommand(ids, opts));
|
|
170
|
+
const trash = rec.command("trash").description("回收站(最近删除):list / restore / purge");
|
|
171
|
+
trash
|
|
172
|
+
.command("list", { isDefault: true })
|
|
173
|
+
.alias("ls")
|
|
174
|
+
.description("列出回收站里的录音")
|
|
175
|
+
.action(() => trashListCommand());
|
|
176
|
+
trash
|
|
177
|
+
.command("restore [ids...]")
|
|
178
|
+
.description("从回收站恢复录音")
|
|
179
|
+
.option("--all", "恢复回收站里的全部录音")
|
|
180
|
+
.action((ids, opts) => trashRestoreCommand(ids, opts));
|
|
181
|
+
trash
|
|
182
|
+
.command("purge [ids...]")
|
|
183
|
+
.description("从回收站永久删除录音(不可恢复)")
|
|
184
|
+
.option("--all", "清空回收站")
|
|
185
|
+
.option("-y, --yes", "跳过确认(非交互模式下必须)")
|
|
186
|
+
.action((ids, opts) => trashPurgeCommand(ids, opts));
|
|
187
|
+
// ============ 配置 ============
|
|
188
|
+
const config = rec.command("config").description("查看 / 修改录音配置(.micloud/config.json 的 recording 节)");
|
|
189
|
+
config
|
|
190
|
+
.command("show", { isDefault: true })
|
|
191
|
+
.description("查看生效配置与拉取状态")
|
|
192
|
+
.option("-o, --output <dir>", "查看指定拉取目录的状态(缺省同 pull)")
|
|
193
|
+
.action((_opts, cmd) => configShowCommand(cmd.optsWithGlobals()));
|
|
194
|
+
config
|
|
195
|
+
.command("set <key> <value>")
|
|
196
|
+
.description("设置配置项:output / fileNameTemplate / concurrency")
|
|
197
|
+
.action((key, value) => configSetCommand(key, value));
|
|
198
|
+
config
|
|
199
|
+
.command("unset <key>")
|
|
200
|
+
.description("移除配置项,恢复默认值")
|
|
201
|
+
.action((key) => configUnsetCommand(key));
|
|
202
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { dateVars, renderTemplate } from "../core/template.js";
|
|
2
|
+
import { formatDateTime, sanitizeFileName } from "../core/utils.js";
|
|
3
|
+
/** 缺省文件名模板:沿用手机上的录音文件名 */
|
|
4
|
+
export const DEFAULT_FILE_NAME_TEMPLATE = "${title}";
|
|
5
|
+
/** 模板可用的占位符 → 取值 */
|
|
6
|
+
export function templateVars(r) {
|
|
7
|
+
return {
|
|
8
|
+
...dateVars(r.recordedAt),
|
|
9
|
+
title: r.title,
|
|
10
|
+
id: r.id,
|
|
11
|
+
type: r.type,
|
|
12
|
+
typeName: r.typeName,
|
|
13
|
+
duration: String(r.duration),
|
|
14
|
+
contact: r.contactName ?? r.phoneNumber ?? "",
|
|
15
|
+
phone: r.phoneNumber ?? "",
|
|
16
|
+
app: r.appName ?? "",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 渲染文件名模板,返回相对拉取目录的路径(`/` 分隔、不含扩展名)。
|
|
21
|
+
*
|
|
22
|
+
* - 占位符见 templateVars,语法(含条件段 `[...]`)见 core/template.ts
|
|
23
|
+
* - `/` 分隔子目录;变量值里的斜杠不会变成目录;每段单独清理非法字符,空段与 `.` / `..` 段被丢弃,无法逃出拉取目录
|
|
24
|
+
* - 渲染为空时回退到 `<录音时间>_<id>`
|
|
25
|
+
*/
|
|
26
|
+
export function renderFileName(template, r) {
|
|
27
|
+
const rendered = renderTemplate(template, templateVars(r), (v) => v.replace(/[/\\]/g, "_"));
|
|
28
|
+
const segments = rendered
|
|
29
|
+
.split(/[/\\]/)
|
|
30
|
+
.map((seg) => sanitizeFileName(seg))
|
|
31
|
+
.filter((seg) => seg.length > 0);
|
|
32
|
+
if (segments.length > 0)
|
|
33
|
+
return segments.join("/");
|
|
34
|
+
return `${formatDateTime(r.recordedAt)}_${r.id}`;
|
|
35
|
+
}
|
|
36
|
+
/** 录音落盘的相对路径(含扩展名);suffix 加在文件名主体之后、扩展名之前(同名冲突时用 `_<id>`) */
|
|
37
|
+
export function recordingRelPath(template, r, suffix = "") {
|
|
38
|
+
const base = renderFileName(template, r) + suffix;
|
|
39
|
+
return r.extension ? `${base}.${r.extension}` : base;
|
|
40
|
+
}
|
|
41
|
+
/** 路径比较键:大小写与 Unicode 规范化不敏感(macOS / Windows 默认文件系统如此) */
|
|
42
|
+
export function pathKey(relPath) {
|
|
43
|
+
return relPath.normalize("NFC").toLowerCase();
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "micloud-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "AI-friendly CLI for Xiaomi Cloud (i.mi.com): notes (read, write, sync to Markdown) and recordings (list, download, incremental pull, cleanup), with one login.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"micloud": "dist/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"README.en.md"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18.0.0"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc",
|
|
19
|
+
"dev": "tsc --watch",
|
|
20
|
+
"start": "node dist/cli.js",
|
|
21
|
+
"prepublishOnly": "npm run build",
|
|
22
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tests",
|
|
23
|
+
"test": "node --import tsx/esm --test tests/*.test.ts tests/*/*.test.ts",
|
|
24
|
+
"test:integration": "MICLOUD_INTEGRATION=1 node --import tsx/esm --test tests/note/integration.test.ts tests/recording/integration.test.ts"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/ceynri/micloud-cli.git"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/ceynri/micloud-cli#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/ceynri/micloud-cli/issues"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"xiaomi",
|
|
36
|
+
"mi",
|
|
37
|
+
"micloud",
|
|
38
|
+
"note",
|
|
39
|
+
"recorder",
|
|
40
|
+
"recording",
|
|
41
|
+
"cli",
|
|
42
|
+
"markdown",
|
|
43
|
+
"ai"
|
|
44
|
+
],
|
|
45
|
+
"license": "AGPL-3.0-only",
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"commander": "^12.1.0",
|
|
48
|
+
"playwright": "^1.52.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^20.0.0",
|
|
52
|
+
"tsx": "^4.19.0",
|
|
53
|
+
"typescript": "^5.6.0"
|
|
54
|
+
}
|
|
55
|
+
}
|