-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
123 lines (109 loc) · 3.32 KB
/
login.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
var client_id = '98ad42fc81564a52b3bb9025563f04e7';
var client_secret = '6c3bb2e76b854cb0a21ad6aed23f9d61';
var SpotifyWebApi = require('spotify-web-api-node');
var spotifyApi = new SpotifyWebApi({
clientId: client_id,
clientSecret: client_secret
});
var recommended;
var credentials;
var deviceId;
var uri;
var title;
var artist;
var imgUrl;
var songId;
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status == 0) {
credentials = rawFile.responseText;
console.log(credentials);
}
}
}
rawFile.send(null);
}
readTextFile('validation.txt');
spotifyApi.setAccessToken(credentials);
window.onSpotifyWebPlaybackSDKReady = () => {
}
function onSpotifyPlayerAPIReady() {
player = new Spotify.Player({
name: 'Music Momentum Player',
getOauthToken: function (callback) { callback(credentials); }
});
player.on('ready', function (data) {
deviceId = data.device_id;
console.log('hi', deviceId);
setTimeout(() => {
fetch('https://api.spotify.com/v1/me/player', {
method: "PUT",
body: JSON.stringify({
device_ids:[
data.device_id
], play: false
}),
headers: {
'Authorization': `Bearer ${credentials}`
}}).catch(e => console.error(e));
}, 100);
});
player.connect().then(success => {
if (success) {
console.log('The Web Playback SDK successfully connected to Spotify!');
}
})
};
global.drawCircle = function (event) {
var canvas = document.getElementById("circlecanvas");
var context = canvas.getContext("2d");
context.canvas.width = window.innerWidth;
context.canvas.height = window.innerHeight;
x = event.pageX;
y = event.pageY;
context.fillStyle = "white";
context.beginPath();
context.arc(x, y, 10, 0, Math.PI * 2, false);
context.fill();
recommended = spotifyApi.getRecommendations(
{
limit: 10,
seed_genres: 'country'
}
).then(function(data) {
recommended = data;
randNum = Math.floor(Math.random() * 10);
console.log(recommended);
console.log(recommended.body.tracks[randNum].uri);
title = recommended.body.tracks[randNum].name;
artist = recommended.body.tracks[randNum].artists[0].name;
imgUrl = recommended.body.tracks[randNum].album.images[0].url;
uri = recommended.body.tracks[randNum].uri
songId = recommended.body.tracks[randNum].id;
console.log(title, artist, imgUrl, uri);
document.getElementById('song').innerHTML = title;
document.getElementById('artist').innerHTML = artist;
document.getElementById('album_placeholder').src= imgUrl;
function playback(id) {
let query = '/analysis?id=' + id;
console.log('test', deviceId, uri, credentials);
return fetch(query).then(e => e.json()).then(data => {
fetch(`https://api.spotify.com/v1/me/player/play${deviceId && `?device_id=${deviceId}`}`, {
method: "PUT",
body: JSON.stringify({
"uris": uri
}),
headers: {
'Authorization': `Bearer ${credentials}`
}}).catch(e => console.error(e));
});
}
playback(songId);
}, function(err) {
console.log(err);
});
}