-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
449 lines (397 loc) · 12.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
var app = angular.module('QueueTube', []);
Ladda.bind( 'button', {
callback: function( instance ) {
setTimeout(function() {
instance.stop();
},6000)
var videos = angular.element(document.getElementById('query')).scope().upcoming;
var history = angular.element(document.getElementById('query')).scope().history;
if(history.length >= 1){
videos.unshift(history[0]);
}
var apiUrl = "https://us-central1-intuitive-bot.cloudfunctions.net/mysql_demo";
postData = {"data": videos};
console.log(postData);
var saveData = $.ajax({
type: 'POST',
url: apiUrl,
data: JSON.stringify(postData),
dataType: "json",
success: function(resultData) {
instance.stop();
var finalURL = window.location.href.split('#')[0]+"#"+resultData.playlistUrl;
vex.dialog.open({
message: 'Share your playlist using the below link:',
input: [
'<input name="url" type="text" placeholder="URL" value="'+finalURL+'"/>',
].join(''),
buttons: [
$.extend({}, vex.dialog.buttons.NO, { text: 'Back' })
],
callback: function (data) {
if (!data) {
} else {
}
}
})
}
});
saveData.error(function() { alert("Something went wrong"); });
}
});
function onKeyDown(e) {
//console.log("In instant search");
}
function openAbout() {
vex.dialog.open({
message: 'QueueTube is a YouTube powered music player',
input: [
'<p>Features:</p>',
'<li>Autocomplete Instant search</li>',
'<li>Playlists (upcoming and archived videos)</li>',
'<li>Automatic play</li>',
'<li>Playlist sharing</li>',
'<li>Drag and drop playlist reordering</li>'
].join(''),
buttons: [
$.extend({}, vex.dialog.buttons.NO, { text: 'Back' })
],
callback: function (data) {
if (!data) {
console.log('Cancelled')
} else {
// console.log('Username', data.username, 'Password', data.password)
}
}
})
}
function onBodyLoad() {
currentSearch = "";
currentSuggestion = "";
currentVideoId = "";
playlistShowing = false;
playlistArr = [];
xhrWorking = false;
pendingSearch = false;
pendingDoneWorking = false;
}
function doInstantSearch() {
//console.log("In instant search");
if (xhrWorking) {
pendingSearch = true;
return;
}
var searchBox = $("#query");
if (searchBox.val() == currentSearch) {
return;
}
currentSearch = searchBox.val();
keyword = searchBox.val();
if(keyword == ''){
console.log("Blank search");
return;
}
var the_url = "https://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&jsonp=window.yt.www.suggest.handleResponse&q=" + encodeURIComponent(searchBox.val()) + "&cp=1";
$.ajax({
type: "GET",
url: the_url,
dataType: "script"
});
xhrWorking = true;
}
yt = {};
yt.www = {};
yt.www.suggest = {};
yt.www.suggest.handleResponse = function(suggestions) {
//console.log("Suggestions are:");
//console.log(suggestions);
if (suggestions[1][0]) {
var searchTerm = suggestions[1][0][0];
} else {
var searchTerm = null;
}
//console.log("Yo!!");
document.getElementById('play').innerHTML = searchTerm;
searchtermdisplay = keyword;
//$rootScope.$apply();
if (!searchTerm) {
searchTerm = keyword;
}
angular.element(document.getElementById('query')).scope().search(searchTerm);
};
function doneWorking() {
xhrWorking = false;
if (pendingSearch) {
pendingSearch = false;
doInstantSearch();
}
var searchBox = $("#searchBox");
searchBox.attr("class", "statusPlaying");
}
// Run
app.run(function () {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
});
// Config
app.config( function ($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
// Service
app.service('VideosService', ['$window', '$rootScope', '$log', function ($window, $rootScope, $log) {
var service = this;
var youtube = {
ready: false,
player: null,
playerId: null,
videoId: null,
videoTitle: null,
playerHeight: '480',
playerWidth: '640',
state: 'stopped'
};
var searchtermdisplay = 'hello';
var results = [];
var upcoming = [
{id: 'bpOSxM0rNPM', title: 'Arctic Monkeys - Do I Wanna Know? (Official Video)'},
{id: 'DQ2AJlN_ksc', title: 'Jai Wolf - Indian Summer'}
];
var history = [
];
$window.onYouTubeIframeAPIReady = function () {
$log.info('Youtube API is ready');
var searchBox = $("#query");
searchBox.keyup(doInstantSearch);
onBodyLoad();
youtube.ready = true;
service.bindPlayer('placeholder');
service.loadPlayer();
$rootScope.$apply();
};
function onYoutubeReady (event) {
$log.info('YouTube Player is ready');
if (window.location.hash) {
var searchTerm = decodeURIComponent(window.location.hash.substring(1));
var apiUrl = "https://us-central1-intuitive-bot.cloudfunctions.net/mysql_demo";
$.ajax({
url: apiUrl,
type: "get", //send it through get method
data: {
playlistUrl: searchTerm,
},
success: function(response) {
console.log(response.playlist);
var newupcoming = JSON.parse(response.playlist);
var oldupcoming = service.getUpcoming().slice();
for (var i = 0; i <= oldupcoming.length - 1; i++) {
service.deleteVideo('upcoming', oldupcoming[i].id);
}
for (var i = 1; i <= newupcoming.length - 1; i++) {
service.queueVideo(newupcoming[i].id, newupcoming[i].title);
}
service.launchPlayer(newupcoming[0].id, newupcoming[0].title);
service.archiveVideo(newupcoming[0].id, newupcoming[0].title);
var el = document.getElementById('upcoming');
var sortable = Sortable.create(el, {onEnd: function (/**Event*/evt) {
upcoming.splice(evt.newIndex, 0, upcoming.splice(evt.oldIndex, 1)[0]);
}});
// service.deleteVideo('upcoming', newupcoming[0].id);
},
error: function(xhr) {
alert("No playlist found");
}
});
}
else{
var randomNumber = Math.floor(Math.random() * upcoming.length);
var firstVideo = upcoming[randomNumber];
service.launchPlayer(firstVideo.id, firstVideo.title);
service.archiveVideo(firstVideo.id, firstVideo.title);
service.deleteVideo('upcoming', firstVideo.id);
//event.target.playVideo();
var el = document.getElementById('upcoming');
var sortable = Sortable.create(el, {onEnd: function (/**Event*/evt) {
upcoming.splice(evt.newIndex, 0, upcoming.splice(evt.oldIndex, 1)[0]);
}});
}
}
function onYoutubeStateChange (event) {
if (event.data == YT.PlayerState.PLAYING) {
youtube.state = 'playing';
} else if (event.data == YT.PlayerState.PAUSED) {
youtube.state = 'paused';
} else if (event.data == YT.PlayerState.ENDED) {
youtube.state = 'ended';
service.launchPlayer(upcoming[0].id, upcoming[0].title);
service.archiveVideo(upcoming[0].id, upcoming[0].title);
service.deleteVideo('upcoming', upcoming[0].id);
}
$rootScope.$apply();
}
function onPlayerError (event) {
}
this.bindPlayer = function (elementId) {
$log.info('Binding to ' + elementId);
youtube.playerId = elementId;
};
this.createPlayer = function () {
$log.info('Creating a new Youtube player for DOM id ' + youtube.playerId + ' and video ' + youtube.videoId);
return new YT.Player(youtube.playerId, {
height: youtube.playerHeight,
width: youtube.playerWidth,
playerVars: {
rel: 0,
showinfo: 0
},
events: {
'onReady': onYoutubeReady,
'onStateChange': onYoutubeStateChange,
'onError': onPlayerError
}
});
};
this.loadPlayer = function () {
if (youtube.ready && youtube.playerId) {
if (youtube.player) {
youtube.player.destroy();
}
youtube.player = service.createPlayer();
}
};
this.launchPlayer = function (id, title) {
youtube.player.loadVideoById(id);
youtube.videoId = id;
youtube.videoTitle = title;
return youtube;
}
this.listResults = function (data) {
results.length = 0;
for (var i = 0; i <= data.items.length - 1; i++) {
results.push({
id: data.items[i].id.videoId,
title: data.items[i].snippet.title,
description: data.items[i].snippet.description,
thumbnail: data.items[i].snippet.thumbnails.default.url,
author: data.items[i].snippet.channelTitle
});
}
return results;
}
this.queueVideo = function (id, title) {
upcoming.push({
id: id,
title: title
});
return upcoming;
};
this.archiveVideo = function (id, title) {
history.unshift({
id: id,
title: title
});
return history;
};
this.deleteVideo = function (list, id) {
if(list === 'upcoming'){
list = upcoming;
}
else{
list = history;
}
for (var i = list.length - 1; i >= 0; i--) {
if (list[i].id === id) {
list.splice(i, 1);
break;
}
}
};
this.getYoutube = function () {
return youtube;
};
this.getResults = function () {
return results;
};
this.getUpcoming = function () {
return upcoming;
};
this.setUpcoming = function (newupcoming) {
upcoming = newupcoming;
};
this.getHistory = function () {
return history;
};
}]);
// Controller
app.controller('VideosController', function ($scope, $http, $log, VideosService) {
init();
function init() {
$scope.youtube = VideosService.getYoutube();
$scope.results = VideosService.getResults();
$scope.upcoming = VideosService.getUpcoming();
$scope.history = VideosService.getHistory();
$scope.playlist = true;
}
function doYoutubeSearch(keyword) {
$http.get('https://www.googleapis.com/youtube/v3/search', {
params: {
key: 'AIzaSyBWFzUEFKIjPnaxlSN4UTNMmUV_09gbu6I',
type: 'video',
maxResults: '8',
part: 'id,snippet',
fields: 'items/id,items/snippet/title,items/snippet/description,items/snippet/thumbnails/default,items/snippet/channelTitle',
q: keyword
}
})
.success( function (data) {
VideosService.listResults(data);
$log.info(data);
doneWorking();
})
.error( function () {
$log.info('Search error');
});
}
$scope.launch = function (id, title) {
VideosService.launchPlayer(id, title);
VideosService.archiveVideo(id, title);
VideosService.deleteVideo('upcoming', id);
$log.info('Launched id:' + id + ' and title:' + title);
};
$scope.queue = function (id, title) {
VideosService.queueVideo(id, title);
VideosService.deleteVideo('history', id);
$log.info('Queued id:' + id + ' and title:' + title);
};
$scope.delete = function (list, id) {
//console.log("in delete with " + list + " " + id);
VideosService.deleteVideo(list, id);
};
$scope.changed = function (event) {
//console.log("haha");
};
$scope.search = function (keyword) {
$http.get('https://www.googleapis.com/youtube/v3/search', {
params: {
key: 'AIzaSyBWFzUEFKIjPnaxlSN4UTNMmUV_09gbu6I',
type: 'video',
maxResults: '8',
part: 'id,snippet',
fields: 'items/id,items/snippet/title,items/snippet/description,items/snippet/thumbnails/default,items/snippet/channelTitle',
q: keyword
}
})
.success( function (data) {
VideosService.listResults(data);
$log.info(data);
doneWorking();
})
.error( function () {
$log.info('Search error');
});
}
$scope.tabulate = function (state) {
$scope.playlist = state;
}
});