Created
June 15, 2017 16:55
-
-
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
This file contains 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
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