62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
|
/** @typedef {import("fastify").FastifyInstance} FastifyInstance */
|
||
|
|
||
|
import { eq } from "drizzle-orm";
|
||
|
import { db } from "../db/index.js";
|
||
|
import { users } from "../db/schemas.js";
|
||
|
import { authMiddleware } from "../modules/middleware.js";
|
||
|
import { getCaptionText, getNewToken, getVideoCaptions, getVideosFromPlaylist } from "../utils/youtube.js";
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param {FastifyInstance} fastify
|
||
|
* @param {unknown} _
|
||
|
* @param {() => void} done
|
||
|
*/
|
||
|
export const videoRoutes = (fastify, _, done) => {
|
||
|
fastify.register(authMiddleware);
|
||
|
|
||
|
fastify.get("/", async (request, response) => {
|
||
|
try {
|
||
|
const token = await getNewToken(fastify.googleOAuth2, request.session, {});
|
||
|
const [user] = await db.select().from(users).where(eq(users.id, request.session.user_id));
|
||
|
const videos = await getVideosFromPlaylist(token.access_token, user.uploads_playlist_id);
|
||
|
|
||
|
response.send({
|
||
|
success: true,
|
||
|
videos
|
||
|
})
|
||
|
} catch (e) {
|
||
|
console.log(e);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
fastify.get("/captions/:video_id", async (request, response) => {
|
||
|
try {
|
||
|
|
||
|
const token = await getNewToken(fastify.googleOAuth2, request.session, {});
|
||
|
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();
|
||
|
};
|