This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.coffee
138 lines (119 loc) · 3.94 KB
/
spotify.coffee
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
# @file spotify.coffee
# @brief Interface between the app and the Spotify API
# @author Justin Gallagher, justingallag@gmail.com
# @since 20 Jun 2015
spotify = {}
request = require 'request'
# @brief Retrieve the userID and display name of a user.
# @param oAuthToken: the oAuthToken needed to authenticate the request.
# @param next: Callback function.
spotify.getMe = (oAuthToken, next) ->
request.get 'https://api.spotify.com/v1/me',
auth:
bearer: oAuthToken
json: true
, (err, res, body) ->
if err?
return next(err, null)
values = {}
values.id = body.id
values.display_name = body.display_name
if body.images.length > 0
values.image = body.images[0].url
next(null, values)
# @brief Retrieve a list of playlist ids owned by the user. Will paginate
# through all of the requests to return a full list.
# @param oAuthToken: the oAuthToken needed to authenticate the request.
# @param userId: Id of user to retrieve playlists for.
# @param next: Callback function.
spotify.getPlaylists = (oAuthToken, userId, next) ->
values = {}
values.playlists = []
repeat = (url, next) ->
request.get url,
auth:
bearer: oAuthToken
json: true
, (err, res, body) ->
if err?
return next(err, null)
items = []
if body.items?
items = body.items.map (p) ->
result = {}
result.id = p.id
result.name = p.name
result.owner = p.owner.id
return result
values.playlists = values.playlists.concat items
if body.next?
repeat body.next, next
else
next null, values
repeat "https://api.spotify.com/v1/users/#{ userId }/playlists?limit=50", next
# @brief Retrieve current songs from a playlist.
# @param oAuthToken: the oAuthToken needed to authenticate the request.
# @param playlistId: Id of playlist to retrieve songs for.
# @param next: Callback function.
spotify.getSnapshot = (oAuthToken, userId, playlistId, next) ->
values = {}
values.songs = []
repeat = (url, next) ->
request.get url,
auth:
bearer: oAuthToken
json: true
, (err, res, body) ->
if err?
return next(err, null)
items = []
if body.items?
items = body.items.map (p) ->
result = {}
result.id = p.track.id
result.name = p.track.name
result.artist = p.track.artists.map (a) -> a.name
.join ", "
return result
values.songs = values.songs.concat items
if body.next?
repeat body.next, next
else
next null, values
repeat "https://api.spotify.com/v1/users/#{ userId }/playlists/#{ playlistId }/tracks?limit=100", next
# @brief Deletes all songs from a playlist and adds the given list of songs.
# @param oAuthToken: the oAuthToken needed to authenticate the request.
# @param playlistId: Id of playlist to set.
# @param songs: Songs to write to playlist.
# @param next: Callback function.
spotify.setSnapshot = (oAuthToken, userId, playlistId, songs, next) ->
chunk = (arr) -> arr.slice i, i + 100 for i in [0..arr.length] by 100
songs = chunk songs.map((i) -> "spotify:track:#{i}")
songs.reverse()
s = songs.pop()
request
method: 'PUT'
auth:
bearer: oAuthToken
json:
uris: s
uri: "https://api.spotify.com/v1/users/#{ userId }/playlists/#{ playlistId }/tracks"
, (err, res, body) ->
if err?
return next(err, null)
if songs.length == 0
return next null
recurse = () ->
if songs.length == 0
return next null
s = songs.pop()
request
method: 'POST'
auth:
bearer: oAuthToken
json:
uris: s
uri: "https://api.spotify.com/v1/users/#{ userId }/playlists/#{ playlistId }/tracks"
, recurse
recurse()
module.exports = spotify