From f7150e7a677ff85472fc04a4fc08026d5c06c1d2 Mon Sep 17 00:00:00 2001 From: hanyd Date: Wed, 13 Nov 2024 18:45:29 +0800 Subject: [PATCH] stash --- src/cronjob/refreshCid.ts | 57 +++++++++++++++++++++++++++++++++++++++ src/utils/timeUtil.ts | 3 +++ 2 files changed, 60 insertions(+) create mode 100644 src/cronjob/refreshCid.ts create mode 100644 src/utils/timeUtil.ts diff --git a/src/cronjob/refreshCid.ts b/src/cronjob/refreshCid.ts new file mode 100644 index 00000000..33fd4b04 --- /dev/null +++ b/src/cronjob/refreshCid.ts @@ -0,0 +1,57 @@ +import { CronJob } from "cron"; +import { db } from "../databases/databases"; +import { DBSegment } from "../types/segments.model"; +import { Logger } from "../utils/logger"; +import { BilibiliAPI } from "../utils/bilibiliApi"; +import { sleep } from "../utils/timeUtil"; + +export const refreshCidJob = new CronJob("0 5 * * *", () => refreshCid()); + +let isRunning = false; + +async function refreshCid() { + if (isRunning) { + Logger.info("refreshCid already running, skipping"); + return; + } + + isRunning = true; + const allSegments: DBSegment[] = await db.prepare("all", `SELECT * FROM "sponsorTimes" WHERE "cid" = NULL or "cid" = ''`, []); + const videoSegmentMap = new Map(); + for (const segment of allSegments) { + if (!videoSegmentMap.has(segment.videoID)) { + videoSegmentMap.set(segment.videoID, []); + } + videoSegmentMap.get(segment.videoID)?.push(segment); + } + Logger.info(`Found ${videoSegmentMap.size} videos with missing cids`); + + for (const [videoID, segments] of videoSegmentMap) { + try { + const biliVideoDetail = await BilibiliAPI.getVideoDetailView(videoID); + if (biliVideoDetail === null || biliVideoDetail === undefined) { + Logger.error(`Failed to get video detail for ${videoID}`); + continue; + } + for (const segment of segments) { + if (biliVideoDetail.pages.length === 1 || !!biliVideoDetail.pages[0].cid) { + await db.prepare("run", `UPDATE "sponsorTimes" SET "cid" = ? WHERE "videoID" = ?`, [biliVideoDetail.pages[0].cid, videoID]); + } else { + const segmentIndex = segment.startTime / biliVideoDetail.duration; + const segmentPage = biliVideoDetail.pages[Math.floor(segmentIndex)]; + if (segmentPage !== undefined) { + segment.cid = segmentPage.cid; + } + } + await db.prepare("run", `UPDATE "sponsorTimes" SET "cid" = ? WHERE "UUID" = ?`, [videoSegments, segments]); + } + } catch (e) { + Logger.error(`Failed to get video detail for ${videoID}`); + continue; + } + + await sleep(1000); + } + + isRunning = false; +} diff --git a/src/utils/timeUtil.ts b/src/utils/timeUtil.ts new file mode 100644 index 00000000..dffb47d6 --- /dev/null +++ b/src/utils/timeUtil.ts @@ -0,0 +1,3 @@ +export function sleep(time: number) { + return new Promise((resolve) => setTimeout(resolve, time)); +}