Use newleaf instead of YouTube API

This commit is contained in:
Ajay Ramachandran
2021-06-02 22:34:38 -04:00
parent c1609a826a
commit 0904036009
14 changed files with 227 additions and 154 deletions

View File

@@ -44,7 +44,7 @@ addDefaults(config, {
},
},
userCounterURL: null,
youtubeAPIKey: null,
newLeafURL: null,
maxRewardTimePerSegmentInSeconds: 86400,
postgres: null,
dumpDatabase: {

View File

@@ -1,7 +1,7 @@
import {config} from '../config';
import {Logger} from '../utils/logger';
import {db, privateDB} from '../databases/databases';
import {YouTubeAPI} from '../utils/youtubeApi';
import {getMaxResThumbnail, YouTubeAPI} from '../utils/youtubeApi';
import {getSubmissionUUID} from '../utils/getSubmissionUUID';
import fetch from 'node-fetch';
import isoDurations, { end } from 'iso8601-duration';
@@ -18,16 +18,11 @@ import { deleteLockCategories } from './deleteLockCategories';
import { getCategoryActionType } from '../utils/categoryInfo';
import { QueryCacher } from '../utils/queryCacher';
import { getReputation } from '../utils/reputation';
import { APIVideoData, APIVideoInfo } from '../types/youtubeApi.model';
interface APIVideoInfo {
err: string | boolean,
data?: any
}
async function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: any, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) {
async function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: APIVideoData, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) {
const row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID]);
const userName = row !== undefined ? row.userName : null;
const video = youtubeData.items[0];
let scopeName = "submissions.other";
if (submissionCount <= 1) {
@@ -37,8 +32,8 @@ async function sendWebhookNotification(userID: string, videoID: string, UUID: st
dispatchEvent(scopeName, {
"video": {
"id": videoID,
"title": video.snippet.title,
"thumbnail": video.snippet.thumbnails.maxres ? video.snippet.thumbnails.maxres : null,
"title": youtubeData?.title,
"thumbnail": getMaxResThumbnail(youtubeData) || null,
"url": "https://www.youtube.com/watch?v=" + videoID,
},
"submission": {
@@ -76,7 +71,7 @@ async function sendWebhooks(apiVideoInfo: APIVideoInfo, userID: string, videoID:
method: 'POST',
body: JSON.stringify({
"embeds": [{
"title": data.items[0].snippet.title,
"title": data?.title,
"url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (parseInt(startTime.toFixed(0)) - 2),
"description": "Submission ID: " + UUID +
"\n\nTimestamp: " +
@@ -87,7 +82,7 @@ async function sendWebhooks(apiVideoInfo: APIVideoInfo, userID: string, videoID:
"name": userID,
},
"thumbnail": {
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
"url": getMaxResThumbnail(data) || "",
},
}],
}),
@@ -177,10 +172,7 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo,
const {err, data} = apiVideoInfo;
if (err) return false;
// Check to see if video exists
if (data.pageInfo.totalResults === 0) return "No video exists with id " + submission.videoID;
const duration = getYouTubeVideoDuration(apiVideoInfo);
const duration = apiVideoInfo?.data?.lengthSeconds;
const segments = submission.segments;
let nbString = "";
for (let i = 0; i < segments.length; i++) {
@@ -220,8 +212,7 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo,
return a[0] - b[0] || a[1] - b[1];
}));
let videoDuration = data.items[0].contentDetails.duration;
videoDuration = isoDurations.toSeconds(isoDurations.parse(videoDuration));
const videoDuration = data.lengthSeconds;
if (videoDuration != 0) {
let allSegmentDuration = 0;
//sum all segment times together
@@ -273,13 +264,8 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo,
}
}
function getYouTubeVideoDuration(apiVideoInfo: APIVideoInfo): VideoDuration {
const duration = apiVideoInfo?.data?.items[0]?.contentDetails?.duration;
return duration ? isoDurations.toSeconds(isoDurations.parse(duration)) as VideoDuration : null;
}
async function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
if (config.youtubeAPIKey !== null) {
if (config.newLeafURL !== null) {
return YouTubeAPI.listVideos(videoID, ignoreCache);
} else {
return null;
@@ -375,7 +361,7 @@ export async function postSkipSegments(req: Request, res: Response) {
// Don't use cache if we don't know the video duraton, or the client claims that it has changed
apiVideoInfo = await getYouTubeVideoInfo(videoID, !videoDuration || videoDurationChanged(videoDuration));
}
const apiVideoDuration = getYouTubeVideoDuration(apiVideoInfo);
const apiVideoDuration = apiVideoInfo?.data?.lengthSeconds as VideoDuration;
if (!videoDuration || (apiVideoDuration && Math.abs(videoDuration - apiVideoDuration) > 2)) {
// If api duration is far off, take that one instead (it is only precise to seconds, not millis)
videoDuration = apiVideoDuration || 0 as VideoDuration;

View File

@@ -2,7 +2,7 @@ import {Request, Response} from 'express';
import {Logger} from '../utils/logger';
import {isUserVIP} from '../utils/isUserVIP';
import fetch from 'node-fetch';
import {YouTubeAPI} from '../utils/youtubeApi';
import {getMaxResThumbnail, YouTubeAPI} from '../utils/youtubeApi';
import {db, privateDB} from '../databases/databases';
import {dispatchEvent, getVoteAuthor, getVoteAuthorRaw} from '../utils/webhookUtils';
import {getFormattedTime} from '../utils/getFormattedTime';
@@ -57,11 +57,11 @@ async function sendWebhooks(voteData: VoteData) {
webhookURL = config.discordCompletelyIncorrectReportWebhookURL;
}
if (config.youtubeAPIKey !== null) {
if (config.newLeafURL !== null) {
const { err, data } = await YouTubeAPI.listVideos(submissionInfoRow.videoID);
if (err || data.items.length === 0) {
if (err) Logger.error(err.toString());
if (err) {
Logger.error(err.toString());
return;
}
const isUpvote = voteData.incrementAmount > 0;
@@ -72,9 +72,9 @@ async function sendWebhooks(voteData: VoteData) {
},
"video": {
"id": submissionInfoRow.videoID,
"title": data.items[0].snippet.title,
"title": data?.title,
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID,
"thumbnail": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
"thumbnail": getMaxResThumbnail(data) || null,
},
"submission": {
"UUID": voteData.UUID,
@@ -103,7 +103,7 @@ async function sendWebhooks(voteData: VoteData) {
method: 'POST',
body: JSON.stringify({
"embeds": [{
"title": data.items[0].snippet.title,
"title": data?.title,
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID
+ "&t=" + (submissionInfoRow.startTime.toFixed(0) - 2),
"description": "**" + voteData.row.votes + " Votes Prior | " +
@@ -120,7 +120,7 @@ async function sendWebhooks(voteData: VoteData) {
"name": voteData.finalResponse?.finalMessage ?? getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission),
},
"thumbnail": {
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
"url": getMaxResThumbnail(data) || "",
},
}],
}),

View File

@@ -6,7 +6,7 @@ export interface SBSConfig {
mockPort?: number;
globalSalt: string;
adminUserID: string;
youtubeAPIKey?: string;
newLeafURL?: string;
discordReportChannelWebhookURL?: string;
discordFirstTimeSubmissionsWebhookURL?: string;
discordCompletelyIncorrectReportWebhookURL?: string;

View File

@@ -0,0 +1,111 @@
export interface APIVideoData {
"title": string,
"videoId": string,
"videoThumbnails": [
{
"quality": string,
"url": string,
second__originalUrl: string,
"width": number,
"height": number
}
],
"description": string,
"descriptionHtml": string,
"published": number,
"publishedText": string,
"keywords": string[],
"viewCount": number,
"likeCount": number,
"dislikeCount": number,
"paid": boolean,
"premium": boolean,
"isFamilyFriendly": boolean,
"allowedRegions": string[],
"genre": string,
"genreUrl": string,
"author": string,
"authorId": string,
"authorUrl": string,
"authorThumbnails": [
{
"url": string,
"width": number,
"height": number
}
],
"subCountText": string,
"lengthSeconds": number,
"allowRatings": boolean,
"rating": number,
"isListed": boolean,
"liveNow": boolean,
"isUpcoming": boolean,
"premiereTimestamp"?: number,
"hlsUrl"?: string,
"adaptiveFormats": [
{
"index": string,
"bitrate": string,
"init": string,
"url": string,
"itag": string,
"type": string,
"clen": string,
"lmt": string,
"projectionType": number,
"container": string,
"encoding": string,
"qualityLabel"?: string,
"resolution"?: string
}
],
"formatStreams": [
{
"url": string,
"itag": string,
"type": string,
"quality": string,
"container": string,
"encoding": string,
"qualityLabel": string,
"resolution": string,
"size": string
}
],
"captions": [
{
"label": string,
"languageCode": string,
"url": string
}
],
"recommendedVideos": [
{
"videoId": string,
"title": string,
"videoThumbnails": [
{
"quality": string,
"url": string,
"width": number,
"height": number
}
],
"author": string,
"lengthSeconds": number,
"viewCountText": string
}
]
}
export interface APIVideoInfo {
err: string | boolean,
data?: APIVideoData
}

View File

@@ -1,22 +1,16 @@
import fetch from 'node-fetch';
import {config} from '../config';
import {Logger} from './logger';
import redis from './redis';
// @ts-ignore
import _youTubeAPI from 'youtube-api';
_youTubeAPI.authenticate({
type: "key",
key: config.youtubeAPIKey,
});
import { APIVideoData, APIVideoInfo } from '../types/youtubeApi.model';
export class YouTubeAPI {
static async listVideos(videoID: string, ignoreCache = false): Promise<{err: string | boolean, data?: any}> {
const part = 'contentDetails,snippet';
static async listVideos(videoID: string, ignoreCache = false): Promise<APIVideoInfo> {
if (!videoID || videoID.length !== 11 || videoID.includes(".")) {
return { err: "Invalid video ID" };
}
const redisKey = "youtube.video." + videoID;
const redisKey = "yt.newleaf.video." + videoID;
if (!ignoreCache) {
const {err, reply} = await redis.getAsync(redisKey);
@@ -25,34 +19,37 @@ export class YouTubeAPI {
return { err: err?.message, data: JSON.parse(reply) }
}
}
}
if (!config.newLeafURL) return {err: "NewLeaf URL not found", data: null};
try {
const { ytErr, data } = await new Promise((resolve) => _youTubeAPI.videos.list({
part,
id: videoID,
}, (ytErr: boolean | string, result: any) => resolve({ytErr, data: result?.data})));
const result = await fetch(config.newLeafURL + "/api/v1/videos/" + videoID, { method: "GET" });
if (!ytErr) {
// Only set cache if data returned
if (data.items.length > 0) {
const { err: setErr } = await redis.setAsync(redisKey, JSON.stringify(data));
if (result.ok) {
const data = await result.json();
if (data.error) {
return { err: data.err, data: null };
}
if (setErr) {
Logger.warn(setErr.message);
redis.setAsync(redisKey, JSON.stringify(data)).then((result) => {
if (result?.err) {
Logger.warn(result?.err.message);
} else {
Logger.debug("redis: video information cache set for: " + videoID);
}
return { err: false, data }; // don't fail
} else {
return { err: false, data }; // don't fail
}
});
return { err: false, data };
} else {
return { err: ytErr, data };
return { err: result.statusText, data: null };
}
} catch (err) {
return {err, data: null}
}
}
}
export function getMaxResThumbnail(apiInfo: APIVideoData): string | void {
return apiInfo?.videoThumbnails?.find((elem) => elem.quality === "maxres")?.second__originalUrl;
}