[WIP] music platform user data scraper
teal-fm atproto
at main 1.3 kB view raw
1package spotify 2 3import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "log" 8 "net/http" 9) 10 11type Playlist struct { 12 Name string `json:"name"` 13 ID string `json:"id"` 14} 15 16type PlaylistResponse struct { 17 Limit int `json:"limit"` 18 Next string `json:"next"` 19 Offset int `json:"offset"` 20 Previous string `json:"previous"` 21 Total int `json:"total"` 22 Items []Playlist `json:"items"` 23} 24 25func (s *SpotifyService) getUserPlaylists(userID int64) (*PlaylistResponse, error) { 26 s.mu.RLock() 27 token, exists := s.userTokens[userID] 28 s.mu.RUnlock() 29 30 if !exists || token == "" { 31 return nil, fmt.Errorf("no access token for user %d", userID) 32 } 33 34 resp, err := http.NewRequest("GET", "https://api.spotify.com/v1/me/playlists", nil) 35 if err != nil { 36 return nil, fmt.Errorf("failed to get user playlists: %w", err) 37 } 38 defer resp.Body.Close() 39 40 if resp.Response.StatusCode != http.StatusOK { 41 return nil, fmt.Errorf("error response from Spotify: %s", resp.Response.Status) 42 } 43 body, err := io.ReadAll(resp.Body) 44 if err != nil { 45 log.Fatal("failed to read resp.Body") 46 } 47 48 var playlistResponse PlaylistResponse 49 50 if err := json.Unmarshal(body, &playlistResponse); err != nil { 51 return nil, fmt.Errorf("failed to decode user playlists: %w", err) 52 } 53 54 return &playlistResponse, nil 55}