From 42da1b6c2319aae1584d1e66eb881655ada7e44e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Wed, 23 Jun 2021 19:19:30 -0400 Subject: [PATCH] Add endpoint to submit unlisted videos A temporary measure which will be removed next month https://support.google.com/youtube/answer/9230970 --- databases/_upgrade_sponsorTimes_15.sql | 10 ++++++++ src/app.ts | 3 +++ src/routes/addUnlistedVideo.ts | 33 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 databases/_upgrade_sponsorTimes_15.sql create mode 100644 src/routes/addUnlistedVideo.ts diff --git a/databases/_upgrade_sponsorTimes_15.sql b/databases/_upgrade_sponsorTimes_15.sql new file mode 100644 index 0000000..a36d70d --- /dev/null +++ b/databases/_upgrade_sponsorTimes_15.sql @@ -0,0 +1,10 @@ +BEGIN TRANSACTION; + +CREATE TABLE IF NOT EXISTS "unlistedVideos" ( + "videoID" TEXT NOT NULL, + "timeSubmitted" INTEGER NOT NULL +); + +UPDATE "config" SET value = 15 WHERE key = 'version'; + +COMMIT; diff --git a/src/app.ts b/src/app.ts index 589f352..3ce004f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -30,6 +30,7 @@ import {rateLimitMiddleware} from './middleware/requestRateLimit'; import dumpDatabase, {redirectLink} from './routes/dumpDatabase'; import {endpoint as getSegmentInfo} from './routes/getSegmentInfo'; import {postClearCache} from './routes/postClearCache'; +import { addUnlistedVideo } from './routes/addUnlistedVideo'; export function createServer(callback: () => void) { // Create a service (the app object is just a callback). @@ -140,6 +141,8 @@ function setupRoutes(app: Express) { //clear cache as VIP app.post('/api/clearCache', postClearCache) + app.post('/api/unlistedVideo', addUnlistedVideo) + if (config.postgres) { app.get('/database', (req, res) => dumpDatabase(req, res, true)); app.get('/database.json', (req, res) => dumpDatabase(req, res, false)); diff --git a/src/routes/addUnlistedVideo.ts b/src/routes/addUnlistedVideo.ts new file mode 100644 index 0000000..557f7f8 --- /dev/null +++ b/src/routes/addUnlistedVideo.ts @@ -0,0 +1,33 @@ +import { time } from 'console'; +import {Request, Response} from 'express'; +import { db } from '../databases/databases'; +import { Logger } from '../utils/logger'; + +/** + * Optional API method that will be used temporarily to help collect + * unlisted videos created before 2017 + * + * https://support.google.com/youtube/answer/9230970 + */ + +export function addUnlistedVideo(req: Request, res: Response) { + const videoID = req.body.videoID; + console.log(req.body) + + if (videoID === undefined || typeof(videoID) !== "string" || videoID.length !== 11) { + res.status(400).send("Invalid parameters"); + return; + } + + try { + const timeSubmitted = Date.now(); + db.prepare('run', `INSERT INTO "unlistedVideos" ("videoID", "timeSubmitted") values (?, ?)`, [videoID, timeSubmitted]); + } catch (err) { + Logger.error(err); + res.sendStatus(500); + + return; + } + + res.sendStatus(200); +} \ No newline at end of file