From ec7344b92329505cc12aa356384efa2f6fd3f1d0 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 30 Jan 2023 16:53:19 +0800 Subject: [PATCH] Get playlist id from the URL and retrieve the information of the playlist --- .../src/websites/youtube-handler.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/content-handler/src/websites/youtube-handler.ts b/packages/content-handler/src/websites/youtube-handler.ts index a12107f50..05a6a59b3 100644 --- a/packages/content-handler/src/websites/youtube-handler.ts +++ b/packages/content-handler/src/websites/youtube-handler.ts @@ -18,6 +18,11 @@ export const getYoutubeVideoId = (url: string) => { return videoId } +const getYoutubePlaylistId = (url: string) => { + const u = new URL(url) + return u.searchParams.get('list') +} + export const escapeTitle = (title: string) => { return _.escape(title) } @@ -33,14 +38,24 @@ export class YoutubeHandler extends ContentHandler { } async preHandle(url: string): Promise { - const videoId = getYoutubeVideoId(url) - if (!videoId) { - return {} + let urlToEncode: string + let src: string + const playlistId = getYoutubePlaylistId(url) + if (playlistId) { + urlToEncode = `https://www.youtube.com/playlist?list=${playlistId}` + src = `https://www.youtube.com/embed/videoseries?list=${playlistId}` + } else { + const videoId = getYoutubeVideoId(url) + if (!videoId) { + return {} + } + urlToEncode = `https://www.youtube.com/watch?v=${videoId}` + src = `https://www.youtube.com/embed/${videoId}` } const oembedUrl = `https://www.youtube.com/oembed?format=json&url=` + - encodeURIComponent(`https://www.youtube.com/watch?v=${videoId}`) + encodeURIComponent(urlToEncode) const oembed = (await axios.get(oembedUrl.toString())).data as { title: string width: number @@ -68,7 +83,7 @@ export class YoutubeHandler extends ContentHandler { - +

${escapedTitle}