Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for video cid #1

Merged
merged 36 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b500b2b
Upgrade database
hanydd Oct 30, 2024
75793d1
Update dao layer
hanydd Oct 31, 2024
3697a3d
Remove webhook add cid
hanydd Oct 31, 2024
be63a43
save cid
hanydd Oct 31, 2024
f11bb5e
Remove rate limit
hanydd Oct 31, 2024
038696b
get cid
hanydd Oct 31, 2024
5b9f197
Add validation
hanydd Nov 1, 2024
645538c
save cid
hanydd Nov 1, 2024
1b1fa93
Update git ignore
hanydd Nov 1, 2024
98fe018
allow empty cid
hanydd Nov 1, 2024
cb183a4
get cid from label
hanydd Nov 1, 2024
aae3cfb
VIP doesn't start locked
hanydd Nov 1, 2024
75bdacb
Make label return cid
hanydd Nov 3, 2024
3e68995
get segments match cid
hanydd Nov 3, 2024
6211579
Add cid filter
hanydd Nov 3, 2024
ae975b0
Add default cid empty string
hanydd Nov 3, 2024
a01fccf
Update prettier
hanydd Nov 3, 2024
27ab1ce
Add cid to choose segments
hanydd Nov 3, 2024
1729389
Add clear key pattern
hanydd Nov 4, 2024
d911758
wildcard key
hanydd Nov 4, 2024
4cefcb8
Reformat
hanydd Nov 5, 2024
29f723b
remove old method
hanydd Nov 5, 2024
8ff83b1
get all cid when not specified
hanydd Nov 5, 2024
a57cbf7
add method to get all cid list
hanydd Nov 5, 2024
a39dc0d
move video id validator
hanydd Nov 5, 2024
8cfe007
Change video detail info
hanydd Nov 5, 2024
2b0633a
add cid when getting duraiton
hanydd Nov 5, 2024
d470ce6
reformat
hanydd Nov 5, 2024
fa9edac
check cid valid
hanydd Nov 5, 2024
406a878
update error msg
hanydd Nov 5, 2024
d3e30f1
longer timeout
hanydd Nov 10, 2024
6cd84b6
make cid primary key
hanydd Nov 10, 2024
8fa987b
Update db to take cid
hanydd Nov 10, 2024
d45ca38
save cid in video info
hanydd Nov 10, 2024
2ef4787
Add more info for videoInfo
hanydd Nov 10, 2024
0020055
add job to populate cid
hanydd Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# vscode
.vscode

