append metadata tags after saving tracks
also rewrite requests to monochrome using 2 helper methods
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
package monochrome
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AlbumArtist struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
PictureID string `json:"picture"`
|
||||
}
|
||||
|
||||
type AlbumTrack struct {
|
||||
Type string `json:"type"`
|
||||
Item struct {
|
||||
@@ -30,6 +38,8 @@ type AlbumInfo struct {
|
||||
CoverID string `json:"cover"`
|
||||
VibrantColor string `json:"vibrantColor"`
|
||||
Explicit bool `json:"explicit"`
|
||||
Artist AlbumArtist `json:"artist"`
|
||||
Artists []AlbumArtist `json:"artists"`
|
||||
Items []AlbumTrack `json:"items"`
|
||||
}
|
||||
|
||||
@@ -38,25 +48,42 @@ type AlbumInfoResponse struct {
|
||||
}
|
||||
|
||||
func (c *Client) AlbumInfo(id int) (*AlbumInfo, error) {
|
||||
req, err := http.NewRequest("GET", c.config.ApiURL+"/album", nil)
|
||||
req, err := c.makeRequest("GET", "/album", nil, map[string]string{
|
||||
"id": strconv.Itoa(id),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("id", strconv.Itoa(id))
|
||||
req.URL.RawQuery = params.Encode()
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
data, err := c.do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response := AlbumInfoResponse{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
||||
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode json response: %v", err)
|
||||
}
|
||||
|
||||
return &response.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) AlbumCoverImage(coverId string) ([]byte, error) {
|
||||
coverUrl := fmt.Sprintf("%s/%s/640x640.jpg", "https://resources.tidal.com/images", strings.ReplaceAll(coverId, "-", "/"))
|
||||
resp, err := http.Get(coverUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.Header.Get("Content-Type") != "image/jpeg" {
|
||||
return nil, fmt.Errorf("invalid response type: got %s", resp.Header.Get("Content-Type"))
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %v", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user