This commit is contained in:
Omer Sabic 2024-06-08 21:08:59 +02:00
parent 1a8b81cb53
commit f1bbf852e2

View File

@ -10,7 +10,7 @@ export async function getVideoById(access_token, video_id) {
access_token, access_token,
id: video_id, id: video_id,
part: "snippet" part: "snippet"
}).then(x=>x.data.items[0].snippet); }).then(x => x.data.items[0].snippet);
return video; return video;
} }
@ -27,7 +27,7 @@ export async function getVideoCaptions(access_token, video_id) {
access_token, access_token,
videoId: video_id, videoId: video_id,
part: "id,snippet" part: "id,snippet"
}).then(x=>x.data.items); }).then(x => x.data.items);
return captions; return captions;
} }
@ -48,7 +48,7 @@ export async function getCaptionText(access_token, caption_id) {
headers: { headers: {
"Authorization": "Bearer " + access_token "Authorization": "Bearer " + access_token
} }
}).then(res=>res.data).then(x=>x.text()); }).then(res => res.data).then(x => x.text());
return caption_text; return caption_text;
} }
@ -57,10 +57,10 @@ export function parseTextFromCaptions(caption_text) {
let text_content = ""; let text_content = "";
const captionEntries = caption_text.split(/\n\n/); const captionEntries = caption_text.split(/\n\n/);
for (const entry of captionEntries) { for (const entry of captionEntries) {
const lines = entry.trim().split('\n'); const lines = entry.trim().split('\n');
if (lines.length >= 2 && !lines[1].includes('-->')) { if (lines.length >= 2 && !lines[1].includes('-->')) {
text_content += lines.slice(1).join(' ').trim() + ' '; text_content += lines.slice(1).join(' ').trim() + ' ';
} }
} }
return text_content return text_content
@ -74,14 +74,21 @@ export function parseTextFromCaptions(caption_text) {
* @returns {Promise<import("googleapis").youtube_v3.Schema$PlaylistItem[]>} * @returns {Promise<import("googleapis").youtube_v3.Schema$PlaylistItem[]>}
*/ */
export async function getVideosFromPlaylist(access_token, playlist_id) { export async function getVideosFromPlaylist(access_token, playlist_id) {
const videos = await service.playlistItems.list({ if(!access_token) return [];
access_token, let videos;
part: "id,snippet", try {
playlistId: playlist_id, videos = await service.playlistItems.list({
maxResults: 10 access_token,
}).then(res => res); part: "id,snippet",
playlistId: playlist_id,
maxResults: 10
}).then(res => res)
} catch (e) {
return [];
}
if(videos.status > 299 || videos.status < 200) { console.log(videos.status);
if (videos.status > 299 || videos.status < 200) {
return []; return [];
} }
@ -102,7 +109,7 @@ export async function getChannelInfo(access_token) {
maxResults: 1 maxResults: 1
}).then(res => res); }).then(res => res);
if(!channel.data.items[0]) { if (!channel.data.items[0]) {
throw new Error("no_channel"); throw new Error("no_channel");
} }
return channel.data.items[0]; return channel.data.items[0];
@ -141,9 +148,11 @@ export async function getAccessToken(fastify, request) {
// const [cachedToken] = await db.select().from(sessions).where(eq(sessions.id, request.session.id)); // const [cachedToken] = await db.select().from(sessions).where(eq(sessions.id, request.session.id));
let access_token = request.session.access_token; let access_token = request.session.access_token;
if((new Date().getTime() + 10) > request.session.expires_at) { if ((new Date().getTime() + 10) > request.session.expires_at) {
/** @type {import('@fastify/oauth2').Token} */ /** @type {import('@fastify/oauth2').Token} */
const {token} = await fastify.googleOAuth2.getNewAccessTokenUsingRefreshToken(request.session); const { token } = await fastify.googleOAuth2.getNewAccessTokenUsingRefreshToken(request.session).catch(e => ({token: null}));
if(!token) return ""
access_token = token.access_token; access_token = token.access_token;