mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fix parsing of video IDs out of shared YouTube URLs
This commit is contained in:
parent
400a62b9f4
commit
08e14c7577
1 changed files with 16 additions and 7 deletions
|
|
@ -12,21 +12,30 @@ const YOUTUBE_URL_MATCH =
|
||||||
exports.youtubeHandler = {
|
exports.youtubeHandler = {
|
||||||
|
|
||||||
shouldPrehandle: (url, env) => {
|
shouldPrehandle: (url, env) => {
|
||||||
return YOUTUBE_URL_MATCH.test(url.toString())
|
return YOUTUBE_URL_MATCH.test(url.toString())
|
||||||
|
},
|
||||||
|
|
||||||
|
getVideoId: (url) => {
|
||||||
|
const u = new URL(url);
|
||||||
|
const videoId = u.searchParams['v']
|
||||||
|
if (!videoId) {
|
||||||
|
const match = url.toString().match(YOUTUBE_URL_MATCH)
|
||||||
|
if (match === null || match.length < 6 || !match[5]) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
return match[5]
|
||||||
|
}
|
||||||
|
return videoId
|
||||||
},
|
},
|
||||||
|
|
||||||
prehandle: async (url, env) => {
|
prehandle: async (url, env) => {
|
||||||
console.log('prehandling youtube url', url)
|
const videoId = getVideoId(url)
|
||||||
|
if (!videoId) {
|
||||||
const match = url.toString().match(YOUTUBE_URL_MATCH)
|
|
||||||
if (match === null || match.length < 6 || !match[5]) {
|
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const videoId = match[5]
|
|
||||||
const oembedUrl = `https://www.youtube.com/oembed?format=json&url=` + encodeURIComponent(`https://www.youtube.com/watch?v=${videoId}`)
|
const oembedUrl = `https://www.youtube.com/oembed?format=json&url=` + encodeURIComponent(`https://www.youtube.com/watch?v=${videoId}`)
|
||||||
const oembed = (await axios.get(oembedUrl.toString())).data;
|
const oembed = (await axios.get(oembedUrl.toString())).data;
|
||||||
console.log('pageContent', oembed);
|
|
||||||
const title = oembed.title;
|
const title = oembed.title;
|
||||||
const ratio = oembed.width / oembed.height;
|
const ratio = oembed.width / oembed.height;
|
||||||
const thumbnail = oembed.thumbnail_url;
|
const thumbnail = oembed.thumbnail_url;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue