Created
February 9, 2022 02:20
-
-
Save GarrettWeinberg/1a69ef77b6db414f487cb6d1250e55fd to your computer and use it in GitHub Desktop.
Verify youtube and vimeo urls and return correct url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const verifyVideoLinks = async (url) => { | |
const YOUTUBE = process.env.REACT_APP_YOUTUBE_API; | |
url.match( | |
/(http|https?:\/\/)(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(&\S+)?/ | |
); | |
const id = RegExp.$6; | |
console.log("id", id); | |
let embed = null; | |
let verified = false; | |
let video; | |
if (RegExp.$3.indexOf("youtu") > -1) { | |
video = await axios( | |
`https://www.googleapis.com/youtube/v3/videos?part=id&id=${id}&key=${YOUTUBE}` | |
); | |
if (video.data.items[0].id) { | |
embed = `https://www.youtube.com/embed/${video.data.items[0].id}`; | |
verified = true; | |
} | |
} else if (RegExp.$3.indexOf("vimeo") > -1) { | |
video = await axios( | |
`https://vimeo.com/api/oembed.json?url=https://vimeo.com/${id}` | |
); | |
if (video.data.video_id) { | |
embed = `https://player.vimeo.com/video/${video.data.video_id}`; | |
verified = true; | |
} | |
} | |
return { | |
embed: embed, | |
verified: verified, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment