-
Notifications
You must be signed in to change notification settings - Fork 0
/
tequilaAndLights.js
168 lines (140 loc) · 4.87 KB
/
tequilaAndLights.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
/**
//Start bpm updater
clearInterval(refresher);
tal.updateBPM(access_token, track_id, changeSongDetails);
refresher = setInterval(function () {
tal.updateBPM(access_token, track_id, changeSongDetails);
}, 5000);
*/
const request = require("request");
const tequila_uri = "spotify:track:5gJKsGij5oGt5H5RSFYXPa";
const bpmIntervalTime = 5000;
let tequilaIntervalBase = 45 * 60 * 1000;
let tequilaIntervalMax = 45 * 60 * 1000;
let bpmInterval;
let tequilaInterval;
let beats, track;
let context, track_id, track_uri, name, artists;
module.exports = {
run: run,
reset: function() {
clearInterval(bpmInterval);
clearInterval(tequilaInterval);
},
setIntervalTimes: function(base, max) {
tequilaIntervalBase = base * 60 * 1000;
tequilaIntervalMax = max * 60 * 1000;
console.log("Base: ", base);
console.log("Max: ", max);
},
testTequila: function(spotifyApi) {
tequilaTime(spotifyApi);
let newTime = tequilaIntervalBase + Math.random() * tequilaIntervalMax;
console.log("First Tequila time in " + Math.floor(newTime / 60000) + " mins");
setTimeout(function() {
tequilaTime(spotifyApi);
}, newTime);
}
}
function run(spotifyApi) {
module.exports.reset();
bpmInterval = setInterval(function() {
getSongDetails(spotifyApi);
}, bpmIntervalTime);
let newTime = tequilaIntervalBase + Math.random() * tequilaIntervalMax;
console.log("First Tequila time in " + Math.floor(newTime / 60000) + " mins");
setTimeout(function() {
tequilaTime(spotifyApi);
}, newTime);
}
function getSongDetails(spotifyApi) {
let progress_ms;
spotifyApi.getMyCurrentPlaybackState()
.then(function(data) {
if(Object.keys(data.body).length == 0) {
//console.log("No song currently playing");
return null;
}
if(track_id == data.body.item.id && track_uri == data.body.item.uri)
return null;
track_id = data.body.item.id;
track_uri = data.body.item.uri;
context = data.body.context;
name = data.body.item.name;
artists = data.body.item.artists.map(a => a.name);
progress_ms = data.body.progress_ms;
return spotifyApi.getAudioAnalysisForTrack(track_id);
}).then(function(data) {
if(data == null)
return;
beats = data.body.beats;
track = data.body.track;
//Send data to pi
request({
uri: 'http://192.168.178.31/start',
method: 'POST',
json: {
beats: beats,
track: track,
progress_ms: progress_ms
}
}, function(err, req, res) {});
console.log("Currently playing: \"" + name + "\" by " + artists.join(", "));
}).catch(function(err) {
console.log(err);
});
}
function tequilaTime(spotifyApi) {
//play the tequila song
let position_ms;
const previous_uri = track_uri;
spotifyApi.getMyCurrentPlaybackState()
.then(function(data) {
if(Object.keys(data.body).length == 0)
return Promise.reject("No Playback device available");
position_ms = data.body.progress_ms;
return spotifyApi.play({
uris: [tequila_uri]
});
}).then(function() {
//Send Tequila mode to pi
request.post({
url: "http://192.168.178.31/mode",
body: "tequila"
}, function(err, req, res) {});
return spotifyApi.seek(20000);
}).then(function() {
//Wait until Tequila is done
setTimeout(function(){
//Change back to beats mode
request.post({
url: "http://192.168.178.31/mode",
body: "beats"
}, function(err, req, res) {});
//Continue previous track
let options = context ? {
context_uri: context.uri,
offset: { "uri": previous_uri }
} : {
uris: [previous_uri]
};
if(!previous_uri)
options = {
context_uri: "spotify:playlist:37i9dQZF1DX9EM98aZosoy"
};
spotifyApi.play(options)
.then(function() {
return spotifyApi.seek(position_ms);
}).catch(function(err) {
console.log(err);
});
}, 115000);
}).catch(function(err) {
console.log(err);
});
let newTime = tequilaIntervalBase + Math.random() * tequilaIntervalMax;
setTimeout(function() {
tequilaTime(spotifyApi);
}, newTime);
console.log("Next Tequila time in " + Math.floor(newTime / 60000) + " mins");
}