Ver Fonte

Added Playlist Importing

theflametrooper há 8 anos atrás
pai
commit
bef54df27d

+ 32 - 2
backend/logic/actions/playlists.js

@@ -8,7 +8,7 @@ const hooks = require('./hooks');
 const async = require('async');
 const playlists = require('../playlists');
 
-module.exports = {
+let lib = {
 
 	indexForUser: (session, createdBy, cb) => {
 		db.models.playlist.find({ createdBy }, (err, playlists) => {
@@ -112,6 +112,34 @@ module.exports = {
 			else return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
 		});
 	},
+	
+	addSetToPlaylist: (session, url, playlistId, cb) => {
+		async.waterfall([
+			(next) => {
+				utils.getPlaylistFromYouTube(url, songs => {
+					next(null, songs);
+				});
+			},
+			(songs, next) => {
+				for (let s = 0; s < songs.length; s++) {
+					lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, (res) => {});
+				}
+				next(null);
+			},
+			(next) => {
+				db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
+					if (err) throw err;
+
+					next(null, playlist);
+				});
+			}
+		],
+		(err, playlist) => {
+			if (err) return cb({ status: 'error', message: err });
+			else return cb({ status: 'success', message: 'Playlist has been successfully added', data: playlist.songs });
+		});
+	},
+
 
 	removeSongFromPlaylist: (session, songId, playlistId, cb) => {
 		db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
@@ -207,4 +235,6 @@ module.exports = {
 		});
 	}
 
-};
+};
+
+module.exports = lib;

+ 23 - 0
backend/logic/utils.js

@@ -252,6 +252,29 @@ module.exports = {
 			cb(song);
 		});
 	},
+	getPlaylistFromYouTube: (url, cb) => {
+		
+		let name = 'list'.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
+		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('&');
+
+		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);
+			cb(body.items);
+		});
+	},
 	getSongFromSpotify: (song, cb) => {
 		const spotifyParams = [
 			`q=${encodeURIComponent(song.title)}`,

+ 8 - 3
frontend/components/Modals/Playlists/Edit.vue

@@ -51,10 +51,10 @@
 				</table>
 				<div class='control is-grouped'>
 					<p class='control is-expanded'>
-						<input class='input' type='text' placeholder='YouTube Playlist URL'>
+						<input class='input' type='text' placeholder='YouTube Playlist URL' v-model='importQuery'>
 					</p>
 					<p class='control'>
-						<a class='button is-info' @click='submitQuery()'>Import</a>
+						<a class='button is-info' @click='importPlaylist()'>Import</a>
 					</p>
 				</div>
 				<h5>Edit playlist details:</h5>
@@ -89,7 +89,6 @@
 		data() {
 			return {
 				playlist: {},
-				songQuery: '',
 				songQueryResults: []
 			}
 		},
@@ -119,6 +118,12 @@
 					}
 				});
 			},
+			importPlaylist: function () {
+				let _this = this;
+				this.socket.emit('playlists.addSetToPlaylist', _this.importQuery, _this.playlist._id, res => {
+					if (res.status == 'success') _this.playlist.songs = res.data;
+				});
+			},
 			removeSongFromPlaylist: function (id) {
 				let _this = this;
 				this.socket.emit('playlists.removeSongFromPlaylist', id, _this.playlist._id, res => {