# Logs
logs
*.log
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"printWidth": 140,
"tabWidth": 4,
"semi": true,
"endOfLine": "auto"
Expand Down
18 changes: 0 additions & 18 deletions databases/_private.db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@ CREATE TABLE IF NOT EXISTS "config" (
"value" TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS "titleVotes" (
"id" SERIAL PRIMARY KEY,
"videoID" TEXT NOT NULL,
"UUID" TEXT NOT NULL,
"userID" TEXT NOT NULL,
"hashedIP" TEXT NOT NULL,
"type" INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS "thumbnailVotes" (
"id" SERIAL PRIMARY KEY,
"videoID" TEXT NOT NULL,
"UUID" TEXT NOT NULL,
"userID" TEXT NOT NULL,
"hashedIP" TEXT NOT NULL,
"type" INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS "portVideo" (
"bvID" TEXT NOT NULL,
"UUID" TEXT PRIMARY KEY,
Expand Down
2 changes: 1 addition & 1 deletion databases/_private_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

CREATE INDEX IF NOT EXISTS "privateDB_sponsorTimes_v4"
ON public."sponsorTimes" USING btree
("videoID" ASC NULLS LAST, service COLLATE pg_catalog."default" ASC NULLS LAST, "timeSubmitted" ASC NULLS LAST);
("videoID" ASC NULLS LAST, "cid" ASC NULLS LAST, service COLLATE pg_catalog."default" ASC NULLS LAST, "timeSubmitted" ASC NULLS LAST);

-- votes

Expand Down
43 changes: 0 additions & 43 deletions databases/_sponsorTimes.db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,6 @@ CREATE TABLE IF NOT EXISTS "config" (
"value" TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS "titles" (
"videoID" TEXT NOT NULL,
"title" TEXT NOT NULL,
"original" INTEGER default 0,
"userID" TEXT NOT NULL,
"service" TEXT NOT NULL,
"hashedVideoID" TEXT NOT NULL,
"timeSubmitted" INTEGER NOT NULL,
"UUID" TEXT NOT NULL PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS "titleVotes" (
"UUID" TEXT NOT NULL PRIMARY KEY,
"votes" INTEGER NOT NULL default 0,
"locked" INTEGER NOT NULL default 0,
"shadowHidden" INTEGER NOT NULL default 0,
FOREIGN KEY("UUID") REFERENCES "titles"("UUID")
);

CREATE TABLE IF NOT EXISTS "thumbnails" (
"videoID" TEXT NOT NULL,
"original" INTEGER default 0,
"userID" TEXT NOT NULL,
"service" TEXT NOT NULL,
"hashedVideoID" TEXT NOT NULL,
"timeSubmitted" INTEGER NOT NULL,
"UUID" TEXT NOT NULL PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS "thumbnailTimestamps" (
"UUID" TEXT NOT NULL PRIMARY KEY,
"timestamp" INTEGER NOT NULL default 0,
FOREIGN KEY("UUID") REFERENCES "thumbnails"("UUID")
);

CREATE TABLE IF NOT EXISTS "thumbnailVotes" (
"UUID" TEXT NOT NULL PRIMARY KEY,
"votes" INTEGER NOT NULL default 0,
"locked" INTEGER NOT NULL default 0,
"shadowHidden" INTEGER NOT NULL default 0,
FOREIGN KEY("UUID") REFERENCES "thumbnails"("UUID")
);

CREATE TABLE IF NOT EXISTS "portVideo" (
"bvID" TEXT NOT NULL,
"ytbID" TEXT NOT NULL,
Expand Down
7 changes: 7 additions & 0 deletions databases/_upgrade_private_14.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN TRANSACTION;

ALTER TABLE "sponsorTimes" ADD "cid" TEXT NOT NULL DEFAULT '';

UPDATE "config" SET value = 14 WHERE key = 'version';

COMMIT;
9 changes: 0 additions & 9 deletions databases/_upgrade_private_4.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
BEGIN TRANSACTION;

CREATE TABLE IF NOT EXISTS "ratings" (
"videoID" TEXT NOT NULL,
"service" TEXT NOT NULL default 'YouTube',
"type" INTEGER NOT NULL,
"userID" TEXT NOT NULL,
"timeSubmitted" INTEGER NOT NULL,
"hashedIP" TEXT NOT NULL
);

UPDATE "config" SET value = 4 WHERE key = 'version';

COMMIT;
8 changes: 0 additions & 8 deletions databases/_upgrade_sponsorTimes_28.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
BEGIN TRANSACTION;

CREATE TABLE IF NOT EXISTS "ratings" (
"videoID" TEXT NOT NULL,
"service" TEXT NOT NULL default 'YouTube',
"type" INTEGER NOT NULL,
"count" INTEGER NOT NULL,
"hashedVideoID" TEXT NOT NULL
);

UPDATE "config" SET value = 28 WHERE key = 'version';

COMMIT;
1 change: 0 additions & 1 deletion databases/_upgrade_sponsorTimes_32.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ALTER TABLE "shadowBannedUsers" ADD PRIMARY KEY ("userID"); --!sqlite-ignore
ALTER TABLE "unlistedVideos" ADD "id" SERIAL PRIMARY KEY; --!sqlite-ignore
ALTER TABLE "config" ADD PRIMARY KEY ("key"); --!sqlite-ignore
ALTER TABLE "archivedSponsorTimes" ADD PRIMARY KEY ("UUID"); --!sqlite-ignore
ALTER TABLE "ratings" ADD "id" SERIAL PRIMARY KEY; --!sqlite-ignore

UPDATE "config" SET value = 32 WHERE key = 'version';

Expand Down
2 changes: 0 additions & 2 deletions databases/_upgrade_sponsorTimes_35.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
BEGIN TRANSACTION;

ALTER TABLE "titleVotes" ADD "verification" INTEGER default 0;

UPDATE "config" SET value = 35 WHERE key = 'version';

COMMIT;
2 changes: 0 additions & 2 deletions databases/_upgrade_sponsorTimes_37.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
BEGIN TRANSACTION;

ALTER TABLE "titles" ADD UNIQUE ("videoID", "title"); --!sqlite-ignore

UPDATE "config" SET value = 37 WHERE key = 'version';

COMMIT;
6 changes: 0 additions & 6 deletions databases/_upgrade_sponsorTimes_38.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
BEGIN TRANSACTION;

UPDATE "titleVotes" SET "shadowHidden" = 1
WHERE "UUID" IN (SELECT "UUID" FROM "titles" INNER JOIN "shadowBannedUsers" "bans" ON "titles"."userID" = "bans"."userID");

UPDATE "thumbnailVotes" SET "shadowHidden" = 1
WHERE "UUID" IN (SELECT "UUID" FROM "thumbnails" INNER JOIN "shadowBannedUsers" "bans" ON "thumbnails"."userID" = "bans"."userID");

UPDATE "config" SET value = 38 WHERE key = 'version';

COMMIT;
6 changes: 0 additions & 6 deletions databases/_upgrade_sponsorTimes_39.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
BEGIN TRANSACTION;

ALTER TABLE "titleVotes" ADD "downvotes" INTEGER default 0;
ALTER TABLE "titleVotes" ADD "removed" INTEGER default 0;

ALTER TABLE "thumbnailVotes" ADD "downvotes" INTEGER default 0;
ALTER TABLE "thumbnailVotes" ADD "removed" INTEGER default 0;

UPDATE "config" SET value = 39 WHERE key = 'version';

COMMIT;
30 changes: 30 additions & 0 deletions databases/_upgrade_sponsorTimes_43.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BEGIN TRANSACTION;

ALTER TABLE "sponsorTimes" ADD "cid" TEXT NOT NULL DEFAULT '';
ALTER TABLE "portVideo" ADD "cid" TEXT NOT NULL DEFAULT '';
ALTER TABLE "archivedSponsorTimes" ADD "cid" TEXT NOT NULL DEFAULT '';
ALTER TABLE "lockCategories" ADD "cid" TEXT NOT NULL DEFAULT '';

ALTER TABLE "archivedSponsorTimes" ADD "ytbID" TEXT;
ALTER TABLE "archivedSponsorTimes" ADD "ytbSegmentUUID" TEXT;
ALTER TABLE "archivedSponsorTimes" ADD "portUUID" TEXT;

CREATE TABLE "sqlb_temp_table_43" (
"videoID" TEXT NOT NULL,
"cid" TEXT NOT NULL DEFAULT '',
"channelID" TEXT NOT NULL,
"title" TEXT NOT NULL,
"part" INTEGER NOT NULL DEFAULT 0,
"partTitile" TEXT,
"published" NUMERIC NOT NULL,
PRIMARY KEY("videoID", "cid")
);

INSERT INTO sqlb_temp_table_43 SELECT "videoID", '', "channelID", "title", 1, '', "published" FROM "videoInfo";

DROP TABLE "videoInfo";
ALTER TABLE sqlb_temp_table_43 RENAME TO "videoInfo";

UPDATE "config" SET value = 43 WHERE key = 'version';

COMMIT;
2 changes: 2 additions & 0 deletions src/cronjob/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { config } from "../config";
import { Logger } from "../utils/logger";
import { dumpDatebaseJob } from "./dumpDatabase";
import { refreshCidJob } from "./refreshCid";
import refreshTopUserViewJob from "./refreshTopUserView";

export function startAllCrons(): void {
Expand All @@ -9,6 +10,7 @@ export function startAllCrons(): void {

refreshTopUserViewJob.start();
dumpDatebaseJob.start();
refreshCidJob.start();
} else {
Logger.info("Crons dissabled");
}
Expand Down
78 changes: 78 additions & 0 deletions src/cronjob/refreshCid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { CronJob } from "cron";
import { saveVideoInfo } from "../dao/videoInfo";
import { db } from "../databases/databases";
import { DBSegment, HiddenType, SegmentUUID } from "../types/segments.model";
import { durationEquals } from "../utils/durationUtil";
import { getVideoDetails, VideoDetail } from "../utils/getVideoDetails";
import { Logger } from "../utils/logger";
import { sleep } from "../utils/timeUtil";

export const refreshCidJob = new CronJob("*/1 * * * *", () => 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<string, DBSegment[]>();
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) {
let biliVideoDetail: VideoDetail;
try {
biliVideoDetail = await getVideoDetails(videoID);
if (biliVideoDetail === null || biliVideoDetail === undefined) {
Logger.error(`Failed to get video detail for ${videoID}`);
continue;
}
} catch (e) {
Logger.error(`Failed to get video detail for ${videoID}`);
continue;
}

await saveVideoInfo(biliVideoDetail);

const invalidIDs: SegmentUUID[] = [];

if (biliVideoDetail.page.length === 1 || !!biliVideoDetail.page[0].cid) {
await db.prepare("run", `UPDATE "sponsorTimes" SET "cid" = ? WHERE "videoID" = ? AND "cid" = ''`, [
biliVideoDetail.page[0].cid,
videoID,
]);
invalidIDs.push(
...segments.filter((s) => !durationEquals(s.videoDuration, biliVideoDetail.page[0].duration)).map((s) => s.UUID)
);
} else {
// find a matching cid
for (const segment of segments) {
const possibleCids = biliVideoDetail.page.filter((p) => durationEquals(p.duration, segment.videoDuration));
if (possibleCids.length === 1) {
await db.prepare("run", `UPDATE "sponsorTimes" SET "cid" = ? WHERE "UUID" = ?`, [possibleCids[0].cid, segment.UUID]);
}
}
}

// Hide segments with invalid cids
if (invalidIDs.length > 0) {
await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = ? WHERE "UUID" IN (${invalidIDs.map(() => "?").join(",")})`, [
HiddenType.Hidden,
...invalidIDs,
]);
}

await sleep(10000);
}

isRunning = false;
}
Loading