Skip to content

Commit da93ec0

Browse files
committed
Playlist Enqueue Support (#107)
You can now enqueue entire playlists in youtube just from the URL!
1 parent 15a8024 commit da93ec0

4 files changed

Lines changed: 112 additions & 19 deletions

File tree

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ <h2><span id="hostlabel" class="label label-default"><i class="fas fa-user"></i>
186186

187187
<div class="topbotmargins">
188188
<input type="Video" style="max-width: 200px; margin-right: .5rem; margin-bottom: .5rem; float: left;" class="form-control" id="inputVideoId" placeholder="Video ID / URL">
189-
<button id="enqueueButton" onclick="enqueueVideo(roomnum)" class="btn btn-primary"><i class="fas fa-plus"></i> Add to Queue</button>
189+
<button id="enqueueButton" onclick="enqueueVideoParse(roomnum)" class="btn btn-primary"><i class="fas fa-plus"></i> Add to Queue</button>
190190
<button id="emptyButton" onclick="emptyQueue(roomnum)" class="btn btn-primary"><i class="fas fa-trash"></i> Empty Queue</button>
191191
</div>
192192
<div>
193193
<!-- <a class="ghost-button-full-color"><i class="far fa-times-circle"></i></a> -->
194-
<button id="changeButton" onclick="changeVideo(roomnum)" class="btn btn-primary"><i class="fas fa-exchange-alt"></i> Change Video</button>
194+
<button id="changeButton" onclick="changeVideoParse(roomnum)" class="btn btn-primary"><i class="fas fa-exchange-alt"></i> Change Video</button>
195195
<button id="previousButton" onclick="prevVideo(roomnum)" class="btn btn-primary"><i class="fas fa-step-backward"></i> Previous</button>
196196
<button id="nextButton" onclick="playNext(roomnum)" class="btn btn-primary"><i class="fas fa-step-forward"></i> Next</button>
197197

js/sync.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,52 @@ function idParse(videoId) {
183183
}
184184
return videoId
185185
}
186-
// QueueVideo
187-
function enqueueVideo(roomnum) {
188-
//var videoId = 'sjk7DiH0JhQ';
189-
var videoId = document.getElementById("inputVideoId").value;
190-
videoId = idParse(videoId)
191186

192-
if (videoId != "invalid") {
193-
// Actually change the video!
187+
// This parses the ID out of the video link
188+
function playlistParse(videoId) {
189+
// If user enters a full link
190+
if (videoId.includes("https://") || videoId.includes("http://") || videoId.includes(".com/")) {
191+
// Do some string processing with regex
192+
switch (currPlayer) {
193+
case 0:
194+
var myRegex = /.+&list=([A-Za-z0-9\-_]+)/g
195+
var match = myRegex.exec(videoId)
196+
if (match != null) {
197+
return match[1]
198+
}
199+
break;
200+
201+
case 1:
202+
break;
203+
204+
case 2:
205+
break;
206+
case 3:
207+
break;
208+
default:
209+
console.log("Error invalid player")
210+
}
211+
}
212+
return "invalid"
213+
}
214+
215+
function enqueueVideoParse(roomnum) {
216+
var videoId = document.getElementById("inputVideoId").value;
217+
enqueueVideo(roomnum, videoId)
218+
}
219+
220+
// QueueVideo
221+
function enqueueVideo(roomnum, rawId) {
222+
videoId = idParse(rawId)
223+
playlistId = playlistParse(rawId)
224+
225+
if (playlistId != "invalid") {
226+
socket.emit('enqueue playlist', {
227+
room: roomnum,
228+
playlistId: playlistId,
229+
user: username
230+
})
231+
} else if (videoId != "invalid") {
194232
socket.emit('enqueue video', {
195233
room: roomnum,
196234
videoId: videoId,
@@ -216,11 +254,14 @@ function emptyQueue(roomnum) {
216254
})
217255
}
218256

257+
function changeVideoParse(roomnum) {
258+
var videoId = document.getElementById("inputVideoId").value
259+
changeVideo(roomnum, videoId)
260+
}
261+
219262
// Change playVideo
220-
function changeVideo(roomnum) {
221-
//var videoId = 'sjk7DiH0JhQ';
222-
var videoId = document.getElementById("inputVideoId").value;
223-
videoId = idParse(videoId)
263+
function changeVideo(roomnum, rawId) {
264+
var videoId = idParse(rawId)
224265

225266
if (videoId != "invalid") {
226267
var time = getTime()
@@ -279,7 +320,8 @@ function loveLive(roomnum) {
279320
// rookie, russian roulette, i want you back, TT, whistle, ddu du ddu du, turtle, 24/7
280321
// something new, #cookie jar, lion heart, i will show you, bubble pop, girl front, love cherry motion, ice cream cake
281322
// stay (taeyeon), ordinary love, 11:11, SObeR, I'm so sick, heaven, genie, dinosaur
282-
// Travel
323+
// Travel, blow your mind, pop/stars, BBIBBI, gotta go, galaxy, my trouble, blue
324+
// love scenario, dance the night away, solo, some, yes or yes, when the wind blows, hi high, don't forget
283325
var video_roulette = [
284326
'97uviVyw0_o', 'tIWpr3tHzII', 'WkdtmT8A2iY', 'U7mPqycQ0tQ',
285327
'i0p1bmr0EmE', 'FzVR_fymZw4', 'eNmL4JiGxZQ', 'J_CFBjAyPWE',
@@ -291,11 +333,15 @@ function loveLive(roomnum) {
291333
'bw9CALKOvAI', 'tyInv6RWL0Q', 'VBbeuXW8Nko', 'glXgSSOKlls',
292334
'k9_XH1YibcY', 'xGav-z5yRiU', 'WLJyhhNCHi0', 'DgT4CPv_CCE',
293335
'F4oHuML9U2A', 'L9ro1KjkJMg', '6SwiSpudKWI', '8Oz7DG76ibY',
294-
'xRbPAVnqtcs'
336+
'xRbPAVnqtcs', '08ATpBqlAIk', 'UOxkGD8qRB4', 'nM0xDI5R50E',
337+
'HlN2BXNJzxA', '9U8uA702xrE', 'JRdcPhDkNYw', 'F34e6LYro-4',
338+
'vecSVX1QYbQ', 'Fm5iP0S1z9w', 'b73BI9eUkjM', 'hZmoMyFXDoI',
339+
'mAKsZ26SabQ', 'o3pOzegB-7w', '846cjX0ZTrk', 'TcytstV1_XE'
340+
295341
]
296342

297-
// Random number between 0 and 40 inclusive
298-
var random = Math.floor(Math.random() * (41))
343+
// Random number between 0 and 56 inclusive
344+
var random = Math.floor(Math.random() * (56))
299345
// Only for YouTube testing
300346
socket.emit('change video', {
301347
room: roomnum,
@@ -324,7 +370,6 @@ socket.on('getData', function(data) {
324370

325371
function changePlayer(roomnum, playerId) {
326372
if (playerId != currPlayer) {
327-
console.log("I changed!")
328373
socket.emit('change player', {
329374
room: roomnum,
330375
playerId: playerId
@@ -336,7 +381,6 @@ function changePlayer(roomnum, playerId) {
336381
function changeSinglePlayer(playerId) {
337382
return new Promise((resolve, reject) => {
338383
if (playerId != currPlayer) {
339-
console.log("I changed!")
340384
socket.emit('change single player', {
341385
playerId: playerId
342386
});

js/yt.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function play() {
9898
socket.on('get title', function(data, callback) {
9999
var videoId = data.videoId
100100
var user = data.user
101+
101102
$.get(
102103
"https://www.googleapis.com/youtube/v3/videos", {
103104
part: 'snippet',
@@ -119,3 +120,23 @@ socket.on('get title', function(data, callback) {
119120
}
120121
)
121122
})
123+
124+
socket.on('get playlist videos', function(data) {
125+
var playlistId = data.playlistId
126+
var user = data.user
127+
128+
$.get(
129+
"https://www.googleapis.com/youtube/v3/playlistItems", {
130+
part: 'snippet,contentDetails',
131+
playlistId: playlistId,
132+
maxResults: '50',
133+
key: data.api_key
134+
},
135+
function(data) {
136+
// Iterate through all of the playlist videos
137+
for (let video of data.items) {
138+
enqueueVideo(roomnum, video.contentDetails.videoId)
139+
}
140+
}
141+
)
142+
})

server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,34 @@ io.sockets.on('connection', function(socket) {
444444
}
445445
})
446446

447+
// Enqueue playlist
448+
// Gets all of the playlist videos and enqueues them
449+
// Only supported for YouTube
450+
socket.on('enqueue playlist', function(data) {
451+
if (io.sockets.adapter.rooms['room-' + socket.roomnum] !== undefined) {
452+
var user = data.user
453+
var playlistId = data.playlistId
454+
switch (io.sockets.adapter.rooms['room-' + socket.roomnum].currPlayer) {
455+
case 0:
456+
// See yt.js file
457+
socket.emit('get playlist videos', {
458+
playlistId: playlistId,
459+
user: user,
460+
api_key: YT3_API_KEY
461+
})
462+
break;
463+
case 1:
464+
break;
465+
case 2:
466+
break;
467+
case 3:
468+
break;
469+
default:
470+
console.log("Error invalid player id")
471+
}
472+
}
473+
})
474+
447475
// Empty the queue
448476
socket.on('empty queue', function(data) {
449477
if (io.sockets.adapter.rooms['room-' + socket.roomnum] !== undefined) {

0 commit comments

Comments
 (0)