|
@@ -93,6 +93,10 @@ function convertTime (duration) {
|
|
|
return (hours < 10 ? ("0" + hours + ":") : (hours + ":")) + (minutes < 10 ? ("0" + minutes + ":") : (minutes + ":")) + (seconds < 10 ? ("0" + seconds) : seconds);
|
|
|
}
|
|
|
|
|
|
+let youtubeRequestCallbacks = [];
|
|
|
+let youtubeRequestsPending = 0;
|
|
|
+let youtubeRequestsActive = false;
|
|
|
+
|
|
|
module.exports = {
|
|
|
htmlEntities: str => String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'),
|
|
|
generateRandomString: function(len) {
|
|
@@ -233,48 +237,63 @@ module.exports = {
|
|
|
return roomSockets;
|
|
|
},
|
|
|
getSongFromYouTube: (songId, cb) => {
|
|
|
- const youtubeParams = [
|
|
|
- 'part=snippet,contentDetails,statistics,status',
|
|
|
- `id=${encodeURIComponent(songId)}`,
|
|
|
- `key=${config.get('apis.youtube.key')}`
|
|
|
- ].join('&');
|
|
|
|
|
|
- request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
|
|
|
+ youtubeRequestCallbacks.push({cb: (test) => {
|
|
|
+ youtubeRequestsActive = true;
|
|
|
+ const youtubeParams = [
|
|
|
+ 'part=snippet,contentDetails,statistics,status',
|
|
|
+ `id=${encodeURIComponent(songId)}`,
|
|
|
+ `key=${config.get('apis.youtube.key')}`
|
|
|
+ ].join('&');
|
|
|
|
|
|
- if (err) {
|
|
|
- console.error(err);
|
|
|
- return next('Failed to find song from YouTube');
|
|
|
- }
|
|
|
+ request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
|
|
|
|
|
|
- body = JSON.parse(body);
|
|
|
+ youtubeRequestCallbacks.splice(0, 1);
|
|
|
+ if (youtubeRequestCallbacks.length > 0) {
|
|
|
+ youtubeRequestCallbacks[0].cb(youtubeRequestCallbacks[0].songId);
|
|
|
+ } else {
|
|
|
+ youtubeRequestsActive = false;
|
|
|
+ }
|
|
|
|
|
|
- //TODO Clean up duration converter
|
|
|
- let dur = body.items[0].contentDetails.duration;
|
|
|
- dur = dur.replace('PT', '');
|
|
|
- let duration = 0;
|
|
|
- dur = dur.replace(/([\d]*)H/, (v, v2) => {
|
|
|
- v2 = Number(v2);
|
|
|
- duration = (v2 * 60 * 60);
|
|
|
- return '';
|
|
|
- });
|
|
|
- dur = dur.replace(/([\d]*)M/, (v, v2) => {
|
|
|
- v2 = Number(v2);
|
|
|
- duration += (v2 * 60);
|
|
|
- return '';
|
|
|
- });
|
|
|
- dur = dur.replace(/([\d]*)S/, (v, v2) => {
|
|
|
- v2 = Number(v2);
|
|
|
- duration += v2;
|
|
|
- return '';
|
|
|
+ if (err) {
|
|
|
+ console.error(err);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ body = JSON.parse(body);
|
|
|
+
|
|
|
+ //TODO Clean up duration converter
|
|
|
+ let dur = body.items[0].contentDetails.duration;
|
|
|
+ dur = dur.replace('PT', '');
|
|
|
+ let duration = 0;
|
|
|
+ dur = dur.replace(/([\d]*)H/, (v, v2) => {
|
|
|
+ v2 = Number(v2);
|
|
|
+ duration = (v2 * 60 * 60);
|
|
|
+ return '';
|
|
|
+ });
|
|
|
+ dur = dur.replace(/([\d]*)M/, (v, v2) => {
|
|
|
+ v2 = Number(v2);
|
|
|
+ duration += (v2 * 60);
|
|
|
+ return '';
|
|
|
+ });
|
|
|
+ dur = dur.replace(/([\d]*)S/, (v, v2) => {
|
|
|
+ v2 = Number(v2);
|
|
|
+ duration += v2;
|
|
|
+ return '';
|
|
|
+ });
|
|
|
+
|
|
|
+ let song = {
|
|
|
+ _id: body.items[0].id,
|
|
|
+ title: body.items[0].snippet.title,
|
|
|
+ duration
|
|
|
+ };
|
|
|
+ cb(song);
|
|
|
});
|
|
|
+ }, songId});
|
|
|
|
|
|
- let song = {
|
|
|
- _id: body.items[0].id,
|
|
|
- title: body.items[0].snippet.title,
|
|
|
- duration
|
|
|
- };
|
|
|
- cb(song);
|
|
|
- });
|
|
|
+ if (!youtubeRequestsActive) {
|
|
|
+ youtubeRequestCallbacks[0].cb(youtubeRequestCallbacks[0].songId);
|
|
|
+ }
|
|
|
},
|
|
|
getPlaylistFromYouTube: (url, cb) => {
|
|
|
|
|
@@ -282,22 +301,33 @@ module.exports = {
|
|
|
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
|
|
let playlistId = regex.exec(url)[1];
|
|
|
|
|
|
- const youtubeParams = [
|
|
|
- 'part=contentDetails',
|
|
|
- `playlistId=${encodeURIComponent(playlistId)}`,
|
|
|
- `maxResults=50`,
|
|
|
- `key=${config.get('apis.youtube.key')}`
|
|
|
- ].join('&');
|
|
|
+ function getPage(pageToken, songs) {
|
|
|
+ let nextPageToken = (pageToken) ? `pageToken=${pageToken}` : '';
|
|
|
+ const youtubeParams = [
|
|
|
+ 'part=contentDetails',
|
|
|
+ `playlistId=${encodeURIComponent(playlistId)}`,
|
|
|
+ `maxResults=5`,
|
|
|
+ `key=${config.get('apis.youtube.key')}`,
|
|
|
+ nextPageToken
|
|
|
+ ].join('&');
|
|
|
|
|
|
- request(`https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`, (err, res, body) => {
|
|
|
- if (err) {
|
|
|
- console.error(err);
|
|
|
- return next('Failed to find playlist from YouTube');
|
|
|
- }
|
|
|
+ request(`https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`, (err, res, body) => {
|
|
|
+ if (err) {
|
|
|
+ console.error(err);
|
|
|
+ return next('Failed to find playlist from YouTube');
|
|
|
+ }
|
|
|
+ body = JSON.parse(body);
|
|
|
|
|
|
- body = JSON.parse(body);
|
|
|
- cb(body.items);
|
|
|
- });
|
|
|
+ songs = songs.concat(body.items);
|
|
|
+ if (body.nextPageToken) {
|
|
|
+ getPage(body.nextPageToken, songs);
|
|
|
+ } else {
|
|
|
+ console.log(songs);
|
|
|
+ cb(songs);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ getPage(null, []);
|
|
|
},
|
|
|
getSongFromSpotify: (song, cb) => {
|
|
|
const spotifyParams = [
|