-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hanydd/dev-cid
Add support for video cid
- Loading branch information
Showing
37 changed files
with
1,020 additions
and
796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# vscode | ||
.vscode | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"printWidth": 120, | ||
"printWidth": 140, | ||
"tabWidth": 4, | ||
"semi": true, | ||
"endOfLine": "auto" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.