-
Notifications
You must be signed in to change notification settings - Fork 14
/
api.js
143 lines (120 loc) · 3.3 KB
/
api.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
(function(exports) {
var Search = function() {
}
Search.prototype.searchTracks = function(query, callback) {
var url = 'https://api.spotify.com/v1/search?type=track&q='+encodeURIComponent(query);
$.ajax(url, {
dataType: 'json',
success: function(r) {
console.log('got tracks', r);
callback(r.tracks.items);
}
});
}
var Track = function() {
}
Track.prototype.load = function(id, callback) {
var url = 'https://api.spotify.com/v1/tracks/'+encodeURIComponent(id);
$.ajax(url, {
dataType: 'json',
success: function(r) {
console.log('got track', r);
callback(r);
}
});
}
var Login = function() {
this.client_id = '';
this.access_token = '';
this.redirect = '';
}
Login.prototype.getAccessToken = function() {
return localStorage.getItem('access_token') || '';
}
Login.prototype.setAccessToken = function(access_token) {
localStorage.setItem('access_token', access_token);
}
Login.prototype.getUsername = function() {
return localStorage.getItem('username') || '';
}
Login.prototype.setUsername = function(username) {
localStorage.setItem('username', username);
}
Login.prototype.setClientId = function(client_id) {
this.client_id = client_id;
}
Login.prototype.getClientId = function() {
return this.client_id;
}
Login.prototype.setRedirect = function(redirect) {
this.redirect = redirect;
}
Login.prototype.getRedirect = function() {
return this.redirect;
}
Login.prototype.getLoginURL = function(scopes, state) {
return 'https://accounts.spotify.com/authorize?client_id=' + this.client_id
+ '&redirect_uri=' + encodeURIComponent(this.redirect)
+ '&scope=' + encodeURIComponent(scopes.join(' '))
+ '&response_type=token';
}
Login.prototype.openLogin = function(oldstate) {
var url = this.getLoginURL(['playlist-read-private','user-read-private'], oldstate);
location = url;
// window.open(url);
}
Login.prototype.getAuthHeader = function() {
return 'Bearer ' + this.getAccessToken();
}
Login.prototype.getUserInfo = function(callback) {
var url = 'https://api.spotify.com/v1/me';
$.ajax(url, {
dataType: 'json',
headers: {
'Authorization': this.getAuthHeader()
},
success: function(r) {
console.log('userinfo', r);
callback(r);
}
});
}
var Playlists = function(login) {
this.login = login;
}
Playlists.prototype.getRootlist = function(callback) {
// x
var url = 'https://api.spotify.com/v1/users/' + encodeURIComponent(this.login.getUsername()) + '/playlists';
$.ajax(url, {
dataType: 'json',
headers: {
'Authorization': this.login.getAuthHeader()
},
success: function(r) {
console.log('rootlist', r);
callback(r);
}
});
}
Playlists.prototype.getPlaylist = function(user, playlist, callback) {
// x
var url = 'https://api.spotify.com/v1/users/' + encodeURIComponent(user) + '/playlists/' + encodeURIComponent(playlist) + '/tracks?limit=500';
$.ajax(url, {
dataType: 'json',
headers: {
'Authorization': this.login.getAuthHeader()
},
success: function(r) {
console.log('playlist', r);
callback(r);
}
});
}
var SpotifyAPI = function() {
this.Search = new Search();
this.Track = new Track();
this.Login = new Login();
this.Playlists = new Playlists(this.Login);
}
exports.SpotifyAPI = SpotifyAPI;
})(window || this);