feat: continuous monitoring of new songs

This commit is contained in:
alikia2x (寒寒) 2025-03-24 04:53:43 +08:00
parent 42db333d1a
commit 48b1130cba
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
2 changed files with 38 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts"; 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) { export async function getNotCollectedSongs(client: Client) {
const queryResult = await client.queryObject<{ aid: number }>(` const queryResult = await client.queryObject<{ aid: number }>(`
@ -22,8 +23,23 @@ export async function aidExistsInSongs(client: Client, aid: number) {
FROM songs FROM songs
WHERE aid = $1 WHERE aid = $1
); );
`, `,
[aid], [aid],
); );
return queryResult.rows[0].exists; 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);
}

View File

@ -21,6 +21,7 @@ import { NetSchedulerError } from "lib/mq/scheduler.ts";
import { setBiliVideoStatus } from "lib/db/allData.ts"; import { setBiliVideoStatus } from "lib/db/allData.ts";
import { truncate } from "lib/utils/truncate.ts"; import { truncate } from "lib/utils/truncate.ts";
import { lockManager } from "lib/mq/lockManager.ts"; import { lockManager } from "lib/mq/lockManager.ts";
import { getSongsPublihsedAt } from "lib/db/songs.ts";
const priorityMap: { [key: string]: number } = { const priorityMap: { [key: string]: number } = {
"milestone": 1, "milestone": 1,
@ -208,7 +209,26 @@ export const takeSnapshotForVideoWorker = async (job: Job) => {
return `DONE`; return `DONE`;
} }
else if (type === "new") { 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`; if (type !== "milestone") return `DONE`;
const eta = await getAdjustedShortTermETA(client, aid); const eta = await getAdjustedShortTermETA(client, aid);