Skip to content

Commit

Permalink
chore: use getitems() to get recent updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovler-Young committed Jun 24, 2024
1 parent bb05561 commit 2c5b601
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/lib/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ export class BiliArchiver {
}
}

async getitems(): Promise<Array<ArchivedItem>> {
const url = new URL(`/archive`, this.endpoint);
try {
const res = await fetch(url.toString());
const items = (await res.json())?.items;
return items;
} catch (e) {
console.error(e);
return [];
}
}

async check(bv: Bvid): Promise<Option<URL>> {
try {
const url = bv.getMetadataUrl()
Expand Down
15 changes: 5 additions & 10 deletions src/lib/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,12 @@ const handle_source = async (ctx: Context, source_type: string, source_id: strin
let checked_turns = 0;

const checkArchives = async () => {
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
let newlyCompleted = [];
for (let i = 0; i < remainingBvids.length; i++) {
const bvid = remainingBvids[i];
const result = await api.check(new Bvid(bvid));
if (result.isSome()) {
newlyCompleted.push(bvid);
console.debug(`${i}/${remainingBvids.length}|Archive of ${bvid} was done, item uploaded to\n${result.unwrap().toString()}`);
}
await sleep(2000);
const progress = await api.getitems();
let allCompleted = progress.filter(item => item.status === 'finished').map(item => item.bvid);
newlyCompleted = allCompleted.filter(bvid => remainingBvids.includes(bvid));
if (newlyCompleted.length > 0) {
console.debug(`Archive of ${newlyCompleted.length} BVIDs completed: ${newlyCompleted.join(', ')}`);
}
remainingBvids = remainingBvids.filter(bvid => !newlyCompleted.includes(bvid));
completedBvids.push(...newlyCompleted);
Expand Down

0 comments on commit 2c5b601

Please sign in to comment.