This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
spotify-playlister.js
139 lines (120 loc) · 3.98 KB
/
spotify-playlister.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
angular
.module('app.widgets')
.service('spotifyPlaylisterService', SpotifyPlaylisterService)
.controller('SpotifyPlaylisterController', SpotifyPlaylisterController)
SpotifyPlaylisterController.$inject = ['$scope', '$timeout', 'OHService', 'spotifyPlaylisterService'];
function SpotifyPlaylisterController($scope, $timeout, OHService, spotifyPlaylisterService) {
const ctrl = this;
const initController = () =>{
ctrl.state = {
noConfig: !$scope.config || !$scope.config.accessToken,
accessToken: null,
userId: null,
results: null,
query: null,
error: false
}
ctrl.model = {
query: ''
}
}
initController()
const loadPlaylists = () => {
ctrl.state.error = false;
const { userId, accessToken } = ctrl.state;
spotifyPlaylisterService.getUserPlaylists(accessToken, userId).then(playlists => {
ctrl.state.results = [{name: 'Your Playlists', items: playlists}]
}).catch(() => ctrl.state.error = true)
}
const init = (accessTokenItemName) => {
const accessTokenItem = OHService.getItem(accessTokenItemName);
if (!accessTokenItem || !accessTokenItem.state) {
return
}
if (ctrl.state.accessToken !== accessTokenItem.state) {
ctrl.state.accessToken = accessTokenItem.state
spotifyPlaylisterService.getUserId(ctrl.state.accessToken).then(userId => {
ctrl.state.userId = userId;
loadPlaylists()
}).catch(() => ctrl.state.error = true)
}
}
ctrl.reset = () => {
initController()
if ($scope.config && $scope.config.accessToken) {
init($scope.config.accessToken);
}
}
ctrl.play = (item) => {
OHService.sendCmd($scope.config.spotifyPlayer, item.uri);
}
ctrl.search = () => {
ctrl.state.query = ctrl.model.query;
ctrl.state.error = false;
ctrl.state.results = null;
const { accessToken, query } = ctrl.state;
if (query === '') {
loadPlaylists()
} else {
spotifyPlaylisterService.search(accessToken, query).then(results => {
ctrl.state.results = [
{name: 'Albums', ...results.albums},
{name: 'Playlists', ...results.playlists},
{name: 'Tracks', ...results.tracks},
{name: 'Artists', ...results.artists},
]
}).catch(() => ctrl.state.error = true)
}
}
$scope.$watch('config.accessToken', (accessTokenItemName) => {
if (accessTokenItemName) {
ctrl.state.noConfig = false;
const accessTokenItem = OHService.getItem(accessTokenItemName);
if (accessTokenItem && accessTokenItem.state) {
init(accessTokenItemName);
} else {
//XXX: needed when habpanel loaded with refresh. should be a better way
$timeout(() => {
init(accessTokenItemName);
}, 5000)
}
}
});
}
SpotifyPlaylisterService.$inject = ['$http', '$cacheFactory'];
function SpotifyPlaylisterService($http, $cacheFactory) {
const cache = $cacheFactory('spotify-playlister');
const baseHeaders = (accessToken) => {
return {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + accessToken
}
}
const enqueueCacheRemove = (key) => {
setTimeout(() => {
cache.remove(key)
}, 10000)
}
const doGet = (url, accessToken) => {
enqueueCacheRemove(url);
return $http({
method: 'GET',
url,
headers: baseHeaders(accessToken),
})
}
return {
getUserId: function(accessToken) {
const url = 'https://api.spotify.com/v1/me';
return doGet(url, accessToken).then(({data}) => data.id)
},
getUserPlaylists: function(accessToken, userId) {
const url = `https://api.spotify.com/v1/users/${userId}/playlists?limit=50&offset=0`;
return doGet(url, accessToken).then(({data}) => data.items)
},
search: function(accessToken, query) {
const url = `https://api.spotify.com/v1/search?type=album,artist,playlist,track&q=${encodeURIComponent(query)}&limit=15`
return doGet(url, accessToken).then(({data}) => data)
}
}
}