Skip to content

Commit

Permalink
link preview youtube - erro; verificar se dá pra pegar as infos pelas…
Browse files Browse the repository at this point in the history
… meta tags [clzjsm33b00072k0ldn09tjsw]
  • Loading branch information
pauloendoh committed Aug 11, 2024
1 parent 984c6b6 commit 1f16f52
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/resolvers/utils/LinkPreview/LinkPreviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<YoutubeDataDto>(
`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) => {
Expand All @@ -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
Expand All @@ -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,
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/resolvers/utils/LinkPreview/types/YoutubeDataDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Item {
kind: string
etag: string
id: string
snippet: Snippet
contentDetails: ContentDetails
statistics: Statistics
}
Expand All @@ -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
}

0 comments on commit 1f16f52

Please sign in to comment.