@audiofool/moondog 0.3.0-beta.15 → 0.3.0-beta.17
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 +6 -3
- package/package.json +3 -2
- package/scripts/moondog.mjs +4 -5
- package/src/core/capability-catalog.mjs +24 -14
- package/src/core/host.mjs +30 -0
- package/src/core/library-store.mjs +33 -1
- package/src/core/listening-now.mjs +7 -2
- package/src/core/moondog-application.mjs +219 -143
- package/src/i18n/messages/de.mjs +4 -3
- package/src/i18n/messages/en.mjs +4 -3
- package/src/i18n/messages/es.mjs +4 -3
- package/src/i18n/messages/fr.mjs +4 -3
- package/src/i18n/messages/it.mjs +4 -3
- package/src/i18n/messages/ja.mjs +4 -3
- package/src/i18n/messages/ko.mjs +4 -3
- package/src/i18n/messages/pt.mjs +4 -3
- package/src/i18n/messages/zh-Hant.mjs +4 -3
- package/src/i18n/messages/zh.mjs +4 -3
- package/src/i18n/screen/es.mjs +16 -3
- package/src/i18n/screen/ja.mjs +16 -3
- package/src/i18n/screen/pt.mjs +16 -3
- package/src/i18n/screen/zh.mjs +16 -3
- package/src/importers/zip-reader.mjs +61 -0
- package/src/integrations/apple-music/music-app.mjs +3 -2
- package/src/integrations/lastfm/history.mjs +2 -1
- package/src/integrations/lyrics/lrclib.mjs +1 -1
- package/src/integrations/netease/ncm-cli-player.mjs +62 -20
- package/src/integrations/open-music-similarity/artist-radio.mjs +3 -3
- package/src/integrations/spotify/account-data-history.mjs +63 -57
- package/src/integrations/spotify/authentication.mjs +2 -22
- package/src/integrations/spotify/extended-streaming-history.mjs +48 -40
- package/src/integrations/spotify/history-archive.mjs +2 -13
- package/src/integrations/spotify/service.mjs +62 -57
- package/src/integrations/spotify/web-api-client.mjs +4 -2
- package/src/integrations/user-agent.mjs +6 -0
- package/src/integrations/web/codex-web.mjs +3 -1
- package/src/integrations/youtube-music/takeout.mjs +11 -16
- package/src/profile/history-import.mjs +3 -1
- package/src/runtime/pi/agent-runtime.mjs +138 -136
- package/src/runtime/pi/next-songs.mjs +71 -0
- package/src/runtime/pi/persistent-credential-store.mjs +2 -0
- package/src/surfaces/cli/command-completions.mjs +1 -0
- package/src/surfaces/cli/format-output.mjs +19 -7
- package/src/surfaces/cli/tui.mjs +24 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { lyricIdentity } from "../../core/lyric-profile.mjs";
|
|
2
|
+
import { MOONDOG_USER_AGENT as userAgent } from "../user-agent.mjs";
|
|
2
3
|
|
|
3
4
|
const origin = "https://lrclib.net";
|
|
4
|
-
const userAgent = "Moondog/0.1.0 (https://github.com/Audiofool934/moondog)";
|
|
5
5
|
|
|
6
6
|
export class LyricsProviderError extends Error {
|
|
7
7
|
constructor(message, retryAfterMs = 60_000) {
|
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
// original recordings, NetEase marks songs as not playable (`visible: false`), most often for
|
|
5
5
|
// copyright, and no membership changes that.
|
|
6
6
|
import { execFile } from "node:child_process";
|
|
7
|
-
import { access, constants, stat } from "node:fs/promises";
|
|
7
|
+
import { access, constants, readFile, stat } from "node:fs/promises";
|
|
8
8
|
import path from "node:path";
|
|
9
9
|
|
|
10
|
+
import { environmentPathKey } from "../../core/host.mjs";
|
|
11
|
+
|
|
10
12
|
const COMMAND_TIMEOUT_MS = 20_000;
|
|
11
13
|
// Where Homebrew and npm put ncm-cli, the node it runs on, and mpv, for a PATH that lacks them.
|
|
12
14
|
const EXTRA_FOLDERS = ["/opt/homebrew/bin", "/usr/local/bin"];
|
|
@@ -22,37 +24,59 @@ const KNOWN_SONGS = 300;
|
|
|
22
24
|
const failure = (code, message) => Object.assign(new Error(message), { code });
|
|
23
25
|
|
|
24
26
|
const folders = (value) => String(value ?? "").split(path.delimiter).filter((folder) => path.isAbsolute(folder));
|
|
25
|
-
const searchPath = (environment) => [...new Set([...folders(environment
|
|
27
|
+
const searchPath = (environment, platform) => [...new Set([...folders(environment[environmentPathKey(environment)]),
|
|
28
|
+
...(platform === "win32" ? (environment.APPDATA ? [path.join(environment.APPDATA, "npm")] : []) : EXTRA_FOLDERS)])];
|
|
29
|
+
|
|
30
|
+
async function executable(file) {
|
|
31
|
+
try {
|
|
32
|
+
if (!(await stat(file)).isFile()) return false;
|
|
33
|
+
await access(file, constants.X_OK);
|
|
34
|
+
return true;
|
|
35
|
+
} catch { return false; }
|
|
36
|
+
}
|
|
26
37
|
|
|
27
|
-
async function findIn(places) {
|
|
38
|
+
async function findIn(places, platform) {
|
|
28
39
|
for (const folder of places) {
|
|
29
|
-
const file = path.join(folder, "ncm-cli");
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
const file = path.join(folder, platform === "win32" ? "ncm-cli.exe" : "ncm-cli");
|
|
41
|
+
if (await executable(file)) return { file, args: [] };
|
|
42
|
+
if (platform !== "win32") continue;
|
|
43
|
+
// npm's Windows launcher is a .cmd file. Run its package entry with Node directly so
|
|
44
|
+
// artist names and queries never pass through cmd.exe's quoting and expansion rules.
|
|
45
|
+
for (const packageRoot of [path.join(folder, "node_modules", "@music163", "ncm-cli"), path.resolve(folder, "..", "@music163", "ncm-cli")]) {
|
|
46
|
+
try {
|
|
47
|
+
const metadata = JSON.parse(await readFile(path.join(packageRoot, "package.json"), "utf8"));
|
|
48
|
+
const bin = typeof metadata.bin === "string" ? metadata.bin : metadata.bin?.["ncm-cli"];
|
|
49
|
+
if (typeof bin !== "string") continue;
|
|
50
|
+
const entry = path.resolve(packageRoot, bin);
|
|
51
|
+
if (!(await stat(entry)).isFile()) continue;
|
|
52
|
+
for (const nodeFolder of [folder, ...places]) {
|
|
53
|
+
const node = path.join(nodeFolder, "node.exe");
|
|
54
|
+
if (await executable(node)) return { file: node, args: [entry] };
|
|
55
|
+
}
|
|
56
|
+
} catch {}
|
|
57
|
+
}
|
|
35
58
|
}
|
|
36
59
|
return null;
|
|
37
60
|
}
|
|
38
61
|
|
|
39
62
|
// Opened from Finder, the app has its login shell's PATH, which lacks the folders ~/.zshrc adds,
|
|
40
63
|
// such as nvm's for npm's global commands. There, the listener's shell as Terminal opens it says.
|
|
41
|
-
function terminalPath(environment) {
|
|
64
|
+
function terminalPath(environment, platform) {
|
|
65
|
+
if (platform === "win32") return Promise.resolve([]);
|
|
42
66
|
return new Promise((resolve) => {
|
|
43
|
-
execFile(environment.SHELL || "/bin/zsh", ["-ilc", 'printf "\\n__MOONDOG_PATH__%s\\n" "$PATH"'], { env: environment, timeout: 5_000 },
|
|
67
|
+
execFile(environment.SHELL || (platform === "darwin" ? "/bin/zsh" : "/bin/sh"), ["-ilc", 'printf "\\n__MOONDOG_PATH__%s\\n" "$PATH"'], { env: environment, timeout: 5_000 },
|
|
44
68
|
(_error, stdout) => resolve(folders(String(stdout ?? "").match(/__MOONDOG_PATH__(.+)/u)?.[1])));
|
|
45
69
|
});
|
|
46
70
|
}
|
|
47
71
|
|
|
48
72
|
/** ncm-cli and the PATH to run it with, which has the node it runs on, or null. */
|
|
49
|
-
async function findNcmCli(environment) {
|
|
50
|
-
const places = searchPath(environment);
|
|
51
|
-
const
|
|
52
|
-
if (
|
|
53
|
-
const terminal = await terminalPath(environment);
|
|
54
|
-
const found = await findIn(terminal);
|
|
55
|
-
return found ? {
|
|
73
|
+
export async function findNcmCli(environment, platform = process.platform) {
|
|
74
|
+
const places = searchPath(environment, platform);
|
|
75
|
+
const command = await findIn(places, platform);
|
|
76
|
+
if (command) return { ...command, places };
|
|
77
|
+
const terminal = await terminalPath(environment, platform);
|
|
78
|
+
const found = await findIn(terminal, platform);
|
|
79
|
+
return found ? { ...found, places: [...new Set([...terminal, ...places])] } : null;
|
|
56
80
|
}
|
|
57
81
|
|
|
58
82
|
// Runs one ncm-cli command and parses its JSON. ncm-cli reports most failures as
|
|
@@ -64,9 +88,9 @@ function cliRunner({ environment }) {
|
|
|
64
88
|
return async (args) => {
|
|
65
89
|
found ??= await findNcmCli(environment);
|
|
66
90
|
if (!found) throw failure("netease_cli_missing", "ncm-cli is not installed. Install it with: npm install -g @music163/ncm-cli");
|
|
67
|
-
const env = { ...environment,
|
|
91
|
+
const env = { ...environment, [environmentPathKey(environment)]: found.places.join(path.delimiter) };
|
|
68
92
|
const stdout = await new Promise((resolve, reject) => {
|
|
69
|
-
execFile(found.file, args, { env, timeout: COMMAND_TIMEOUT_MS, maxBuffer: 8 * 1024 * 1024 }, (error, out) =>
|
|
93
|
+
execFile(found.file, [...found.args, ...args], { env, windowsHide: true, timeout: COMMAND_TIMEOUT_MS, maxBuffer: 8 * 1024 * 1024 }, (error, out) =>
|
|
70
94
|
(error && !out ? reject(failure("netease_failed", `ncm-cli ${args[0]} failed: ${String(error.message).slice(0, 200)}`)) : resolve(String(out ?? ""))));
|
|
71
95
|
});
|
|
72
96
|
const start = stdout.indexOf("{");
|
|
@@ -305,6 +329,24 @@ export function createNeteasePlayer({
|
|
|
305
329
|
await beforePlay?.();
|
|
306
330
|
await playAt(index + 1);
|
|
307
331
|
},
|
|
332
|
+
/** One playable song for a title and artist, matched as play() matches, or null. play() takes it back by id. */
|
|
333
|
+
async find({ title, artist, intent } = {}) {
|
|
334
|
+
const wanted = { title: String(title ?? ""), artist: String(artist ?? "") };
|
|
335
|
+
if (!wanted.title.trim()) return null;
|
|
336
|
+
const result = pick(await player.search({ query: `${wanted.title} ${wanted.artist}`.trim(), limit: 10, intent }), wanted);
|
|
337
|
+
return result.song && !result.other_artist ? result.song : null;
|
|
338
|
+
},
|
|
339
|
+
// A song in Up next, counted from the one after this (1 is the next): it plays now, past the ones before it.
|
|
340
|
+
async jump({ ahead, beforePlay } = {}) {
|
|
341
|
+
if (!Number.isInteger(ahead) || ahead < 1 || index + ahead >= queue.length) throw failure("netease_failed", "That song is no longer queued.");
|
|
342
|
+
await beforePlay?.();
|
|
343
|
+
await playAt(index + ahead);
|
|
344
|
+
},
|
|
345
|
+
// A song taken out of Up next, counted the same way. The one playing stays.
|
|
346
|
+
remove({ ahead } = {}) {
|
|
347
|
+
if (!Number.isInteger(ahead) || ahead < 1 || index + ahead >= queue.length) return;
|
|
348
|
+
queue = [...queue.slice(0, index + ahead), ...queue.slice(index + ahead + 1)];
|
|
349
|
+
},
|
|
308
350
|
// Back to the start of the song, or to the one before when it has only just begun.
|
|
309
351
|
async previous({ beforePlay } = {}) {
|
|
310
352
|
const now = await state();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { uuidV5 } from "../../core/uuid-v5.mjs";
|
|
2
|
+
import { MOONDOG_USER_AGENT } from "../user-agent.mjs";
|
|
2
3
|
|
|
3
4
|
export const LISTENBRAINZ_RECORDING_TRACK_NAMESPACE =
|
|
4
5
|
"1ea3cad6-a0a2-58a5-a584-64953336f69f";
|
|
@@ -15,7 +16,6 @@ export const OPEN_MUSIC_SIMILARITY_LIMITS = Object.freeze({
|
|
|
15
16
|
|
|
16
17
|
const wikidataOrigin = "https://www.wikidata.org";
|
|
17
18
|
const listenBrainzOrigin = "https://api.listenbrainz.org";
|
|
18
|
-
const defaultUserAgent = "Moondog/0.1 (https://ai.ruc.edu.cn/)";
|
|
19
19
|
const uuidPattern =
|
|
20
20
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
|
|
21
21
|
const modes = new Set(["easy", "medium", "hard"]);
|
|
@@ -209,7 +209,7 @@ export function createWikidataArtistResolver({
|
|
|
209
209
|
fetchImpl = globalThis.fetch,
|
|
210
210
|
now = Date.now,
|
|
211
211
|
cacheTtlMs = OPEN_MUSIC_SIMILARITY_LIMITS.identityCacheTtlMs,
|
|
212
|
-
userAgent =
|
|
212
|
+
userAgent = MOONDOG_USER_AGENT,
|
|
213
213
|
} = {}) {
|
|
214
214
|
if (typeof fetchImpl !== "function") {
|
|
215
215
|
throw new TypeError("A fetch implementation is required.");
|
|
@@ -437,7 +437,7 @@ export function createListenBrainzArtistRadioClient({
|
|
|
437
437
|
fetchImpl = globalThis.fetch,
|
|
438
438
|
now = Date.now,
|
|
439
439
|
cacheTtlMs = OPEN_MUSIC_SIMILARITY_LIMITS.cacheTtlMs,
|
|
440
|
-
userAgent =
|
|
440
|
+
userAgent = MOONDOG_USER_AGENT,
|
|
441
441
|
} = {}) {
|
|
442
442
|
if (typeof fetchImpl !== "function") {
|
|
443
443
|
throw new TypeError("A fetch implementation is required.");
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
1
|
import { createHash } from "node:crypto";
|
|
3
2
|
import { createReadStream } from "node:fs";
|
|
4
3
|
import { lstat } from "node:fs/promises";
|
|
5
4
|
import path from "node:path";
|
|
6
|
-
import {
|
|
5
|
+
import { openZipArchive } from "../../importers/zip-reader.mjs";
|
|
7
6
|
|
|
8
7
|
import { isUuid, uuidV5 } from "../../core/uuid-v5.mjs";
|
|
9
8
|
import {
|
|
@@ -32,7 +31,6 @@ export const SPOTIFY_ACCOUNT_DATA_HISTORY_LIMITS = Object.freeze({
|
|
|
32
31
|
maximumRecords: 500_000,
|
|
33
32
|
});
|
|
34
33
|
|
|
35
|
-
const execFileAsync = promisify(execFile);
|
|
36
34
|
const musicMemberPattern =
|
|
37
35
|
/^Spotify Account Data\/StreamingHistory_music_([0-9]+)\.json$/u;
|
|
38
36
|
const endTimePattern =
|
|
@@ -376,14 +374,20 @@ async function sha256File(filePath) {
|
|
|
376
374
|
return hash.digest("hex");
|
|
377
375
|
}
|
|
378
376
|
|
|
379
|
-
async function
|
|
377
|
+
async function openArchive(archivePath) {
|
|
380
378
|
try {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
379
|
+
return await openZipArchive(archivePath);
|
|
380
|
+
} catch {
|
|
381
|
+
fail(
|
|
382
|
+
"The Spotify account-data ZIP could not be read safely.",
|
|
383
|
+
"spotify_account_data_archive_unreadable",
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async function zipText(archive, maximumBytes, member) {
|
|
389
|
+
try {
|
|
390
|
+
return (await archive.read(member, maximumBytes)).toString("utf8");
|
|
387
391
|
} catch {
|
|
388
392
|
fail(
|
|
389
393
|
"The Spotify account-data ZIP could not be read safely.",
|
|
@@ -468,58 +472,60 @@ export async function readSpotifyAccountDataHistoryArchive({
|
|
|
468
472
|
fail("The Spotify account-data ZIP is not a supported regular file.");
|
|
469
473
|
}
|
|
470
474
|
|
|
471
|
-
|
|
472
|
-
const
|
|
473
|
-
|
|
475
|
+
// One open archive serves the listing and every member, and is closed even when a check fails.
|
|
476
|
+
const archive = await openArchive(resolvedPath);
|
|
477
|
+
let members;
|
|
478
|
+
let profileMembers;
|
|
474
479
|
const records = [];
|
|
475
480
|
const documents = {};
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
)
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
481
|
+
try {
|
|
482
|
+
const listing = archive.names.join("\n");
|
|
483
|
+
members = selectedMusicMembers(listing);
|
|
484
|
+
profileMembers = selectedProfileMembers(listing);
|
|
485
|
+
let totalJsonBytes = 0;
|
|
486
|
+
for (const member of members) {
|
|
487
|
+
const jsonText = await zipText(archive, SPOTIFY_ACCOUNT_DATA_HISTORY_LIMITS.maximumMemberBytes, member.name);
|
|
488
|
+
totalJsonBytes += Buffer.byteLength(jsonText, "utf8");
|
|
489
|
+
if (
|
|
490
|
+
totalJsonBytes >
|
|
491
|
+
SPOTIFY_ACCOUNT_DATA_HISTORY_LIMITS.maximumTotalJsonBytes
|
|
492
|
+
) {
|
|
493
|
+
fail("Spotify music history JSON is too large to import safely.");
|
|
494
|
+
}
|
|
495
|
+
let parsed;
|
|
496
|
+
try {
|
|
497
|
+
parsed = JSON.parse(jsonText.replace(/^\uFEFF/u, ""));
|
|
498
|
+
} catch {
|
|
499
|
+
fail(`Spotify music history segment ${member.index} is invalid JSON.`);
|
|
500
|
+
}
|
|
501
|
+
if (!Array.isArray(parsed)) {
|
|
502
|
+
fail(`Spotify music history segment ${member.index} is not an array.`);
|
|
503
|
+
}
|
|
504
|
+
records.push(...parsed);
|
|
505
|
+
if (records.length > SPOTIFY_ACCOUNT_DATA_HISTORY_LIMITS.maximumRecords) {
|
|
506
|
+
fail("Spotify music history contains too many records.");
|
|
507
|
+
}
|
|
501
508
|
}
|
|
502
|
-
}
|
|
503
509
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
} catch {
|
|
521
|
-
fail(`Spotify account-profile file ${member.basename} is invalid JSON.`);
|
|
510
|
+
for (const member of profileMembers) {
|
|
511
|
+
const jsonText = await zipText(archive, SPOTIFY_ACCOUNT_DATA_HISTORY_LIMITS.maximumMemberBytes, member.name);
|
|
512
|
+
totalJsonBytes += Buffer.byteLength(jsonText, "utf8");
|
|
513
|
+
if (
|
|
514
|
+
totalJsonBytes >
|
|
515
|
+
SPOTIFY_ACCOUNT_DATA_HISTORY_LIMITS.maximumTotalJsonBytes
|
|
516
|
+
) {
|
|
517
|
+
fail("Spotify account-profile JSON is too large to import safely.");
|
|
518
|
+
}
|
|
519
|
+
try {
|
|
520
|
+
documents[member.basename] = JSON.parse(
|
|
521
|
+
jsonText.replace(/^\uFEFF/u, ""),
|
|
522
|
+
);
|
|
523
|
+
} catch {
|
|
524
|
+
fail(`Spotify account-profile file ${member.basename} is invalid JSON.`);
|
|
525
|
+
}
|
|
522
526
|
}
|
|
527
|
+
} finally {
|
|
528
|
+
archive.close();
|
|
523
529
|
}
|
|
524
530
|
|
|
525
531
|
const archiveSha256 = await sha256File(resolvedPath);
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import {
|
|
3
2
|
createHash,
|
|
4
3
|
randomBytes,
|
|
@@ -6,6 +5,8 @@ import {
|
|
|
6
5
|
} from "node:crypto";
|
|
7
6
|
import { createServer } from "node:http";
|
|
8
7
|
|
|
8
|
+
import { openInDefaultBrowser } from "../../core/host.mjs";
|
|
9
|
+
|
|
9
10
|
const SPOTIFY_AUTHORIZE_URL = "https://accounts.spotify.com/authorize";
|
|
10
11
|
const SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token";
|
|
11
12
|
const ACCESS_TOKEN_DEFAULT_LIFETIME_SECONDS = 60 * 60;
|
|
@@ -499,27 +500,6 @@ export async function refreshSpotifyAccessToken({
|
|
|
499
500
|
);
|
|
500
501
|
}
|
|
501
502
|
|
|
502
|
-
function openInDefaultBrowser(url) {
|
|
503
|
-
const [command, args] =
|
|
504
|
-
process.platform === "darwin"
|
|
505
|
-
? ["open", [url]]
|
|
506
|
-
: process.platform === "win32"
|
|
507
|
-
? ["rundll32", ["url.dll,FileProtocolHandler", url]]
|
|
508
|
-
: ["xdg-open", [url]];
|
|
509
|
-
return new Promise((resolve, reject) => {
|
|
510
|
-
const child = spawn(command, args, {
|
|
511
|
-
detached: true,
|
|
512
|
-
stdio: "ignore",
|
|
513
|
-
windowsHide: true,
|
|
514
|
-
});
|
|
515
|
-
child.once("error", reject);
|
|
516
|
-
child.once("spawn", () => {
|
|
517
|
-
child.unref();
|
|
518
|
-
resolve();
|
|
519
|
-
});
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
|
|
523
503
|
async function createCallbackListener({ redirectUri, expectedState, signal }) {
|
|
524
504
|
const redirect = normalizeRedirectUri(redirectUri);
|
|
525
505
|
let resolveCode;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
1
|
import { createHash } from "node:crypto";
|
|
3
2
|
import { createReadStream } from "node:fs";
|
|
4
3
|
import { lstat } from "node:fs/promises";
|
|
5
4
|
import path from "node:path";
|
|
6
|
-
import {
|
|
5
|
+
import { openZipArchive } from "../../importers/zip-reader.mjs";
|
|
7
6
|
|
|
8
7
|
import { isUuid, uuidV5 } from "../../core/uuid-v5.mjs";
|
|
9
8
|
import {
|
|
@@ -30,7 +29,6 @@ export const SPOTIFY_EXTENDED_HISTORY_LIMITS = Object.freeze({
|
|
|
30
29
|
maximumRecords: 500_000,
|
|
31
30
|
});
|
|
32
31
|
|
|
33
|
-
const execFileAsync = promisify(execFile);
|
|
34
32
|
const historyMemberPattern =
|
|
35
33
|
/^Spotify Extended Streaming History\/Streaming_History_(?:Audio|Video)_([0-9]{4})(?:_([0-9]+))?\.json$/u;
|
|
36
34
|
const trackUriPattern = /^spotify:track:([A-Za-z0-9]{22})$/u;
|
|
@@ -402,14 +400,20 @@ async function sha256File(filePath) {
|
|
|
402
400
|
return hash.digest("hex");
|
|
403
401
|
}
|
|
404
402
|
|
|
405
|
-
async function
|
|
403
|
+
async function openArchive(archivePath) {
|
|
406
404
|
try {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
405
|
+
return await openZipArchive(archivePath);
|
|
406
|
+
} catch {
|
|
407
|
+
fail(
|
|
408
|
+
"The Spotify Extended History ZIP could not be read safely.",
|
|
409
|
+
"spotify_extended_history_archive_unreadable",
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
async function zipText(archive, maximumBytes, member) {
|
|
415
|
+
try {
|
|
416
|
+
return (await archive.read(member, maximumBytes)).toString("utf8");
|
|
413
417
|
} catch {
|
|
414
418
|
fail(
|
|
415
419
|
"The Spotify Extended History ZIP could not be read safely.",
|
|
@@ -480,38 +484,42 @@ export async function readSpotifyExtendedStreamingHistoryArchive({
|
|
|
480
484
|
fail("The Spotify Extended History ZIP is not a supported regular file.");
|
|
481
485
|
}
|
|
482
486
|
|
|
483
|
-
|
|
484
|
-
const
|
|
487
|
+
// One open archive serves the listing and every member, and is closed even when a check fails.
|
|
488
|
+
const archive = await openArchive(resolvedPath);
|
|
489
|
+
let members;
|
|
485
490
|
const records = [];
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
491
|
+
try {
|
|
492
|
+
const listing = archive.names.join("\n");
|
|
493
|
+
members = selectedHistoryMembers(listing);
|
|
494
|
+
let totalJsonBytes = 0;
|
|
495
|
+
for (const member of members) {
|
|
496
|
+
const jsonText = await zipText(archive, SPOTIFY_EXTENDED_HISTORY_LIMITS.maximumMemberBytes, member.name);
|
|
497
|
+
totalJsonBytes += Buffer.byteLength(jsonText, "utf8");
|
|
498
|
+
if (
|
|
499
|
+
totalJsonBytes > SPOTIFY_EXTENDED_HISTORY_LIMITS.maximumTotalJsonBytes
|
|
500
|
+
) {
|
|
501
|
+
fail("Spotify Extended History JSON is too large to import safely.");
|
|
502
|
+
}
|
|
503
|
+
let parsed;
|
|
504
|
+
try {
|
|
505
|
+
parsed = JSON.parse(jsonText.replace(/^\uFEFF/u, ""));
|
|
506
|
+
} catch {
|
|
507
|
+
fail(
|
|
508
|
+
`Spotify Extended History audio/video file for ${member.year} is invalid JSON.`,
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
if (!Array.isArray(parsed)) {
|
|
512
|
+
fail(
|
|
513
|
+
`Spotify Extended History audio/video file for ${member.year} is not an array.`,
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
records.push(...parsed);
|
|
517
|
+
if (records.length > SPOTIFY_EXTENDED_HISTORY_LIMITS.maximumRecords) {
|
|
518
|
+
fail("Spotify Extended History contains too many records.");
|
|
519
|
+
}
|
|
514
520
|
}
|
|
521
|
+
} finally {
|
|
522
|
+
archive.close();
|
|
515
523
|
}
|
|
516
524
|
|
|
517
525
|
return projectSpotifyExtendedStreamingHistory({
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
1
|
import path from "node:path";
|
|
3
|
-
import {
|
|
2
|
+
import { listZipMembers } from "../../importers/zip-reader.mjs";
|
|
4
3
|
|
|
5
4
|
import { readSpotifyAccountDataHistoryArchive } from "./account-data-history.mjs";
|
|
6
5
|
import { readSpotifyExtendedStreamingHistoryArchive } from "./extended-streaming-history.mjs";
|
|
7
6
|
|
|
8
|
-
const execFileAsync = promisify(execFile);
|
|
9
|
-
|
|
10
7
|
function fail(message, code = "spotify_history_archive_invalid") {
|
|
11
8
|
const error = new Error(message);
|
|
12
9
|
error.code = code;
|
|
@@ -20,15 +17,7 @@ export async function readSpotifyHistoryArchive(options = {}) {
|
|
|
20
17
|
const archivePath = path.resolve(options.archivePath);
|
|
21
18
|
let listing;
|
|
22
19
|
try {
|
|
23
|
-
|
|
24
|
-
"/usr/bin/unzip",
|
|
25
|
-
["-Z1", archivePath],
|
|
26
|
-
{
|
|
27
|
-
encoding: "utf8",
|
|
28
|
-
maxBuffer: 1024 * 1024,
|
|
29
|
-
windowsHide: true,
|
|
30
|
-
},
|
|
31
|
-
));
|
|
20
|
+
listing = (await listZipMembers(archivePath)).join("\n");
|
|
32
21
|
} catch {
|
|
33
22
|
fail(
|
|
34
23
|
"The Spotify history ZIP could not be read safely.",
|