-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.js
63 lines (58 loc) · 1.87 KB
/
spotify.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
/* jslint node: true, nomen: true*/
'use strict'
var Promise = require('promise')
var config = require('./config')
var SpotifyWebApi = require('spotify-web-api-node')
module.exports = {}
function newApi(refreshToken) {
var api = new SpotifyWebApi({
clientId: config.client_id,
clientSecret: config.client_secret,
redirectUri: config.app_url + '/callback',
refreshToken: refreshToken
})
api.refresh = function() {
return new Promise(function(resolve, reject) {
api.refreshAccessToken().then(function(data) {
api.setAccessToken(data.body.access_token)
resolve()
}, function(err) {
console.log('Could not refresh access token', err)
reject()
})
})
}
function loadPart(fn, args, page, loaded) {
return api[fn].apply(api, args).then(function(response) {
loaded = loaded.concat(response.body.items)
if (response.body.next !== null) {
page.offset += page.limit
console.log('page ', page.offset, '/', response.body.total)
return loadPart(fn, args, page, loaded)
} else {
return loaded
}
})
}
api.all = function(fn, args, limit) {
var page = {
offset: 0,
limit: limit || 50
}
args.push(page)
return loadPart(fn, args, page, []).then(function(loaded) {
return loaded
})
}
api.filter = function(fn, args, predicate, limit) {
return api.all(fn, args, limit).then(function(found) {
return found.filter(predicate)
})
}
return api
};
module.exports.newApi = newApi
module.exports.userApi = function(user) {
return newApi(user.refreshToken)
}
module.exports.rootApi = newApi(config.refresh_token)