mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1export const extractYoutubeMeta = (html: string): Record<string, string> => {
2 const res: Record<string, string> = {}
3 const youtubeTitleRegex = /"videoDetails":.*"title":"([^"]*)"/i
4 const youtubeDescriptionRegex =
5 /"videoDetails":.*"shortDescription":"([^"]*)"/i
6 const youtubeThumbnailRegex = /"videoDetails":.*"url":"(.*)(default\.jpg)/i
7 const youtubeAvatarRegex =
8 /"avatar":{"thumbnails":\[{.*?url.*?url.*?url":"([^"]*)"/i
9 const youtubeTitleMatch = youtubeTitleRegex.exec(html)
10 const youtubeDescriptionMatch = youtubeDescriptionRegex.exec(html)
11 const youtubeThumbnailMatch = youtubeThumbnailRegex.exec(html)
12 const youtubeAvatarMatch = youtubeAvatarRegex.exec(html)
13
14 if (youtubeTitleMatch && youtubeTitleMatch.length >= 1) {
15 res.title = decodeURI(youtubeTitleMatch[1])
16 }
17 if (youtubeDescriptionMatch && youtubeDescriptionMatch.length >= 1) {
18 res.description = decodeURI(youtubeDescriptionMatch[1]).replace(
19 /\\n/g,
20 '\n',
21 )
22 }
23 if (youtubeThumbnailMatch && youtubeThumbnailMatch.length >= 2) {
24 res.image = youtubeThumbnailMatch[1] + 'default.jpg'
25 }
26 if (!res.image && youtubeAvatarMatch && youtubeAvatarMatch.length >= 1) {
27 res.image = youtubeAvatarMatch[1]
28 }
29
30 return res
31}