diff --git a/lib/db/songs.ts b/lib/db/songs.ts index 0d5a096..15a49b3 100644 --- a/lib/db/songs.ts +++ b/lib/db/songs.ts @@ -1,4 +1,5 @@ import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts"; +import { parseTimestampFromPsql } from "lib/utils/formatTimestampToPostgre.ts"; export async function getNotCollectedSongs(client: Client) { const queryResult = await client.queryObject<{ aid: number }>(` @@ -22,8 +23,23 @@ export async function aidExistsInSongs(client: Client, aid: number) { FROM songs WHERE aid = $1 ); - `, + `, [aid], ); return queryResult.rows[0].exists; } + +export async function getSongsPublihsedAt(client: Client, aid: number) { + const queryResult = await client.queryObject<{ published_at: string }>( + ` + SELECT published_at + FROM songs + WHERE aid = $1; + `, + [aid], + ); + if (queryResult.rows.length === 0) { + return null; + } + return parseTimestampFromPsql(queryResult.rows[0].published_at); +} diff --git a/lib/mq/exec/snapshotTick.ts b/lib/mq/exec/snapshotTick.ts index de7b039..f14ebfd 100644 --- a/lib/mq/exec/snapshotTick.ts +++ b/lib/mq/exec/snapshotTick.ts @@ -21,6 +21,7 @@ import { NetSchedulerError } from "lib/mq/scheduler.ts"; import { setBiliVideoStatus } from "lib/db/allData.ts"; import { truncate } from "lib/utils/truncate.ts"; import { lockManager } from "lib/mq/lockManager.ts"; +import { getSongsPublihsedAt } from "lib/db/songs.ts"; const priorityMap: { [key: string]: number } = { "milestone": 1, @@ -208,7 +209,26 @@ export const takeSnapshotForVideoWorker = async (job: Job) => { return `DONE`; } else if (type === "new") { - + const publihsedAt = await getSongsPublihsedAt(client, aid); + const timeSincePublished = stat.time - publihsedAt!; + const viewsPerHour = stat.views / timeSincePublished * HOUR; + if (timeSincePublished > 48 * HOUR) { + return `DONE` + } + if (timeSincePublished > 2 * HOUR && viewsPerHour < 10) { + return `DONE` + } + let intervalMins = 240; + if (viewsPerHour > 50) { + intervalMins = 120; + } + if (viewsPerHour > 100) { + intervalMins = 60; + } + if (viewsPerHour > 1000) { + intervalMins = 15; + } + await scheduleSnapshot(client, aid, type, Date.now() + intervalMins * MINUTE); } if (type !== "milestone") return `DONE`; const eta = await getAdjustedShortTermETA(client, aid);