Skip to content

Instantly share code, notes, and snippets.

@frankhale
Created June 15, 2017 16:55
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 frankhale/6acf7487bb7f5724fedb47a98cae33d6 to your computer and use it in GitHub Desktop.
Save frankhale/6acf7487bb7f5724fedb47a98cae33d6 to your computer and use it in GitHub Desktop.
Example of using YouTube-Search to perform a video search and then integrate the video duration into the results
const util = require("util");
const search = require("youtube-search");
const _ = require("lodash");
var xhr = require("xhr");
if (!xhr.open) xhr = require("request");
var opts = {
maxResults: 1,
key: "<YOUR_KEY_HERE>"
};
search("Dash Berlin", opts, (err, videoResults) => {
if (err) return console.log(err);
let finalSearchResults = [];
let ids = _.map(_.filter(videoResults, { kind: "youtube#video" }), r => {
return r.id;
});
xhr(
{
url: `https://www.googleapis.com/youtube/v3/videos?id=${ids.join(
","
)}&part=contentDetails&key=${opts.key}`,
method: "GET"
},
(err, res, body) => {
const contentDetailsResults = JSON.parse(body);
if (!err) {
//console.log(util.inspect(contentDetailsResults, false, null));
_.forEach(contentDetailsResults.items, r => {
let video = _.find(videoResults, { id: r.id });
if (r.id) {
video.contentDetails = r.contentDetails;
finalSearchResults.push(video);
}
});
console.log(util.inspect(finalSearchResults, false, null));
} else {
console.log(err);
}
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment