65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package monochrome
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type SearchItemType = string
|
|
|
|
var (
|
|
SearchItemAlbum SearchItemType = "ALBUM"
|
|
SearchItemSingle SearchItemType = "SINGLE"
|
|
)
|
|
|
|
type AudioQuality = string
|
|
|
|
var (
|
|
AudioQualityLossless AudioQuality = "LOSSLESS"
|
|
AudioQualityLow AudioQuality = "LOW"
|
|
)
|
|
|
|
type SearchAlbum 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"`
|
|
URL string `json:"url"`
|
|
CoverID string `json:"cover"`
|
|
VibrantColor string `json:"vibrantColor"`
|
|
Explicit bool `json:"explicit"`
|
|
AudioQuality AudioQuality `json:"audioQuality"`
|
|
}
|
|
|
|
type SearchResponse struct {
|
|
Data struct {
|
|
Albums struct {
|
|
Items []SearchAlbum `json:"items"`
|
|
} `json:"albums"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
func (c *Client) SearchAlbum(q string) ([]SearchAlbum, error) {
|
|
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)
|
|
}
|
|
|
|
data, err := c.do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to send request: %v", err)
|
|
}
|
|
|
|
response := SearchResponse{}
|
|
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.Albums.Items, nil
|
|
}
|