Skip to content

Commit 26561e7

Browse files
committed
player: 修正歌词库同步超出限制的问题
1 parent 76f91fd commit 26561e7

File tree

1 file changed

+87
-37
lines changed
  • packages/player/src/components/LocalMusicContext

1 file changed

+87
-37
lines changed

packages/player/src/components/LocalMusicContext/index.tsx

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ const LyricContext: FC = () => {
245245

246246
(async () => {
247247
const fileListRes = await fetch(
248-
"https://api.github.com/repos/Steve-xmh/amll-ttml-db/contents/raw-lyrics",
248+
"https://api.github.com/repos/Steve-xmh/amll-ttml-db/contents",
249249
{
250250
signal: sig.signal,
251251
redirect: "follow",
@@ -255,18 +255,57 @@ const LyricContext: FC = () => {
255255
if (fileListRes.status < 200 || fileListRes.status > 399) {
256256
console.warn(
257257
TTML_LOG_TAG,
258-
"TTML DB 歌词库同步失败",
258+
"TTML DB 歌词库同步失败:获取根目录文件列表失败",
259259
fileListRes.status,
260260
fileListRes.statusText,
261261
);
262262
return;
263263
}
264264

265265
const fileList = await fileListRes.json();
266-
const fileMap = Object.fromEntries(fileList.map((v) => [v.name, v]));
266+
const rawLyricsEntry = fileList.find(
267+
(v) => v.name === "raw-lyrics" && v.type === "dir",
268+
);
269+
270+
if (!rawLyricsEntry) {
271+
console.warn(TTML_LOG_TAG, "未找到 raw-lyrics 目录");
272+
return;
273+
}
274+
console.log(
275+
TTML_LOG_TAG,
276+
"raw-lyric 目录已找到,SHA 为",
277+
rawLyricsEntry.sha,
278+
);
279+
280+
const rawLyricsRes = await fetch(
281+
`https://api.github.com/repos/Steve-xmh/amll-ttml-db/git/trees/${rawLyricsEntry.sha}`,
282+
{
283+
signal: sig.signal,
284+
redirect: "follow",
285+
},
286+
);
287+
288+
if (rawLyricsRes.status < 200 || rawLyricsRes.status > 399) {
289+
console.warn(
290+
TTML_LOG_TAG,
291+
"TTML DB 歌词库同步失败:获取 raw-lyrics 文件夹下的文件列表失败",
292+
rawLyricsRes.status,
293+
rawLyricsRes.statusText,
294+
);
295+
return;
296+
}
297+
298+
const lyricFileList = await rawLyricsRes.json();
299+
300+
const fileMap = Object.fromEntries(
301+
lyricFileList.tree.map((v) => [v.path, v]),
302+
);
303+
console.log(fileMap);
267304

268305
const localFileList = new Set<string>();
269-
const remoteFileList = new Set<string>(fileList.map((v) => v.name));
306+
const remoteFileList = new Set<string>(
307+
lyricFileList.tree.map((v) => v.path),
308+
);
270309

271310
await db.ttmlDB.each((obj) => {
272311
localFileList.add(obj.name);
@@ -277,50 +316,61 @@ const LyricContext: FC = () => {
277316

278317
const shouldFetchList = remoteFileList.difference(localFileList);
279318

280-
console.log(TTML_LOG_TAG, "需要下载的歌词数量", shouldFetchList.size);
319+
console.log(
320+
TTML_LOG_TAG,
321+
"需要下载的歌词数量",
322+
shouldFetchList.size,
323+
shouldFetchList,
324+
);
281325

282326
let synced = 0;
283327
let errored = 0;
284328

285329
const fetchTasks = [];
286-
330+
287331
// Safari 目前不支持对迭代器对象使用 map 方法
288332
for (const fileName of shouldFetchList.keys()) {
289-
fetchTasks.push((async () => {
290-
const lyricRes = await fetch(fileMap[fileName].download_url, {
291-
signal: sig.signal,
292-
redirect: "follow",
293-
});
294-
295-
if (fileListRes.status < 200 || fileListRes.status > 399) {
296-
console.warn(
297-
"同步歌词文件",
298-
fileName,
299-
"失败",
300-
fileListRes.status,
301-
fileListRes.statusText,
333+
if (!(fileName in fileMap)) continue;
334+
fetchTasks.push(
335+
(async () => {
336+
const lyricRes = await fetch(
337+
`https://raw.githubusercontent.com/Steve-xmh/amll-ttml-db/main/raw-lyrics/${fileMap[fileName].path}`,
338+
{
339+
signal: sig.signal,
340+
redirect: "follow",
341+
},
302342
);
303-
errored++;
304-
return;
305-
}
306343

307-
const lyricContent = await lyricRes.text();
344+
if (fileListRes.status < 200 || fileListRes.status > 399) {
345+
console.warn(
346+
"同步歌词文件",
347+
fileName,
348+
"失败",
349+
fileListRes.status,
350+
fileListRes.statusText,
351+
);
352+
errored++;
353+
return;
354+
}
308355

309-
try {
310-
const ttml = parseTTML(lyricContent);
311-
db.ttmlDB.add({
312-
name: fileName,
313-
content: ttml,
314-
raw: lyricContent,
315-
});
316-
synced++;
317-
} catch (err) {
318-
console.warn("下载并解析歌词文件", fileName, "失败", err);
319-
errored++;
320-
}
321-
})())
356+
const lyricContent = await lyricRes.text();
357+
358+
try {
359+
const ttml = parseTTML(lyricContent);
360+
db.ttmlDB.add({
361+
name: fileName,
362+
content: ttml,
363+
raw: lyricContent,
364+
});
365+
synced++;
366+
} catch (err) {
367+
console.warn("下载并解析歌词文件", fileName, "失败", err);
368+
errored++;
369+
}
370+
})(),
371+
);
322372
}
323-
373+
324374
await Promise.all(fetchTasks);
325375

326376
console.log(

0 commit comments

Comments
 (0)