-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_client.js
205 lines (161 loc) · 6.8 KB
/
spotify_client.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
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
/**
* Created by attila on 04.03.17.
*/
const baseUrl = "https://api.spotify.com/v1/";
const market = "DE";
const GET = "GET";
var request = require('request');
var querystring = require("querystring");
var debugMode = false;
var debug = function (msg) {
if(debugMode)
console.log(msg);
};
var sendRequest = function(path, method, callback) {
console.log("- [REST] Sending " + method + " request to " + baseUrl + path + "...");
if(method === GET) {
request(baseUrl + path, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("- [REST] Successs sending " + method + " request to " + baseUrl + path);
callback(JSON.parse(body));
} else {
console.log("- [REST] Error sending " + method + " request to " + baseUrl + path + " -> \nStatus: "+response.statusCode + ": \nError: " + error + "\nBody: " + body);
}
});
}
};
/**
* @param name Name of the artist
* @param deepCallback (optional) function to carry out with the result of this method after the method
* @return NULL. if you want to use the result, use it by providing a deepCallback
*/
var getArtistId =function (name, deepCallback) {
console.log("[SPOTIFY] Setting up artist ID search for " + name);
var query = querystring.stringify({q:name,type:"artist",market:market,limit:1});
var leUrl = "search?" + query;
var callback = function(chunk) {
if (chunk.artists.items.length === 0 ){
console.log("No spotify id found for artist with name"+ name);
return;
}
var result = chunk.artists.items[0].id;
debug("Found spotify id ", result, "for artist with name "+ name);
if(deepCallback)
deepCallback(result);
return result;
};
return sendRequest(leUrl, GET, callback);
};
/**
* @param name Name of the artist
* @param artistSpotifyId (optional) spotify ID of the artist. if not given, getArtistId method is used before sending the request for this method
* @param limit amount of similar artists wanted
* @param deepCallback (optional) function to carry out with the result of this method after the method
* @return NULL. if you want to use the result, use it by providing a deepCallback
*/
var getSimilarArtists = function (name, artistSpotifyId, limit, deepCallback) {
var doIt = function (spotifyId) {
console.log("[SPOTIFY] Setting up similar artists search for " + name);
var leUrl = "artists/" + spotifyId + "/related-artists?country=" + market;
var callback = function(chunk) {
if (chunk.artists.length === 0 ){
console.log("No spotify similar artist found for artist with name"+ name);
return;
}
var result = [];
for(var i = 0; i < chunk.artists.length && i < limit; i++) {
var artist = chunk.artists[i];
result.push(artist.name);
}
debug("Found spotify similar artists ", result, "for artist with name "+ name);
if(deepCallback)
deepCallback(result);
return result;
};
return sendRequest(leUrl, GET, callback);
};
if(!artistSpotifyId) {
getArtistId(name, doIt);
} else {
doIt(artistSpotifyId);
}
};
/**
* Result is list of object with (name, url)
* @param name Name of the artist
* @param artistSpotifyId (optional) spotify ID of the artist. if not given, getArtistId method is used before sending the request for this method
* @param limit amount of top tracks wanted
* @param deepCallback (optional) function to carry out with the result of this method after the method
* @return NULL. if you want to use the result, use it by providing a deepCallback
*/
var getArtistTopTracks = function (name, artistSpotifyId, limit, deepCallback) {
var doIt = function (spotifyId) {
console.log("[SPOTIFY] Setting up artist top tracks search for " + name);
var leUrl = "artists/" + spotifyId + "/top-tracks?country=" + market;
var callback = function(chunk) {
if (chunk.tracks.length === 0 ){
console.log("No spotify top tracks found for artist with name"+ name);
return;
}
var result = [];
for(var i = 0; i < chunk.tracks.length && i < limit; i++) {
var track = chunk.tracks[i];
var artistName = "";
var sep = ", ";
for(var j = 0; j < track.artists.length; j++) {
artistName+=track.artists[j].name +sep;
}
artistName = artistName.substring(0, artistName.length-sep.length);
result.push({artist:artistName,name:track.name, url:track.external_urls.spotify});
}
debug("Found spotify top tracks ", result, "for artist with name "+ name);
if(deepCallback)
deepCallback(result);
return result;
};
return sendRequest(leUrl, GET, callback);
};
if(!artistSpotifyId) {
getArtistId(name, doIt);
} else {
doIt(artistSpotifyId);
}
};
/**
* Spotify does not really give a response if no query string is provided....
var querystring = require("querystring");
* Result is list of object with (name, url)
* @param genre Name of the genre
* @param limit amount of top tracks wanted
* @param deepCallback (optional) function to carry out with the result of this method after the method
* @return NULL. if you want to use the result, use it by providing a deepCallback
var getGenreTopTracks = function (leGenre, limit, deepCallback) {
var doIt = function () {
var escGenre = querystring.stringify({genre:"\""+leGenre+"\"", country:market, q:"*", type:"track", limit:limit});
var leUrl = "search?"+escGenre;
var callback = function(chunk) {
console.log("Found genre top tracks ", chunk, "for genre with name "+ leGenre);
if (chunk.tracks.length === 0 ){
console.log("No spotify top tracks found for artist with name"+ leGenre);
return;
}
var result = [];
for(var i = 0; i < chunk.tracks.length && i < limit; i++) {
var track = chunk.tracks[i];
result.push({name:track.name, url:track.external_urls.spotify});
}
console.log("Found spotify top tracks ", result, "for artist with name "+ leGenre);
if(deepCallback)
deepCallback(result);
return result;
};
return sendRequest(leUrl, GET, callback);
};
doIt();
};
*/
module.exports = {
getArtistId: getArtistId,
getSimilarArtists: getSimilarArtists,
getArtistTopTracks: getArtistTopTracks
};