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
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package monochrome
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type ClientConfig struct {
|
||||
ApiURL string
|
||||
}
|
||||
@@ -13,3 +20,41 @@ func NewClient(config ClientConfig) *Client {
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) makeRequest(method string, path string, body io.Reader, params map[string]string) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, c.config.ApiURL+path, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
|
||||
p := url.Values{}
|
||||
for key, value := range params {
|
||||
p.Add(key, value)
|
||||
}
|
||||
req.URL.RawQuery = p.Encode()
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) do(req *http.Request) ([]byte, error) {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusTooManyRequests {
|
||||
return nil, fmt.Errorf("rate limited")
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return data, fmt.Errorf("got error from api, data is filled with response")
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package monochrome
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type SearchItemType = string
|
||||
@@ -44,23 +43,20 @@ type SearchResponse struct {
|
||||
}
|
||||
|
||||
func (c *Client) SearchAlbum(q string) ([]SearchAlbum, error) {
|
||||
req, err := http.NewRequest("GET", c.config.ApiURL+"/search", nil)
|
||||
req, err := c.makeRequest("GET", "/search", nil, map[string]string{
|
||||
"al": q,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("al", q)
|
||||
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 := SearchResponse{}
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -20,24 +18,21 @@ type TrackInfoResponse struct {
|
||||
}
|
||||
|
||||
func (c *Client) TrackInfo(id int, quality AudioQuality) (*TrackInfo, error) {
|
||||
req, err := http.NewRequest("GET", c.config.ApiURL+"/track", nil)
|
||||
req, err := c.makeRequest("GET", "/track", nil, map[string]string{
|
||||
"id": strconv.Itoa(id),
|
||||
"quality": quality,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("id", strconv.Itoa(id))
|
||||
params.Set("quality", quality)
|
||||
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 := TrackInfoResponse{}
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user