-
-
Save codernik/0b6ee9410615accb181e39746ba626fe to your computer and use it in GitHub Desktop.
Fetches YouTube Video Information and creates a json-ld Video Object
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
<!doctype html> | |
<html class="no-js" lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Foundation | Welcome</title> | |
<link rel="stylesheet" href="css/foundation.css" /> | |
</head> | |
<body> | |
<div class="row"> | |
<div class="medium-2 columns"> </div> | |
<div class="medium-8 columns"> | |
<div class="YT-Container text-center"> | |
YoutTube Video ID: <input type="text" id="YT-ID"> | |
<div class="button" id="YT-GO">Submit</div> | |
<div class="button" id="YT-Copy">Copy</div> | |
</div> | |
<code id="YT-Data" style="overflow-wrap: break-word;"></code> | |
</div> | |
<div class="medium-2 columns"> </div> | |
</div> | |
<script src="js/vendor/jquery.js"></script> | |
<script defer> | |
$(document).ready(function(){ | |
$("#YT-GO").click(function(){ | |
let apiKey = "AIzaSyCVns3Lh4xYBWNgOYeB8LQ5oMLOfL7u6Yw", | |
part = "snippet,contentDetails,statistics", | |
id = $("#YT-ID").val() | |
if(id == null || id.match(/^[a-zA-Z0-9_-]{11}$/) == null) | |
return $("#YT-Data").text("Invalid YouTube ID.").css("color", "#F00"); | |
$.getJSON(`https://www.googleapis.com/youtube/v3/videos?key=${apiKey}&part=${part}&id=${id}`, function(data){ | |
if (data == null || data.error != null || data.pageInfo.totalResults == 0) | |
$("#YT-Data").text("Error fetching data.").css("color", "#F00"); | |
else { | |
let video = data.items[0]; | |
$("#YT-Data").text( | |
`<div class="video-container"><script type="application/ld+json">{"@context":"http://schema.org","@type":"VideoObject",${reduce({ | |
"name" : video.snippet.title.replace(new RegExp('"', 'g'), '\\"'), | |
"description" : video.snippet.description.replace(new RegExp('"', 'g'), '\\"'), | |
"thumbnailUrl" : video.snippet.thumbnails.standard != null ? | |
video.snippet.thumbnails.standard.url : | |
video.snippet.thumbnails.default.url, | |
"uploadDate" : video.snippet.publishedAt, | |
"duration" : video.contentDetails.duration, | |
"keywords" : video.snippet.tags, | |
"contentUrl" : `https://youtube.com/watch/?v=${video.id}`, | |
"embedUrl" : `https://www.youtube.com/embed/${video.id}`, | |
"commentCount" : video.statistics.commentCount, | |
"interactionCount" : video.statistics.viewCount | |
})}}<\/script><div class="youtube-player" data-id="${video.id}"></div></div>`).css("color", "#bababa"); | |
} | |
}); | |
}); | |
$("#YT-Copy").click(function(){copyToClipboard("#YT-Data");}); | |
}); | |
function reduce(dict, showIndex = true) { | |
let returnList = []; | |
for(let [k,v] of Object.entries(dict)) { | |
if(typeof(v) == "string") | |
returnList.push(showIndex ? `"${k}":"${v}"` : `"${v}"`); | |
else if(typeof(v) == "object") | |
returnList.push(`"${k}":[${reduce(v, false)}]`) | |
} | |
return returnList.reduce((a,b) => String(a) + "," + String(b)); | |
} | |
// https://codepen.io/shaikmaqsood/pen/XmydxJ/ | |
function copyToClipboard(element) { | |
var $temp = $("<input>"); | |
$("body").append($temp); | |
$temp.val($(element).text()).select(); | |
document.execCommand("copy"); | |
$temp.remove(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment