From 1f16f5237e0559382eccd7b397d6bfdc46812a7e Mon Sep 17 00:00:00 2001 From: Paulo Endoh Date: Sat, 10 Aug 2024 21:05:04 -0300 Subject: [PATCH] =?UTF-8?q?link=20preview=20youtube=20-=20erro;=20verifica?= =?UTF-8?q?r=20se=20d=C3=A1=20pra=20pegar=20as=20infos=20pelas=20meta=20ta?= =?UTF-8?q?gs=20[clzjsm33b00072k0ldn09tjsw]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/LinkPreview/LinkPreviewService.ts | 21 ++++++++---- .../utils/LinkPreview/types/YoutubeDataDto.ts | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/resolvers/utils/LinkPreview/LinkPreviewService.ts b/src/resolvers/utils/LinkPreview/LinkPreviewService.ts index 1010dc9..40d43fa 100644 --- a/src/resolvers/utils/LinkPreview/LinkPreviewService.ts +++ b/src/resolvers/utils/LinkPreview/LinkPreviewService.ts @@ -65,20 +65,21 @@ export default class LinkPreviewService { response.youtubeVideoLength = videoInfo.duration response.viewCount = videoInfo.viewCount response.url = `https://www.youtube.com/watch?v=${videoInfo.videoId}` + response.title = videoInfo.title + response.image = videoInfo.thumbnail } return response } - // PE 1/3 - remove elses + // PE 1/3 - improve xd async #getYoutubeVideoInfo(url: string) { const videoId = this.$getYoutubeVideoId.exec(url) - let durationStr = "00:00h" let viewCount = 0 - await axios + const metadata = await axios .get( - `https://youtube.googleapis.com/youtube/v3/videos?part=contentDetails%2Cstatistics&id=${videoId}&key=${YOUTUBE_API_KEY}` + `https://youtube.googleapis.com/youtube/v3/videos?part=contentDetails,statistics,snippet&id=${videoId}&key=${YOUTUBE_API_KEY}` ) .then((res) => { @@ -87,7 +88,7 @@ export default class LinkPreviewService { res.data.items[0].contentDetails.duration ).toObject() - durationStr = "" + let durationStr = "00:00h" if (durationObj.hours) { if (durationObj.hours < 10) { durationStr += "0" + durationObj.hours @@ -106,13 +107,19 @@ export default class LinkPreviewService { } else { durationStr += ":00h" } - return durationStr + return { + duration: durationStr, + title: res.data.items[0].snippet.title, + thumbnail: res.data.items[0].snippet.thumbnails.default.url, + } }) return { - duration: durationStr, + duration: metadata.duration, viewCount, videoId, + title: metadata.title, + thumbnail: metadata.thumbnail, } } diff --git a/src/resolvers/utils/LinkPreview/types/YoutubeDataDto.ts b/src/resolvers/utils/LinkPreview/types/YoutubeDataDto.ts index a17a791..5288a5a 100644 --- a/src/resolvers/utils/LinkPreview/types/YoutubeDataDto.ts +++ b/src/resolvers/utils/LinkPreview/types/YoutubeDataDto.ts @@ -14,6 +14,7 @@ interface Item { kind: string etag: string id: string + snippet: Snippet contentDetails: ContentDetails statistics: Statistics } @@ -36,3 +37,36 @@ interface ContentDetails { } interface ContentRating {} + +interface Snippet { + publishedAt: string + channelId: string + title: string + description: string + thumbnails: Thumbnails + channelTitle: string + categoryId: string + liveBroadcastContent: string + defaultLanguage: string + localized: Localized + defaultAudioLanguage: string +} + +interface Localized { + title: string + description: string +} + +interface Thumbnails { + default: Default + medium: Default + high: Default + standard: Default + maxres: Default +} + +interface Default { + url: string + width: number + height: number +}