vrchat-youtube-search-api/VRCYoutubeSearch.js

105 lines
2.7 KiB
JavaScript

import { searchYouTubeVideos, continueYouTubeVideoSearch } from "./simpleYoutubeSearch.js";
import { putVrcUrl } from "./vrcurl.js";
import { makeImageSheetVrcUrl, thumbnailWidth, thumbnailHeight, iconWidth, iconHeight } from "./imagesheet.js";
import { getTrending } from "./trending.js";
var cache = {};
export async function cachedVRCYoutubeSearch(pool, query, options) {
var key = JSON.stringify([pool, query, options]);
if (!cache[key]) {
cache[key] = VRCYoutubeSearch(pool, query, options);
setTimeout(() => {
delete cache[key];
}, 1000*60*10); // 10 mins
}
return await cache[key];
}
async function VRCYoutubeSearch(pool, query, options = {}) {
console.debug("search:", JSON.stringify(query));
var data = {results: []};
if (typeof query == "object") {
switch (query.type) {
case "trending":
var {videos, tabs} = await getTrending(query.bp);
data.tabs = [];
for (let tab of tabs) {
data.tabs.push({
name: tab.name,
vrcurl: await putVrcUrl(pool, {type: "trending", bp: tab.bp})
});
}
break;
case "continuation":
var {videos, continuationData} = await continueYouTubeVideoSearch(query.continuationData);
break;
}
} else {
var {videos, continuationData} = await searchYouTubeVideos(query);
}
if (options.thumbnails) {
var thumbnailUrls = videos.map(video => video.thumbnailUrl);
}
if (options.icons) {
var iconUrls = new Set();
for (let video of videos) {
iconUrls.add(video.channel.iconUrl);
}
iconUrls = [...iconUrls];
}
if (thumbnailUrls?.length || iconUrls?.length) {
try {
var {vrcurl: imagesheet_vrcurl, thumbnails, icons} = await makeImageSheetVrcUrl(pool, thumbnailUrls, iconUrls);
data.imagesheet_vrcurl = imagesheet_vrcurl;
} catch (error) {
console.error(error.stack);
}
}
for (let video of videos) {
video.vrcurl = await putVrcUrl(pool, {type: "redirect", url: `https://www.youtube.com/watch?v=${video.id}`});
if (thumbnails?.length) {
let thumbnail = thumbnails.find(x => x.url == video.thumbnailUrl);
video.thumbnail = {
x: thumbnail?.x,
y: thumbnail?.y,
width: thumbnailWidth,
height: thumbnailHeight
};
}
if (icons?.length) {
let icon = icons.find(x => x.url == video.channel.iconUrl);
video.channel.icon = {
x: icon?.x,
y: icon?.y,
width: iconWidth,
height: iconHeight
};
}
if (options.captions) {
video.captions_vrcurl = await putVrcUrl(pool, {type: "captions", videoId: video.id});
}
delete video.thumbnailUrl;
delete video.channel.iconUrl;
data.results.push(video);
}
if (continuationData) data.nextpage_vrcurl = await putVrcUrl(pool, {
type: "ytContinuation",
continuationData,
options
});
return data;
}