-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlGenerator.js
85 lines (67 loc) · 2.09 KB
/
urlGenerator.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
"use strict";
// Optionen:
// Limit
// Market
// Genres
/* urlOptions = {
limit: 10,
market: "US",
genre_seeds: [
"Pop",
"Rock",
Disney
]
} */
/** URL Generator for the Spotify Endpoint
*
* Converts given properties into a single string,
* attached to the base url of the endpoint
*
* Returns the generated as a single string
* which can be used to get data from the Spotify API
*
* @param {List of specified musicPreferences} musicPreferences
* @param {*} callback
*/
const generateUrl = function(musicPreferences, options, callback) {
if (!callback) {
throw new Error("Callback is missing");
}
//Checking if function is invoked properly
if (!musicPreferences) {
const musicPreferences = [];
}
if (!options.genre_seeds) {
options.genre_seeds = ["pop", "rock", "classical", "country", "dance"];
}
let url = "https://api.spotify.com/v1/recommendations?";
const standardLimit = 5;
const standardMarket = "US";
if(options) {
url += `limit=${options.limit || standardLimit}`;
url += `&market=${options.market || standardMarket}`;
let genresString = "";
for (const seed of options.genre_seeds) {
genresString += `${seed}%2C`;
}
url += `&seed_genres=${genresString}`;
}
//Converts all elements of the propertie Array into a single String
for (const preference of musicPreferences) {
if (!preference["name"]) {
return callback(new Error("Name of the preference is missing"));
}
if (!preference["value"]) {
return callback(new Error("Value of the preference is missing"));
}
let suffix = `&${preference["name"]}=${preference["value"].toFixed(1)}`;
url += suffix;
};
return callback(null, url);
};
/* Kleiner Test Aufruf
const music = {name: "loudness", value: 0.9}
generateUrl([music], {limit: 6, market: "DE", genre_seeds: ["pop", "rock"]}, (err, data) => {
console.log(data);
})*/
module.exports = generateUrl;