removed unnecessary code
This commit is contained in:
parent
fbdabf0a39
commit
ba5e89e892
@ -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,
|
||||
|
@ -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();
|
||||
};
|
||||
|
@ -17,57 +17,6 @@ export async function getVideoById(access_token, video_id) {
|
||||
return video;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} access_token
|
||||
* @param {string} video_id
|
||||
*
|
||||
* @returns {Promise<import("googleapis").youtube_v3.Schema$Caption[]>}
|
||||
*/
|
||||
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<string>}
|
||||
*/
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user