diff --git a/src/utils/youtube.js b/src/utils/youtube.js index 955a589..29425fb 100644 --- a/src/utils/youtube.js +++ b/src/utils/youtube.js @@ -10,7 +10,7 @@ export async function getVideoById(access_token, video_id) { access_token, id: video_id, part: "snippet" - }).then(x=>x.data.items[0].snippet); + }).then(x => x.data.items[0].snippet); return video; } @@ -27,7 +27,7 @@ export async function getVideoCaptions(access_token, video_id) { access_token, videoId: video_id, part: "id,snippet" - }).then(x=>x.data.items); + }).then(x => x.data.items); return captions; } @@ -48,8 +48,8 @@ export async function getCaptionText(access_token, caption_id) { headers: { "Authorization": "Bearer " + access_token } - }).then(res=>res.data).then(x=>x.text()); - + }).then(res => res.data).then(x => x.text()); + return caption_text; } @@ -57,10 +57,10 @@ 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() + ' '; - } + const lines = entry.trim().split('\n'); + if (lines.length >= 2 && !lines[1].includes('-->')) { + text_content += lines.slice(1).join(' ').trim() + ' '; + } } return text_content @@ -74,14 +74,21 @@ export function parseTextFromCaptions(caption_text) { * @returns {Promise} */ export async function getVideosFromPlaylist(access_token, playlist_id) { - const videos = await service.playlistItems.list({ - access_token, - part: "id,snippet", - playlistId: playlist_id, - maxResults: 10 - }).then(res => res); + if(!access_token) return []; + let videos; + try { + videos = await service.playlistItems.list({ + access_token, + 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 []; } @@ -102,7 +109,7 @@ export async function getChannelInfo(access_token) { maxResults: 1 }).then(res => res); - if(!channel.data.items[0]) { + if (!channel.data.items[0]) { throw new Error("no_channel"); } return channel.data.items[0]; @@ -125,7 +132,7 @@ export async function getUserInfo(access_token) { } }) const data = await response.json(); - + return data } @@ -141,9 +148,11 @@ export async function getAccessToken(fastify, request) { // const [cachedToken] = await db.select().from(sessions).where(eq(sessions.id, request.session.id)); 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} */ - 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;