Skip to content

Commit 1b357b8

Browse files
committed
Friday mode
1 parent 1aa0127 commit 1b357b8

File tree

5 files changed

+209
-17
lines changed

5 files changed

+209
-17
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
clientId=<SPOTIFY-ID>
2+
clientSecret=<SPOTIFY-SECRET>

index.html

+37-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
padding: 30px;
3131
margin: 0;
3232
text-align: center;
33-
background: #292929;
33+
/* background: #292929; */
34+
background: rgba(0, 0, 0, .5);
3435
color: #1ed760;
3536
font-size: 90px;
3637
text-transform: uppercase;
@@ -89,12 +90,32 @@
8990
color: #777777;
9091
padding: 5px 15px;
9192
}
93+
94+
.party {
95+
-webkit-animation: PartyAnimation .6s infinite;
96+
/* Safari 4+ */
97+
-moz-animation: PartyAnimation .6s infinite;
98+
/* Fx 5+ */
99+
-o-animation: PartyAnimation .6s infinite;
100+
/* Opera 12+ */
101+
animation: PartyAnimation .6s infinite;
102+
/* IE 10+, Fx 29+ */
103+
}
104+
105+
@-webkit-keyframes PartyAnimation {
106+
0% {
107+
background-color: rgb(117, 209, 63);
108+
}
109+
100% {
110+
background-color: #e50000;
111+
}
112+
}
92113
</style>
93114
</head>
94115

95116
<body>
96117

97-
<h1>
118+
<h1 class="mainHeader">
98119
Now playing
99120
</h1>
100121

@@ -138,7 +159,8 @@ <h1>
138159
time: document.querySelector('.timehead'),
139160
currentTime: document.querySelector('.currentTime'),
140161
totalTime: document.querySelector('.totalTime'),
141-
dj: document.querySelector('.dj')
162+
dj: document.querySelector('.dj'),
163+
mainHeader: document.querySelector('.mainHeader')
142164
}
143165

144166
fetch("/track")
@@ -151,6 +173,17 @@ <h1>
151173
e.albumart.src = track.albumArtURL
152174
e.totalTime.innerHTML = secondsToTime(track.duration)
153175
e.dj.innerHTML = "DJ: " + track.dj
176+
177+
// Friday
178+
if (track.isFridaySong) {
179+
e.mainHeader.innerHTML = `<img src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" /> PARTY TIME
180+
<img src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" />`
181+
182+
document.body.classList.add("party")
183+
} else {
184+
e.mainHeader.innerHTML = "Now playing"
185+
document.body.classList.remove("party")
186+
}
154187
}
155188

156189
e.time.style.width = String((track.position / track.duration) * 100) + "%"
@@ -161,6 +194,7 @@ <h1>
161194
setInterval(() => {
162195
main();
163196
}, 1000)
197+
164198
</script>
165199

166200
</html>

main.js

+68-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,86 @@
1+
require('dotenv').config()
12
const { Sonos } = require('sonos')
23
const express = require('express')
3-
const path = require("path");
4+
const path = require("path")
5+
const request = require('request')
6+
const SpotifyWebApi = require('spotify-web-api-node')
47
const app = express()
58

69
const ip = "192.168.172.240"
7-
const device = new Sonos(ip);
10+
const device = new Sonos(ip)
11+
12+
const clientId = process.env.clientId
13+
const clientSecret = process.env.clientSecret
14+
const redirectUri = 'localhost:3000'
15+
16+
const spotifyApi = new SpotifyWebApi({
17+
clientId,
18+
clientSecret,
19+
redirectUri
20+
})
21+
22+
let token = null;
23+
let fridayList = []
824

925
app.get('/', (_, res) => res.sendFile(path.join(__dirname, 'index.html')))
1026

1127
app.get('/track', async (_, res) => {
28+
getFridayList();
1229
const track = await device.currentTrack();
1330
const dj = await device.getSpotifyConnectInfo()
1431

32+
const re = /:(.+)(?=\?)/
33+
const spotifyUri = track.uri.match(re)
34+
const isFridaySong = fridayList.includes(decodeURIComponent(spotifyUri[1]))
35+
1536
res.json({
1637
...track,
17-
dj: dj.activeUser
38+
dj: dj.activeUser,
39+
isFridaySong
1840
})
1941
})
2042

43+
fetchToken = () => {
44+
return new Promise((resolve, reject) => {
45+
try {
46+
console.log("Fetch token")
47+
const authOptions = {
48+
url: 'https://accounts.spotify.com/api/token',
49+
headers: {
50+
Authorization:
51+
'Basic ' +
52+
new Buffer(clientId + ':' + clientSecret).toString('base64')
53+
},
54+
form: {
55+
grant_type: 'client_credentials'
56+
},
57+
json: true
58+
};
59+
60+
request.post(authOptions, function(error, response, body) {
61+
if (!error && response.statusCode === 200) {
62+
resolve(body.access_token)
63+
}
64+
});
65+
} catch (error) {
66+
console.log(error)
67+
reject('No tokenz')
68+
}
69+
})
70+
71+
}
72+
73+
getFridayList = async() => {
74+
if(fridayList.length > 0) return
75+
if (token == null) {
76+
token = await fetchToken()
77+
}
78+
79+
spotifyApi.setAccessToken(token);
80+
const list = await spotifyApi.getPlaylist('jonikiiskinen', '7zbIctats0MGzBO2vtPFiv')
81+
82+
fridayList = list.body.tracks.items.map(t => t.track.uri)
83+
}
84+
85+
2186
app.listen(3000, () => console.log('listening on port 3000!'))

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"dependencies": {
3+
"dotenv": "^6.0.0",
34
"express": "^4.16.3",
4-
"sonos": "^1.4.1"
5+
"node-fetch": "^2.2.0",
6+
"request": "^2.88.0",
7+
"sonos": "^1.4.1",
8+
"spotify-web-api-node": "^3.1.1"
59
}
610
}

0 commit comments

Comments
 (0)