Created
August 16, 2017 06:07
-
-
Save ns23/e90f9787b879445dddb64e403f778fab to your computer and use it in GitHub Desktop.
JavaScript-Retrieve all videos from YouTube playlist using youtube v3 API
This file contains hidden or 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
let Youtube = require('./youtube'); | |
let playlist1 = new Youtube(); | |
let listId = 'PL6gx4Cwl9DGBMdkKFn3HasZnnAqVjzHn_'; | |
playlist1.parsePlaylist(listId).then(function(res) { | |
console.log(res); //list of videos in youtue playlist | |
}).catch(function(error) { | |
console.log(error); | |
}); |
This file contains hidden or 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
module.exports = function() { | |
const https = require('https'); | |
const YOUR_API_KEY = ''; | |
/** | |
* Builds a query string for making youtube API call | |
* | |
* @param {any} url | |
* @param {any} params | |
* @returns queryString | |
*/ | |
function buildQueryString(url, params) { | |
let queryString = url + '?'; | |
for (let key in params) { | |
if (params.hasOwnProperty(key)) { | |
queryString += key + '=' + params[key] + '&'; | |
queryString += (key === 'key') ? '' : '&'; | |
} | |
} | |
return queryString; | |
} | |
/** | |
* Makes a get request to url | |
* | |
* @param {any} apiUrl | |
* @returns a promise | |
*/ | |
function getRequest(apiUrl) { | |
let parsed; | |
return new Promise((resolve, reject) => { | |
https.get(apiUrl, function(res) { | |
let body = ''; // Will contain the final response | |
// Received data is a buffer. | |
// Adding it to our body | |
res.on('data', function(data) { | |
body += data; | |
}); | |
// After the response is completed, parse it and log it to the console | |
res.on('end', function() { | |
parsed = JSON.parse(body); | |
resolve(parsed); | |
}); | |
}) | |
// If any error has occured, log error to console | |
.on('error', function(e) { | |
reject(e.message); | |
console.log('Got error: ' + e.message); | |
}); | |
}); | |
} | |
/** | |
* Finds number of videos & videos | |
* information in the playlist | |
* | |
* @param {any} id | |
* @param {any} [pageToken=null] | |
* @returns onject containing playlist information | |
*/ | |
function parsePlaylist(id, pageToken = null) { | |
let result; //contains list of all vidos in playlist | |
let apiUrl = 'https://www.googleapis.com/youtube/v3/playlistItems'; | |
let params = { | |
'playlistId': id, | |
'maxResults': 50, | |
'pageToken': (pageToken == null) ? '' : pageToken, | |
'part': 'snippet,contentDetails', | |
'key': YOUR_API_KEY, | |
}; | |
let queryString = buildQueryString(apiUrl, params); | |
console.log(queryString); | |
let numOfReq = 1; | |
return new Promise((resolve, reject) => { | |
getRequest(queryString).then((res) => { | |
if (numOfReq > 1) { | |
for (let myKey in res.items) { | |
if (res.items.hasOwnProperty(myKey)) { | |
result.items.push(res.items[myKey]); | |
} | |
} | |
numOfReq = numOfReq + 1; | |
} else { | |
result = res; | |
resolve(result); | |
} | |
//till all videos are retrived | |
if (res.hasOwnProperty('nextPageToken')) { | |
parsePlaylist(id, res.nextPageToken); | |
} | |
}).catch((err) => { | |
console.log(err); | |
reject(err); | |
}); | |
}); | |
} | |
return { | |
parsePlaylist: parsePlaylist, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment