Skip to content

Commit 8b549fa

Browse files
author
Leonard Wittrock
committed
Select device if spotify is not playing
1 parent d7a1cfe commit 8b549fa

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

common/functions.js

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ const calcNextSong = (queue) => {
77
return {newQueue: queue, newNextSong: max_votes}
88
}
99

10-
const addNextSongToSpotify = (uri, spotify_auth_token, wss, WebSocket) => {
10+
const addNextSongToSpotify = (uri, spotify_auth_token, wss, WebSocket, device_id = false) => {
1111

1212
const config = {
1313
headers: { Authorization: `Bearer ${spotify_auth_token}` }
1414
};
1515

16-
const body = {
17-
uri
18-
}
16+
17+
let body
18+
device_id ? body = {uri, device_id} : body = {uri}
1919

2020
axios.post("https://api.spotify.com/v1/me/player/queue?" + new URLSearchParams(body), {}, config).then(res => {
2121
console.log("Added song to queue")
@@ -27,20 +27,78 @@ const addNextSongToSpotify = (uri, spotify_auth_token, wss, WebSocket) => {
2727
return true
2828
}).catch(err => {
2929
console.log("added song error:")
30-
console.log(err)
31-
return false
30+
if(err.response.data.error.reason === "NO_ACTIVE_DEVICE"){
31+
getSpotifyDevices(spotify_auth_token, (device_error, device_result) => {
32+
if(device_error){
33+
return false
34+
}else{
35+
const {devices} = device_result
36+
if(devices.length < 1) return false
37+
let id
38+
let found = false
39+
for(let i = 0; i < devices.length; i++){
40+
if(devices[i].type === "Smartphone"){
41+
id = devices[i].id
42+
found = true
43+
break
44+
}
45+
}
46+
if(found) return addNextSongToSpotify(uri, spotify_auth_token, wss, WebSocket, id)
47+
48+
for(let i = 0; i < devices.length; i++){
49+
if(devices[i].type === "Computer"){
50+
id = devices[i].id
51+
found = true
52+
break
53+
}
54+
}
55+
if(found) return addNextSongToSpotify(uri, spotify_auth_token, wss, WebSocket, id)
56+
return addNextSongToSpotify(uri, spotify_auth_token, wss, WebSocket, devices[0].id)
57+
58+
}
59+
})
60+
}else{
61+
return false
62+
}
63+
3264
})
65+
66+
3367

68+
3469

3570
}
3671

3772

38-
const newAuthToken = () => {
73+
74+
const getSpotifyDevices = (spotify_auth_token, _callback) => {
75+
76+
const config = {
77+
headers: { Authorization: `Bearer ${spotify_auth_token}` }
78+
};
79+
80+
axios.get("https://api.spotify.com/v1/me/player/devices", config).then(res => {
81+
82+
console.log(res.data)
83+
_callback(false, res.data)
84+
85+
}).catch(err => {
86+
console.log("ERR1")
87+
console.log(err)
88+
if(err.response){
89+
_callback(err.response.status, undefined)
90+
}else{
91+
_callback(true, undefined)
92+
}
93+
})
94+
95+
3996

4097
}
4198

4299

43100
module.exports = {
44101
calcNextSong,
45-
addNextSongToSpotify
102+
addNextSongToSpotify,
103+
getSpotifyDevices
46104
}

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const axios = require("axios")
22
const constants = require("./common/constants")
3-
const {calcNextSong, addNextSongToSpotify} = require("./common/functions")
3+
const {calcNextSong, addNextSongToSpotify, getSpotifyDevices} = require("./common/functions")
44
const db = require("./db")
55
const WSServer = require("ws").Server
66
const {WebSocket} = require("ws")
7+
78
const server = require("http").createServer()
89
const app = require("./express-server")
910

@@ -145,6 +146,7 @@ app.get("/api/test", (req, res) => {
145146
})
146147

147148

149+
148150
setInterval(() => {
149151
pollSessions()
150152
}, 30000)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "nodemon.cmd index.js"
89
},
910
"author": "",
1011
"license": "ISC",

0 commit comments

Comments
 (0)