-
Notifications
You must be signed in to change notification settings - Fork 5
/
backend.js
171 lines (100 loc) · 4.45 KB
/
backend.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
var firebase = require("firebase");
var SpotifyWebApi = require('spotify-web-api-node');
var config = {
apiKey: "AIzaSyCp9HMB336Zfcrl5spkAy4-w1nsrOe5wU0",
authDomain: "jamq-b015a.firebaseapp.com",
databaseURL: "https://jamq-b015a.firebaseio.com",
storageBucket: "jamq-b015a.appspot.com"
};
firebase.initializeApp(config);
var database = firebase.database();
var rootref = database.ref('/parties/');
// When a new room is added, and for all existing rooms at start
rootref.on('child_added', function (snapshot) {
// console.log(snapshot.val());
// console.log(snapshot.key);
// console.log("room added");
// Begin watching that room
checkroom(snapshot.key)
})
function checkroom(roomnumber) {
var roomref = database.ref('/parties/' + roomnumber);
roomref.once('value').then(function (snapshot) {
var room = snapshot.toJSON();
if ('spotifytoken' in room) {
console.log("Room " + roomnumber + " spotify enabled");
createplaylist(roomnumber, room.spotifytoken);
watchroom(roomnumber, room.spotifytoken);
} else {
console.log("Room " + roomnumber + " is not spotify enabled");
}
});
}
function watchroom(roomnumber, spotifytoken) {
console.log("Watching room: " + roomnumber);
var songsref = database.ref('/parties/' + roomnumber + '/songlist/');
songsref.on('child_added', function (snapshot) {
var spotify = new SpotifyWebApi();
spotify.setAccessToken(spotifytoken);
spotify.getMe()
.then(function (data) {
spotify.getUserPlaylists(data.body.id)
.then(function (data) {
console.log('Got playlists!');
var playlists = data.body.items;
var playlistid;
// Find the playlist we are looking for
for (var i = 0; i < playlists.length; i++) {
if (playlists[i].name == 'JamQ' + roomnumber)
playlistid = playlists[i];
}
console.log(playlistid);
console.log(snapshot.toJSON());
var track = "spotify:track:" + snapshot.toJSON().songid;
spotify.addTracksToPlaylist(playlistid.owner.id, playlistid.id, [track])
.then(function (data) {
console.log('Added tracks to playlist!');
}, function (err) {
console.log('Something went wrong in watchroom!', err);
});
}, function (err) {
console.log('Something went wrong!', err);
});
}, function (err) {
console.log('Something went wrong!', err);
});
});
}
function createplaylist(roomnumber, spotifytoken) {
var spotify = new SpotifyWebApi();
spotify.setAccessToken(spotifytoken);
let exists = false;
spotify.getMe()
.then(function (data) {
console.log('Some information about the authenticated user', data.body);
// Get all user playlists
spotify.getUserPlaylists(data.body.id)
.then(function (data) {
console.log('Got playlists!');
var playlists = data.body.items;
// Check if we already created this playlist
for (var i = 0; i < playlists.length; i++) {
if (playlists[i].name == 'JamQ' + roomnumber)
exists = true;
}
}, function (err) {
console.log('Something went wrong!', err);
}).then(function () {
//If we have not created a playlist for this room create one
if (!exists)
spotify.createPlaylist(data.body.id, 'JamQ' + roomnumber, { 'public': true })
.then(function (data) {
console.log('Created playlist!');
}, function (err) {
console.log('Something went wrong!', err);
});
});
}, function (err) {
console.log('Something went wrong!', err);
});
}