Skip to content

Instantly share code, notes, and snippets.

@answerquest
Last active July 6, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save answerquest/9b8071375149054060c273f91c59e0a8 to your computer and use it in GitHub Desktop.
Save answerquest/9b8071375149054060c273f91c59e0a8 to your computer and use it in GitHub Desktop.
Retrieve youtube titles and other data from list of video ids or URLs. Created for making attriuted links list.
<!DOCTYPE html>
<!-- forked from https://github.com/salmanarshad2000/demos/blob/v1.0.0/youtube/retrieve-title-description-and-thumbnail.html -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>YouTube: Retrieve Title and info for list of videos</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>Enter YouTube Video ID or URLs in the text box below, one URL per line</h3>
<table width="100%">
<tr width="100%">
<td width="50%">
<textarea id="search-txt" type="text" value="https://www.youtube.com/watch?v=_w1De4KqshI" rows="20" cols="60"></textarea>
<br><br>
<input id="search-btn" type="button" value="Fetch Video Information">
</td><td>
<br><br><br>
<textarea id="output" rows="30" cols="60"></textarea>
<script>
/*
* YouTube: Retrieve Title, and other info
* Original: http://salman-w.blogspot.com/2010/01/retrieve-youtube-video-title.html
*/
$(function() {
$("#search-btn").on("click", function() {
$("#video-data-1, #video-data-2").empty();
document.getElementById("output").value = "";
var array1 = document.getElementById('search-txt').value.split('\n');
for (i=0; i< array1.length; i++) {
//var videoid = $("#search-txt").val();
var videoid = array1[i];
var matches = videoid.match(/^https:\/\/www\.youtube\.com\/.*[?&]v=([^&]+)/i) || videoid.match(/^https:\/\/youtu\.be\/([^?]+)/i);
if (matches) {
videoid = matches[1];
console.log(matches[0]);
console.log(matches[1]);
}
if (videoid.match(/^[a-z0-9_-]{11}$/i) === null) {
document.getElementById("output").value += videoid + " : Unable to parse Video ID/URL.\n\n";
continue;
}
$.getJSON("https://www.googleapis.com/youtube/v3/videos", {
key: "AIzaSyB67EeMDNvqQqPqwVM-Jtqt8Pdpq-R9q4I",
part: "snippet,statistics",
id: videoid
}, function(data) {
if (data.items.length === 0) {
document.getElementById("output").value += videoid + " : Video not found.\n\n";
} else {
document.getElementById("output").value += "https://www.youtube.com/watch?v=" + data.items[0].id + "\n" + data.items[0].snippet.title + " -" + data.items[0].snippet.channelTitle + ", " + data.items[0].snippet.publishedAt.substr(0,10) + "\n\n" ;
}
/*
other infos, from original script:
$("<img>", {
src: data.items[0].snippet.thumbnails.medium.url,
width: data.items[0].snippet.thumbnails.medium.width,
height: data.items[0].snippet.thumbnails.medium.height
}).appendTo("#video-data-1");
$("<p></p>").text(data.items[0].snippet.description).appendTo("#video-data-1");
$("<li></li>").text("Published at: " + data.items[0].snippet.publishedAt).appendTo("#video-data-2");
$("<li></li>").text("View count: " + data.items[0].statistics.viewCount).appendTo("#video-data-2");
$("<li></li>").text("Favorite count: " + data.items[0].statistics.favoriteCount).appendTo("#video-data-2");
$("<li></li>").text("Like count: " + data.items[0].statistics.likeCount).appendTo("#video-data-2");
$("<li></li>").text("Dislike count: " + data.items[0].statistics.dislikeCount).appendTo("#video-data-2");
*/
}).fail(function(jqXHR, textStatus, errorThrown) {
document.getElementById("output").value += "Error for id " + videoid + ": " + ( jqXHR.responseText || errorThrown ) + "\n\n";
});
}
});
});
</script>
</td></tr></table>
<br>
Note: URLs should have https:// and not http:// .<br>
By <a href="http://nikhilvj.cu.cc">Nikhil VJ</a>. Adapted from <a href="http://salman-w.blogspot.com/2010/01/retrieve-youtube-video-title.html">this script by Salman Arshad</a>, to be able to process multiple URLs.
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment