From ba5e89e892ce98a11ae03d928530c762a4502a14 Mon Sep 17 00:00:00 2001 From: Omer Sabic Date: Wed, 19 Jun 2024 19:27:03 +0200 Subject: [PATCH] removed unnecessary code --- src/index.js | 2 +- src/routes/videos.js | 29 +------------------------ src/utils/youtube.js | 51 -------------------------------------------- 3 files changed, 2 insertions(+), 80 deletions(-) diff --git a/src/index.js b/src/index.js index e5cb353..a44da11 100644 --- a/src/index.js +++ b/src/index.js @@ -45,7 +45,7 @@ export const main = async () => { server.register(oauth, { name: 'googleOAuth2', - scope: ['https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/youtube.force-ssl', "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"], + scope: ['https://www.googleapis.com/auth/youtube.readonly', "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"], credentials: { client: { id: env.GOOGLE_CLIENT_ID, diff --git a/src/routes/videos.js b/src/routes/videos.js index b543448..da0d328 100644 --- a/src/routes/videos.js +++ b/src/routes/videos.js @@ -4,7 +4,7 @@ import { eq } from "drizzle-orm"; import { db } from "../db/index.js"; import { users } from "../db/schemas.js"; import { authMiddleware } from "../modules/middleware.js"; -import { getAccessToken, getCaptionText, getVideoCaptions, getVideosFromPlaylist } from "../utils/youtube.js"; +import { getAccessToken, getVideosFromPlaylist } from "../utils/youtube.js"; /** * * @param {FastifyInstance} fastify @@ -29,32 +29,5 @@ export const videoRoutes = (fastify, _, done) => { } }); - fastify.get("/captions/:video_id", async (request, response) => { - try { - - const token = await getAccessToken(fastify, request); - const captions_list = await getVideoCaptions(token.access_token, request.params.video_id); - - const caption = captions_list.filter(x => x.snippet.language === "en"); - - if (caption.length === 0) { - response.send({ - success: false, - message: "Couldn't find caption" - }); - return; - } - - const caption_text = await getCaptionText(token.access_token, caption[0].id); - - response.send({ - captions_info: caption, - captions: caption_text - }); - } catch (e) { - console.log(e); - } - }); - done(); }; diff --git a/src/utils/youtube.js b/src/utils/youtube.js index 9b553d2..045f6c0 100644 --- a/src/utils/youtube.js +++ b/src/utils/youtube.js @@ -17,57 +17,6 @@ export async function getVideoById(access_token, video_id) { return video; } -/** - * - * @param {string} access_token - * @param {string} video_id - * - * @returns {Promise} - */ -export async function getVideoCaptions(access_token, video_id) { - const captions = await service.captions.list({ - access_token, - videoId: video_id, - part: "id,snippet" - }).then(x => x.data.items); - - return captions; -} - -/** - * - * @param {string} access_token - * @param {string} caption_id - * - * @returns {Promise} - */ -export async function getCaptionText(access_token, caption_id) { - const caption_text = await service.captions.download({ - id: caption_id, - part: "snippet", - tfmt: "vtt", - tlang: "en", - headers: { - "Authorization": "Bearer " + access_token - } - }).then(res => res.data).then(x => x.text()); - - return caption_text; -} - -export function parseTextFromCaptions(caption_text) { - let text_content = ""; - const captionEntries = caption_text.split(/\n\n/); - for (const entry of captionEntries) { - const lines = entry.trim().split('\n'); - if (lines.length >= 2 && !lines[1].includes('-->')) { - text_content += lines.slice(1).join(' ').trim() + ' '; - } - } - - return text_content -} - /** * * @param {string} access_token