tangled
alpha
login
or
join now
tom.sherman.is
/
piper
0
fork
atom
A fork of https://github.com/teal-fm/piper
0
fork
atom
overview
issues
pulls
pipelines
re-begin work on spotify playlists
Natalie B.
11 months ago
de3dcceb
7d2a8492
+56
1 changed file
expand all
collapse all
unified
split
service
spotify
playlists.go
+56
service/spotify/playlists.go
···
1
1
+
package spotify
2
2
+
3
3
+
import (
4
4
+
"encoding/json"
5
5
+
"fmt"
6
6
+
"io"
7
7
+
"log"
8
8
+
"net/http"
9
9
+
)
10
10
+
11
11
+
type Playlist struct {
12
12
+
Name string `json:"name"`
13
13
+
ID string `json:"id"`
14
14
+
}
15
15
+
16
16
+
type PlaylistResponse struct {
17
17
+
Limit int `json:"limit"`
18
18
+
Next string `json:"next"`
19
19
+
Offset int `json:"offset"`
20
20
+
Previous string `json:"previous"`
21
21
+
Total int `json:"total"`
22
22
+
Items []Playlist `json:"items"`
23
23
+
}
24
24
+
25
25
+
func (s *SpotifyService) getUserPlaylists(userID int64) (*PlaylistResponse, error) {
26
26
+
27
27
+
s.mu.RLock()
28
28
+
token, exists := s.userTokens[userID]
29
29
+
s.mu.RUnlock()
30
30
+
31
31
+
if !exists || token == "" {
32
32
+
return nil, fmt.Errorf("no access token for user %d", userID)
33
33
+
}
34
34
+
35
35
+
resp, err := http.NewRequest("GET", "https://api.spotify.com/v1/me/playlists", nil)
36
36
+
if err != nil {
37
37
+
return nil, fmt.Errorf("failed to get user playlists: %w", err)
38
38
+
}
39
39
+
defer resp.Body.Close()
40
40
+
41
41
+
if resp.Response.StatusCode != http.StatusOK {
42
42
+
return nil, fmt.Errorf("error response from Spotify: %s", resp.Response.Status)
43
43
+
}
44
44
+
body, err := io.ReadAll(resp.Body)
45
45
+
if err != nil {
46
46
+
log.Fatal("failed to read resp.Body")
47
47
+
}
48
48
+
49
49
+
var playlistResponse PlaylistResponse
50
50
+
51
51
+
if err := json.Unmarshal(body, &playlistResponse); err != nil {
52
52
+
return nil, fmt.Errorf("failed to decode user playlists: %w", err)
53
53
+
}
54
54
+
55
55
+
return &playlistResponse, nil
56
56
+
}