-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.js
50 lines (39 loc) · 1.63 KB
/
Search.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
const request = require('request');
class ScSearcher {
constructor() {
this.client_id = null;
}
init(client_id) {
this.client_id = client_id;
}
getTracks(search, limit, callback) {
return new Promise((res, rej) => {
if(typeof search != 'string') throw 'Seach term is not type of string';
if(isNaN(limit)) throw 'Not a number';
if(limit > 100 || limit < 1) throw 'Limit must be between 1 and 100';
if(!this.client_id) throw 'Must set client id with init() first';
var searchURL = 'https://api-v2.soundcloud.com/search/tracks?q=SEARCH_TERM&client_id=CLIENT_ID_TERM&limit=LIMIT_TERM';;
var spaces = search.split(' ');
for(var i=0; i < spaces.length; i++) {
search = search.replace(' ', '%20');
}
searchURL = searchURL.replace('CLIENT_ID_TERM', this.client_id).replace('SEARCH_TERM', search).replace('LIMIT_TERM', limit);
const promise = new Promise((_res, _rej) =>
request({url: searchURL}, function (error, response, body) {
if(error) return _rej(new Error(error));
if (!error && response.statusCode === 200){
return _res({
body: JSON.parse(body)
});
}
})
);
promise.then(response => {
const body = response.body;
const track_list = body.collection;
return res(track_list);
});
});
}
}
module.exports = ScSearcher;