Merge pull request #1746 from omnivore-app/fix/save-youtube-playlist

Fix incorrect Youtube playlist author name and description
This commit is contained in:
Hongbo Wu
2023-01-31 11:06:25 +08:00
committed by GitHub
2 changed files with 53 additions and 8 deletions

View File

@ -18,6 +18,11 @@ export const getYoutubeVideoId = (url: string) => {
return videoId
}
export 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<PreHandleResult> {
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 {
<meta property="og:article:author" content="${authorName}" />
</head>
<body>
<iframe width="${width}" height="${height}" src="https://www.youtube.com/embed/${videoId}" title="${escapedTitle}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<iframe width="${width}" height="${height}" src="${src}" title="${escapedTitle}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<p><a href="${url}" target="_blank">${escapedTitle}</a></p>
<p itemscope="" itemprop="author" itemtype="http://schema.org/Person">By <a href="${oembed.author_url}" target="_blank">${authorName}</a></p>
</body>

View File

@ -1,6 +1,6 @@
import { expect } from 'chai'
import 'mocha'
import { escapeTitle, getYoutubeVideoId } from '../src/websites/youtube-handler'
import { expect } from "chai";
import "mocha";
import { escapeTitle, getYoutubePlaylistId, getYoutubeVideoId } from "../src/websites/youtube-handler";
describe('getYoutubeVideoId', () => {
it('should parse video id out of a URL', async () => {
@ -24,6 +24,36 @@ describe('getYoutubeVideoId', () => {
})
})
describe('getYoutubePlaylistId', () => {
it('should parse playlist id out of a URL', async () => {
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/watch?v=BnSUk0je6oo&t=269s&list=PL6D4F6A0D6B0C6A5E'
)
)
expect('RDvFD2gu007dc').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/watch?v=vFD2gu007dc&list=RDvFD2gu007dc&start_radio=1'
)
)
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/playlist?list=PL6D4F6A0D6B0C6A5E'
)
)
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/playlist?list=PL6D4F6A0D6B0C6A5E&feature=share'
)
)
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/playlist?list=PL6D4F6A0D6B0C6A5E&feature=share'
)
)
})
})
describe('escapeTitle', () => {
it('escapes the special characters in the title', async () => {
expect(escapeTitle("The Stanley's Parable")).to.eq(