Maxim Yefremov Maxim Yefremov - 3 months ago 14
Node.js Question

check on server side if youtube video exist

How to check if youtube video exists on node.js app server side:

var youtubeId = "adase268_";

// pseudo code
youtubeVideoExist = function (youtubeId){
return true; // if youtube video exists
}

Answer

Using youtube-feeds module. Works fast (~200ms) and no need API_KEY

youtube = require("youtube-feeds");

existsFunc = function(youtubeId, callback) {
  youtube.video(youtubeId, function(err, result) {
    var exists;
    exists = result.id === youtubeId;
    console.log("youtubeId");
    console.log(youtubeId);
    console.log("exists");
    console.log(exists);
    callback (exists);
  });
};


var notExistentYoutubeId = "y0srjasdkfjcKC4eY"
existsFunc (notExistentYoutubeId, console.log)

var existentYoutubeId = "y0srjcKC4eY"
existsFunc (existentYoutubeId, console.log)

output:

❯ node /pathToFileWithCodeAbove/FileWithCodeAbove.js
youtubeId
y0srjcKC4eY
exists
true
true
youtubeId
y0srjasdkfjcKC4eY
exists
false
false