-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_music.js
341 lines (229 loc) · 9.6 KB
/
spotify_music.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
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
const redirect_uri = 'http://www.example.com/callback'; // Your redirect uri
const SpotifyWebApi = require('spotify-web-api-node');
const fs = require('fs');
let credentials = fs.readFileSync('clientCredentials.json');
var credentials_dict = JSON.parse(credentials);
const client_id = credentials_dict["client_id"]
const client_secret = credentials_dict["client_secret"];
const musicbutton = document.getElementById('music_button');
musicbutton.addEventListener('click', engageMusicPlayer);
document.getElementById("searchbar").addEventListener('change', spotifySearch);
searchInit = false;
function engageMusicPlayer()
{
authenticateSpotify();
launchNcspot();
//createSearch(); //might want a flag to check if this has been clicked before
//musicbutton.classList.remove("music-off");
//musicbutton.classList.add("music-on");
//
//musicbutton.removeEventListener('click',engageMusicPlayer);
//musicbutton.addEventListener('click',disengageMusicPlayer);
//document.getElementById("searchbar").removeEventListener('click', shiftUIup);
//musicbutton.addEventListener('click',shiftUIdown);
}
function engageUI()
{
//this is used as callback once ncspot is fully launched
console.log("engaging UI");
Keyboard.init();
displaySearch(); // displays the search bar
musicButtonOn(); // spins the music button and changes it to an X
shiftUI();
}
function disengageMusicPlayer()
{
ytWindow.close();
musicbutton.classList.remove("music-on");
musicbutton.classList.add("music-off");
musicbutton.style.backgroundImage = "url('images/play_button.png')";
musicbutton.removeEventListener('click',disengageMusicPlayer);
musicbutton.addEventListener('click',engageMusicPlayer);
}
async function launchNcspot()
{
//here we make sure the session is always dead before launching in order to avoid duplicates
const command = "lxterminal -- sh -c 'tmux kill-session -t ncspot_session; tmux new-session -s ncspot_session'; sleep 1; tmux send-keys -t ncspot_session ncspot; tmux send-keys -t ncspot_session Enter"
const { exec } = require ('child_process');
await exec (command,{shell:true}, (error, stdout, stderr) => {
//this is our callback funciton that only engages once ncspot is fully launched
engageUI();
});
return
}
function shiftUIdown()
{ //shifts UI when the X is clicked
var spotifyWrap = document.getElementsByClassName('spotifyWrap')[0];
var clock = document.getElementsByClassName('clock')[0];
var ui_buttons = document.getElementsByClassName('UI_buttons')[0];
var keyboard = document.getElementsByClassName('keyboard')[0];
keyboard.classList.add("keyboard--down");
keyboard.classList.remove("keyboard--up");
clock.classList.remove("clock-up");
spotifyWrap.classList.remove("spotifyWrap-up");
ui_buttons.classList.remove("UI_buttons-up");
clock.classList.add("clock-down");
spotifyWrap.classList.add("spotifyWrap-down");
ui_buttons.classList.add("UI_buttons-down");
}
function shiftUI()
{ //shifts UI when the music button is clicked but before there is input
var spotifyWrap = document.getElementsByClassName('spotifyWrap')[0];
var clock = document.getElementsByClassName('clock')[0];
var ui_buttons = document.getElementsByClassName('UI_buttons')[0];
var keyboard = document.getElementsByClassName('keyboard')[0];
clock.classList.remove("clock-down");
spotifyWrap.classList.remove("spotifyWrap-down");
ui_buttons.classList.remove("UI_buttons-down");
keyboard.classList.remove("keyboard--down");
ui_buttons.classList.add("UI_buttons-up");
clock.classList.add("clock-up");
spotifyWrap.classList.add("spotifyWrap-up");
keyboard.classList.add("keyboard--up");
}
function musicButtonOn()
{
musicbutton.classList.remove("music-off");
musicbutton.classList.add("music-on");
musicbutton.style.backgroundImage = "url('images/cancel.jpg')";
}
function spotifySearch()
{
let searchString = document.getElementById("searchbar").value;
//clearing the results between each run:
let searchResults = document.getElementsByClassName('searchResults')[0];
searchResults.innerHTML='';
const command = "tmux send-keys -t ncspot_session f2; tmux send-keys -t ncspot_session "+searchString+"; tmux send-keys -t ncspot_session Enter"
const { exec } = require ('child_process');
let child= exec (command,{shell:true});
//let child= exec (command2,{shell:true});
child.stdout.on('data',
function (data) {
console.log('command output: ' + data);
});
child.stderr.on('data', function (data) {
//throw errors
console.log('stderr: ' + data);
});
child.on('close', function (code) {
console.log('child process exited with code ' + code);
});
console.log("Search term: "+searchString);
//we write the search results to our array
//search_results.fill(""); to empty out the array
//we wait 1 second for search results to load up -> eventually we should just have a regular wait loop of some sort polling
setTimeout(displayClipboardContents, 1000);
spotifyApiSearch();
}
async function pull_and_draw_text(list_pos)
{
var text = await navigator.clipboard.readText();
console.log('Text that will be used from iteration: '+list_pos+" is: "+text);
//create the div with the search resultss
let searchResults = document.getElementsByClassName('searchResults')[0];
let searchResult = document.createElement('div');
searchResults.appendChild(searchResult);
searchResult.setAttribute('height','15%');
let top_margin = 50+15*list_pos;
top_margin = top_margin+"%"
searchResult.setAttribute('top',top_margin);
searchResult.setAttribute('position','absolute');
searchResult.innerHTML = text;
return
}
function createResultDiv(text, list_pos)
{
let searchResults = document.getElementsByClassName('searchResults')[0];
let searchResult = document.createElement('div');
searchResults.appendChild(searchResult);
searchResult.setAttribute('height','15%');
let top_margin = 50+15*list_pos;
top_margin = top_margin+"%"
searchResult.setAttribute('top',top_margin);
searchResult.setAttribute('position','absolute');
searchResult.innerHTML = text;
searchResult.onclick = function(){playSong(list_pos);}
}
async function playSong(list_pos)
{
console.log("Playing the: "+list_pos+" song.")
var command = "tmux send-keys -t ncspot_session Home;"
//command = command + "tmux send-keys -t ncspot_session Down;"
for (var i =0;i<list_pos+1;i++)
{
command = command + "tmux send-keys -t ncspot_session Down;"
}
command = command + "tmux send-keys -t ncspot_session Enter"
const { exec } = require ('child_process');
await exec (command,{shell:true});
}
async function fetchSingleSearchResult(list_pos)
{//tmux send-keys -t ncspot_session Home;
var command = "tmux send-keys -t ncspot_session Home;"
//command = command + "tmux send-keys -t ncspot_session Down;"
for (var i =0;i<list_pos;i++)
{
command = command + "tmux send-keys -t ncspot_session Down;"
}
command = command + "tmux send-keys -t ncspot_session C-x"
const { exec } = require ('child_process');
await exec (command,{shell:true});
return
}
function displaySearch() {
//this function is executed when the user clicks on the search bar to type in a search term
document.getElementsByClassName('spotifyWrap')[0].style.display = "block";
}
function hideSearch()
{
document.getElementsByClassName('spotifyWrap')[0].style.display = "none";
}
function authenticateSpotify()
{
// Create the api object with the credentials
spotifyApi = new SpotifyWebApi({
redirectUri: 'https://example.com/callback',
clientId: client_id,
clientSecret: client_secret
});
// Retrieve an access token.
spotifyApi.clientCredentialsGrant().then(
function(data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
},
function(err) {
console.log('Something went wrong when retrieving an access token', err);
}
);
}
songObjects=Array(3);
// Search tracks whose name, album or artist contains 'Love'
function spotifyApiSearch()
{
let searchString = document.getElementById("searchbar").value;
spotifyApi.searchTracks(searchString)
.then(function(data) {
console.log('Success, when searching by: '+searchString);
console.log(data.body)
for(var i = 0; i < 3; i++) {
//here we build our Song Object and add it to the right index of our array
//note - we do not append/push because we dont want to grow the array, but rather keep the same length
var songObj = {name:data.body.tracks.items[i].name,
artist: data.body.tracks.items[i].artists[0].name,
uri: data.body.tracks.items[i].uri};
songObjects[i] = songObj;
var result_string = songObj.name + " - " + songObj.artist;
createResultDiv(result_string, i);
//console.log("Result from spotify api: "+result_string)
}
}
, function(err) {
console.log('Error, when searching by: '+searchString);
console.error(err);
});
}