diff --git a/__tests__/lib/__mocks__/youtubeChannel.ts b/__tests__/lib/__mocks__/youtubeChannel.ts new file mode 100644 index 0000000000..ecb3b80314 --- /dev/null +++ b/__tests__/lib/__mocks__/youtubeChannel.ts @@ -0,0 +1,63 @@ +export const youtubeHTML = ` + +
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new features
penguinz0 - YouTube
+ +` diff --git a/__tests__/lib/extractHtmlMeta.test.ts b/__tests__/lib/extractHtmlMeta.test.ts index c33084072d..33f5b4f2d1 100644 --- a/__tests__/lib/extractHtmlMeta.test.ts +++ b/__tests__/lib/extractHtmlMeta.test.ts @@ -69,7 +69,7 @@ describe('extractHtmlMeta', () => { expect(output).toEqual(expectedOutput) }) - it('extracts title and description from a generic youtube page', () => { + it.only('extracts title and description from a generic youtube page', () => { const input = youtubeHTML const expectedOutput = { title: 'HD Video (1080p) with Relaxing Music of Native American Shamans', @@ -81,6 +81,18 @@ describe('extractHtmlMeta', () => { expect(output).toEqual(expectedOutput) }) + it.only('extracts avatar from a youtube channel', () => { + const input = youtubeHTML + const expectedOutput = { + title: 'penguinz0', + description: + "Clips channel: https://www.youtube.com/channel/UC4EQHfzIbkL_Skit_iKt1aA\n\nTwitter: https://twitter.com/MoistCr1TiKaL\n\nInstagram: https://www.instagram.com/bigmoistcr1tikal/?hl=en\n\nTwitch: https://www.twitch.tv/moistcr1tikal\n\nSnapchat: Hugecharles\n\nTik Tok: Hugecharles\n\nI don't have any other public accounts.", + image: 'https://i.ytimg.com/vi/x6UITRjhijI/sddefault.jpg', + } + const output = extractHtmlMeta({html: input, hostname: 'youtube.com'}) + expect(output).toEqual(expectedOutput) + }) + it('extracts username from the url a twitter profile page', () => { const expectedOutput = { title: '@bluesky on Twitter', diff --git a/src/lib/extractYoutubeMeta.ts b/src/lib/extractYoutubeMeta.ts index 566e3be46d..42eed51e8f 100644 --- a/src/lib/extractYoutubeMeta.ts +++ b/src/lib/extractYoutubeMeta.ts @@ -4,10 +4,12 @@ export const extractYoutubeMeta = (html: string): Record => { const youtubeDescriptionRegex = /"videoDetails":.*"shortDescription":"([^"]*)"/i const youtubeThumbnailRegex = /"videoDetails":.*"url":"(.*)(default\.jpg)/i - + const youtubeAvatarRegex = + /"avatar":{"thumbnails":\[{.*?url.*?url.*?url":"([^"]*)"/i const youtubeTitleMatch = youtubeTitleRegex.exec(html) const youtubeDescriptionMatch = youtubeDescriptionRegex.exec(html) const youtubeThumbnailMatch = youtubeThumbnailRegex.exec(html) + const youtubeAvatarMatch = youtubeAvatarRegex.exec(html) if (youtubeTitleMatch && youtubeTitleMatch.length >= 1) { res.title = decodeURI(youtubeTitleMatch[1]) @@ -21,6 +23,9 @@ export const extractYoutubeMeta = (html: string): Record => { if (youtubeThumbnailMatch && youtubeThumbnailMatch.length >= 2) { res.image = youtubeThumbnailMatch[1] + 'default.jpg' } + if (!res.image && youtubeAvatarMatch && youtubeAvatarMatch.length >= 1) { + res.image = youtubeAvatarMatch[1] + } return res } diff --git a/src/lib/images.ts b/src/lib/images.ts index 7b1a939615..beab3956d4 100644 --- a/src/lib/images.ts +++ b/src/lib/images.ts @@ -23,16 +23,12 @@ export interface Image { } export async function downloadAndResize(opts: DownloadAndResizeOpts) { - let appendExt + let appendExt = 'jpeg' try { const urip = new URL(opts.uri) const ext = urip.pathname.split('.').pop() - if (ext === 'jpg' || ext === 'jpeg') { - appendExt = 'jpeg' - } else if (ext === 'png') { + if (ext === 'png') { appendExt = 'png' - } else { - return } } catch (e: any) { console.error('Invalid URI', opts.uri, e)