2024-04-21 22:41:38 +00:00
|
|
|
/** @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";
|
2024-04-24 20:49:59 +00:00
|
|
|
import { getAccessToken, getCaptionText, getVideoCaptions, getVideosFromPlaylist } from "../utils/youtube.js";
|
|
|
|
import { createBlogFromCaptions } from "../utils/ai.js";
|
2024-04-21 22:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {FastifyInstance} fastify
|
|
|
|
* @param {unknown} _
|
|
|
|
* @param {() => void} done
|
|
|
|
*/
|
|
|
|
export const videoRoutes = (fastify, _, done) => {
|
|
|
|
fastify.register(authMiddleware);
|
|
|
|
|
|
|
|
fastify.get("/", async (request, response) => {
|
|
|
|
try {
|
2024-04-24 20:49:59 +00:00
|
|
|
const token = await getAccessToken(fastify, request);
|
2024-04-21 22:41:38 +00:00
|
|
|
const [user] = await db.select().from(users).where(eq(users.id, request.session.user_id));
|
2024-04-28 18:58:31 +00:00
|
|
|
const videos = await getVideosFromPlaylist(token, user.uploads_playlist_id);
|
2024-04-21 22:41:38 +00:00
|
|
|
|
|
|
|
response.send({
|
|
|
|
success: true,
|
|
|
|
videos
|
2024-04-28 18:58:31 +00:00
|
|
|
});
|
2024-04-21 22:41:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get("/captions/:video_id", async (request, response) => {
|
|
|
|
try {
|
|
|
|
|
2024-04-24 20:49:59 +00:00
|
|
|
const token = await getAccessToken(fastify, request);
|
2024-04-21 22:41:38 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-04-24 20:49:59 +00:00
|
|
|
fastify.get("/blogify/: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);
|
|
|
|
|
|
|
|
const ai_response = await createBlogFromCaptions(caption_text);
|
|
|
|
|
|
|
|
response.send(ai_response);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-21 22:41:38 +00:00
|
|
|
done();
|
|
|
|
};
|