diff --git a/packages/backend/routes/video/metadata.ts b/packages/backend/routes/video/metadata.ts index 6419c57..47c98a1 100644 --- a/packages/backend/routes/video/metadata.ts +++ b/packages/backend/routes/video/metadata.ts @@ -1,6 +1,6 @@ import { Elysia, t } from "elysia"; import { db, videoSnapshot } from "@core/drizzle"; -import { bv2av } from "@backend/lib/bilibiliID"; +import { biliIDToAID, bv2av } from "@backend/lib/bilibiliID"; import { getVideoInfo } from "@core/net/getVideoInfo"; import { redis } from "@core/db/redis"; import { ErrorResponseSchema } from "@backend/src/schema"; @@ -46,13 +46,9 @@ export const getVideoMetadataHandler = new Elysia({ prefix: "/video" }).get( "/:id/info", async (c) => { const id = c.params.id; - let aid: number | null = null; + const aid = biliIDToAID(id); - if (id.startsWith("BV1")) { - aid = bv2av(id as `BV1${string}`); - } else if (id.startsWith("av")) { - aid = Number.parseInt(id.slice(2)); - } else { + if (!aid) { return c.status(400, { code: "MALFORMED_SLOT", message: @@ -63,7 +59,7 @@ export const getVideoMetadataHandler = new Elysia({ prefix: "/video" }).get( const cachedData = await retrieveVideoInfoFromCache(aid); if (cachedData) { - return cachedData; + return cachedData.data; } const r = await getVideoInfo(aid, "getVideoInfo"); diff --git a/packages/backend/routes/video/snapshots.ts b/packages/backend/routes/video/snapshots.ts index c788427..5f12b76 100644 --- a/packages/backend/routes/video/snapshots.ts +++ b/packages/backend/routes/video/snapshots.ts @@ -1,6 +1,6 @@ import { Elysia } from "elysia"; import { db, videoSnapshot } from "@core/drizzle"; -import { bv2av } from "@backend/lib/bilibiliID"; +import { biliIDToAID, bv2av } from "@backend/lib/bilibiliID"; import { ErrorResponseSchema } from "@backend/src/schema"; import { eq, desc } from "drizzle-orm"; import z from "zod"; @@ -10,13 +10,9 @@ export const getVideoSnapshotsHandler = new Elysia({ prefix: "/video" }).get( "/:id/snapshots", async (c) => { const id = c.params.id; - let aid: number | null = null; + const aid = biliIDToAID(id); - if (id.startsWith("BV1")) { - aid = bv2av(id as `BV1${string}`); - } else if (id.startsWith("av")) { - aid = Number.parseInt(id.slice(2)); - } else { + if (!aid) { return c.status(400, { code: "MALFORMED_SLOT", message: diff --git a/packages/crawler/mq/exec/directSnapshot.ts b/packages/crawler/mq/exec/directSnapshot.ts index f6e1f57..e626e72 100644 --- a/packages/crawler/mq/exec/directSnapshot.ts +++ b/packages/crawler/mq/exec/directSnapshot.ts @@ -1,5 +1,5 @@ import { Job } from "bullmq"; -import { insertVideoSnapshot } from "mq/task/getVideoStats"; +import { takeVideoSnapshot } from "mq/task/getVideoStats"; import { sql } from "@core/db/dbNew"; import { lockManager } from "@core/mq/lockManager"; @@ -12,6 +12,6 @@ export const directSnapshotWorker = async (job: Job): Promise => { if (!aid) { throw new Error("aid does not exists"); } - await insertVideoSnapshot(sql, aid, "snapshotMilestoneVideo"); + await takeVideoSnapshot(sql, aid, "snapshotMilestoneVideo"); await lockManager.acquireLock(`directSnapshot-${job.data.aid}`, 75); }; diff --git a/packages/crawler/mq/exec/snapshotVideo.ts b/packages/crawler/mq/exec/snapshotVideo.ts index fe32b30..d386bad 100644 --- a/packages/crawler/mq/exec/snapshotVideo.ts +++ b/packages/crawler/mq/exec/snapshotVideo.ts @@ -8,7 +8,7 @@ import { import logger from "@core/log"; import { HOUR, MINUTE, SECOND } from "@core/lib"; import { getBiliVideoStatus, setBiliVideoStatus } from "../../db/bilibili_metadata"; -import { insertVideoSnapshot } from "mq/task/getVideoStats"; +import { takeVideoSnapshot } from "mq/task/getVideoStats"; import { getSongsPublihsedAt } from "db/songs"; import { getAdjustedShortTermETA } from "mq/scheduling"; import { NetSchedulerError } from "@core/net/delegate"; @@ -45,7 +45,7 @@ export const snapshotVideoWorker = async (job: Job): Promise => { } await setSnapshotStatus(sql, id, "processing"); - const stat = await insertVideoSnapshot(sql, aid, task); + const stat = await takeVideoSnapshot(sql, aid, task); if (typeof stat === "number") { await setBiliVideoStatus(aid, stat); await setSnapshotStatus(sql, id, "bili_error"); diff --git a/packages/crawler/mq/task/getVideoStats.ts b/packages/crawler/mq/task/getVideoStats.ts index 45227d8..c6d02e0 100644 --- a/packages/crawler/mq/task/getVideoStats.ts +++ b/packages/crawler/mq/task/getVideoStats.ts @@ -1,6 +1,7 @@ import { getVideoInfo } from "@core/net/getVideoInfo"; import logger from "@core/log"; import type { Psql } from "@core/db/psql.d"; +import { insertVideoSnapshot } from "db/snapshot"; export interface SnapshotNumber { time: number; @@ -24,7 +25,7 @@ export interface SnapshotNumber { * - The native `fetch` function threw an error: with error code `FETCH_ERROR` * - The alicloud-fc threw an error: with error code `ALICLOUD_FC_ERROR` */ -export async function insertVideoSnapshot( +export async function takeVideoSnapshot( sql: Psql, aid: number, task: string @@ -42,10 +43,17 @@ export async function insertVideoSnapshot( const shares = data.stat.share; const favorites = data.stat.favorite; - await sql` - INSERT INTO video_snapshot (aid, views, danmakus, replies, likes, coins, shares, favorites, created_at) - VALUES (${aid}, ${views}, ${danmakus}, ${replies}, ${likes}, ${coins}, ${shares}, ${favorites}, ${new Date(time).toISOString()}) - `; + await insertVideoSnapshot({ + createdAt: new Date(time).toISOString(), + views, + coins, + likes, + favorites, + shares, + danmakus, + replies, + aid + }) logger.log(`Taken snapshot for video ${aid}.`, "net", "fn:insertVideoSnapshot");