Files
music-dl/monochrome/album.go
Daniil Tsivinsky b42a556ec5 append metadata tags after saving tracks
also rewrite requests to monochrome using 2 helper methods
2026-02-20 18:56:38 +03:00

90 lines
2.4 KiB
Go

package monochrome
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"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 {
ID int `json:"id"`
Title string `json:"title"`
Duration int `json:"duration"`
TrackNumber int `json:"trackNumber"`
Explicit bool `json:"explicit"`
AudioQuality AudioQuality `json:"audioQuality"`
} `json:"item"`
}
type AlbumInfo struct {
ID int `json:"id"`
Title string `json:"title"`
Duration int `json:"duration"`
NumberOfTracks int `json:"numberOfTracks"`
ReleaseDate string `json:"releaseDate"`
Type SearchItemType `json:"type"`
CoverID string `json:"cover"`
VibrantColor string `json:"vibrantColor"`
Explicit bool `json:"explicit"`
Artist AlbumArtist `json:"artist"`
Artists []AlbumArtist `json:"artists"`
Items []AlbumTrack `json:"items"`
}
type AlbumInfoResponse struct {
Data AlbumInfo `json:"data"`
}
func (c *Client) AlbumInfo(id int) (*AlbumInfo, error) {
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)
}
data, err := c.do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %v", err)
}
response := AlbumInfoResponse{}
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
}