-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.js
More file actions
324 lines (286 loc) · 10.8 KB
/
Copy pathfunctions.js
File metadata and controls
324 lines (286 loc) · 10.8 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const axios = require('axios');
const { access } = require('fs');
const moment = require('moment');
const schedule = require('node-schedule');
const { get } = require('request');
const { MongoClient, Db } = require('mongodb');
async function main() {
const uri =
"mongodb+srv://doraemon:Fion2002@cluster0.kssuc.mongodb.net/myspotify?authSource=admin&replicaSet=atlas-nimc8j-shard-0&w=majority&readPreference=primary&appname=MongoDB%20Compass&retryWrites=true&ssl=true";
const client = new MongoClient(uri);
try {
await client.connect();
await addId(
client,
await findId(
"BQCIv9xu8JkbY1ko-Kry2x97fvdee1fO4RJN4XFvWKF7UisGJsK6evEas7AmcOCxM4kQ33ZabOZn5QdsStsYnLpfvi4npa_ISqXf0K6meN7Zvpbza0E7Rl0Nv-_hZQTBWu-WA7Aj7JiF-sbe333MldYeLhhOZPrVYV-nPUuwZoUAopBhN9fii4YP6ZKpIvicJx-8TN7OwkLW8nKGbOhAE0LJK-Ci3VhpV070ybyWcujqj8YPyAakujE3KwDsYg_IUBwuLqZELz7A"
) // CHANGE THE ABOVE ACCESS TOKEN TO THE ONE RECEIVED FROM THE APP.JS FILE
);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
/* $('a').click(function() { REPLACE THIS WITH THE INPUT FROM FRONT END TO STOP TRACKING
clearInterval(i);
turnOff(playlist, userRequest); INPUT FROM FRONT END SHOULD COME WITH USER REQUEST
}) */
// adding new userID's to the mongodb database
async function addId(client, newUserid) {
result = await client
.db("Playlists")
.collection("Users")
.find({ user_id: newUserid })
.toArray();
if (result.length === 0) {
client
.db("Playlists")
.collection("Users")
.insertOne({ user_id: newUserid });
console.log("Added new user ID");
} else {
console.log("User ID is already in the database");
}
}
// creates the initial playlist with all the song info
async function addSongsFirst(playlist_id, access_token) {
try {
const response = await axios({
method: "get",
url: `https://api.spotify.com/v1/playlists/${playlist_id}/tracks?market=CA&fields=items(added_at%2Ctrack(name%2Cid%2Cartists(id)))`,
headers: {
Authorization: `Bearer ${access_token}`
}
})
let allInfo = response.data;
let items = allInfo.items;
let songs = [];
let numberOfSongs = items.length;
for (let i = 0; i < numberOfSongs; i++) {
item = items[i];
let artistsItems = item.track.artists;
let artistsId = [];
for (let j = 0; j < artistsItems.length; j++) {
artistsId.push(artistsItems[j].id);
}
let seedGenres = await getGenres(artistsId, access_token);
let firstGenresString = '';
if (seedGenres.length == 0) {
firstGenresString = 'none';
} else {
firstGenresString = seedGenres[0].toString();
}
let seedTrack = items[i].track.id;
let seedArtists = artistsId;
let firstArtistString = artistsId[0].toString();
let topFive = await nextFive(seedTrack, firstGenresString, firstArtistString, access_token);
let song = {
date_added: (items[i].added_at.split("T"))[0],
song_id: seedTrack, // this is also the seed_tracks
seed_artists: seedArtists, // all seeds are in string form
seed_genres: seedGenres, // this causes the program to run slower
is_original: true,
is_archived: false,
top_five: topFive
};
songs.push(song);
}
console.log(songs);
} catch(err) {
console.log(err.response);
}
}
// checks songs routinely for dates since added
async function checkPlaylistSongs(playlistId, limit, access_token) {
let cursor = await client.db("Playlists").collection("playlist").find({ playlist_id: playlistId});
let songs = cursor.toArray().songs; // get all songs first and their info, in an array
let songsIds = [];
for (k = 0; k < songs.length; k++) {
songsIds.push(songs[i].song_id);
}
for (i = 0; i < songs.length; i++) {
existingSongs.push(songs[i].song_id); // keeps track of songs
let dateAdded = songs[i].date_added;
let diffInDays = moment().diff(moment(dateAdded, dateFormat), 'days');
if (diffInDays > limit) {
songs[i].is_archived = true;
let top_five = songs[i].top_five;
let foundSong = top_five.find(element => songsIds.includes(element)); // should prob account for if its not in the playlist
let foundSongInfo = findNewSongInfo(foundSong, access_token);
const result = await client.db("Playlists").collection("playlist").insertOne(foundSongInfo);
console.log(foundSongInfo);
try { // posts the recommended song to the playlist
const response = await axios({
method: "post",
url: `https://api.spotify.com/v1/playlists/${playlistId}/tracks?uris=spotify%3Atrack%3A${foundSongInfo.id}`,
headers: {
Authorization: `Bearer ${access_token}`
}});
} catch (err) {
console.log(err.response);
}
}
}
}
// adds a new recommended song to the playlist
async function findNewSongInfo(song_id, access_token) {
try {
const response = await axios({
method: "get",
url: `https://api.spotify.com/v1/tracks/${song_id}`,
headers: {
Authorization: `Bearer ${access_token}`
}});
let songInfo = response.data;
let artistsItems = songInfo.artists;
let artistsId = [];
for (let j = 0; j < artistsItems.length; j++) {
artistsId.push(artistsItems[j].id);
}
let seedGenres = await getGenres(artistsId, access_token);
let firstGenresString = '';
if (seedGenres.length == 0) {
firstGenresString = 'none';
} else {
firstGenresString = seedGenres[0].toString();
}
let seedTrack = song_id;
let seedArtists = artistsId;
let firstArtistString = artistsId[0].toString();
let topFive = await nextFive(seedTrack, firstGenresString, firstArtistString, access_token);
let song = {
date_added: (items[i].added_at.split("T"))[0],
song_id: seedTrack, // this is also the seed_tracks
seed_artists: seedArtists, // all seeds are in string form
seed_genres: seedGenres, // this causes the program to run slower
is_original: false, // this is false compared to an original song
is_archived: false,
top_five: topFive
}; // similar to the addSongsFirst function
const result = await client.db("Playlists").collection("playlist").insertOne(song);
return song;
} catch (err) {
console.log(err.response);
}
}
// get the genres of an array of artists for seed_artists
async function getGenres(seed_artists, access_token) {
let numArtists = seed_artists.length;
let allGenres = [];
for (i = 0; i < numArtists; i++) {
try {
const response = await axios({
method: "get",
url: `https://api.spotify.com/v1/artists/${seed_artists[i]}`,
headers: {
Authorization: `Bearer ${access_token}`
}
});
let artistResponse = response.data;
let artistGenres = artistResponse.genres;
for (j = 0; j < artistGenres.length; j++) {
allGenres.push(artistGenres[j]);
}
} catch (err) {
console.log(err.response);
}
}
let uniqueGenres = allGenres.reduce(function(a,b) {
if (a.indexOf(b) < 0) a.push(b);
return a;
}, []);
let topGenres = uniqueGenres.slice(0,5);
return topGenres;
}
// get the first 5 recommended songs based on seeds
async function nextFive(seedTracks, seedGenres, seedArtists, access_token) {
if (seedGenres != 'none') {
try {
const response = await axios({
method: "get",
url: `https://api.spotify.com/v1/recommendations?limit=5&market=CA&seed_artists=${seedArtists}&seed_genres=${seedGenres}&seed_tracks=${seedTracks}`,
headers: {
Authorization: `Bearer ${access_token}`
}
})
let allInfo = response.data;
let tracksOnly = allInfo.tracks;
let numTracks = tracksOnly.length;
let song_ids = [];
for (let i = 0; i < numTracks; i++) {
song_ids.push(tracksOnly[i].id);
};
return song_ids; // returns an array of all 5 song ids
} catch (err) {
console.log(err.response)
}
} else {
try {
const response = await axios({
method: "get",
url: `https://api.spotify.com/v1/recommendations?limit=5&market=CA&seed_artists=${seedArtists}&seed_tracks=${seedTracks}`,
headers: {
Authorization: `Bearer ${access_token}`
}
})
let allInfo = response.data;
let tracksOnly = allInfo.tracks;
let numTracks = tracksOnly.length;
let song_ids = [];
for (let i = 0; i < numTracks; i++) {
song_ids.push(tracksOnly[i].id);
};
return song_ids; // returns an array of all 5 song ids
} catch (err) {
console.log(err.response)
}
};
}
async function playlistsOn(client) {
playlistIdArray = await client
.db("Playlists")
.collection("playlist")
.find(
{ on_off: true },
{
fields: {
_id: 0,
playlist_id: 1,
},
}
)
.toArray();
let playlistIds = [];
for (i = 0; i < playlistIdArray.length; i++) {
playlistIds.push(playlistIdArray[i].playlist_id);
}
return playlistIds;
}
async function findId(access_token) {
try {
const response = await axios({
method: "get",
url: `https://api.spotify.com/v1/me`,
headers: {
Authorization: `Bearer ${access_token}`,
},
});
return response.data.id;
} catch (err) {
console.log(err.response);
}
}
// when playlist is no longer tracked
function turnOff(playlistId, userRequest) {
// simply turns off the tracking on the playlist
const result = client.db("Playlists").collection("playlist").updateOne(
{ playlist_id: playlistId },
{ $set: { "on_off": false }});
}
module.exports = {
addSongsFirst,
getGenres,
nextFive,
getplaylist,
}