-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
63 lines (54 loc) · 1.82 KB
/
index.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
const fetch = require('node-fetch')
exports.getWebAccessToken = async function getWebAccessToken (spDcCookie) {
const res = await fetch('https://open.spotify.com/get_access_token?reason=transport&productType=web_player', {
headers: {
Cookie: `sp_dc=${spDcCookie}`
}
})
return res.json()
}
exports.getFriendActivity = async function getFriendActivity (webAccessToken) {
// Looks like the app now uses `https://spclient.wg.spotify.com/presence-view/v1/buddylist`
// but both endpoints appear to be identical in the kind of token they accept
// and the response format.
const res = await
fetch('https://guc-spclient.spotify.com/presence-view/v1/buddylist', {
headers: {
Authorization: `Bearer ${webAccessToken}`
}
})
return res.json()
}
exports.wrapWebApi = function wrapWebApi (api) {
const Request = require('spotify-web-api-node/src/base-request')
const HttpManager = require('spotify-web-api-node/src/http-manager')
api.getWebAccessToken = function getWebAccessToken (callback) {
const { spDcCookie } = this.getCredentials()
return Request.builder()
.withHost('open.spotify.com')
.withPort(443)
.withScheme('https')
.withPath('/get_access_token')
.withQueryParameters({
reason: 'transport',
productType: 'web_player'
})
.withHeaders({
Accept: 'application/json',
Cookie: `sp_dc=${spDcCookie}`
})
.build()
.execute(HttpManager.get, callback)
}
api.getFriendActivity = function getFriendActivity (callback) {
return Request.builder()
.withHost('guc-spclient.spotify.com')
.withPort(443)
.withScheme('https')
.withAuth(this.getAccessToken())
.withPath('/presence-view/v1/buddylist')
.build()
.execute(HttpManager.get, callback)
}
return api
}