69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package monochrome
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
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 := http.NewRequest("GET", c.config.ApiURL+"/search", nil)
|
|
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)
|
|
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 {
|
|
return nil, fmt.Errorf("failed to decode json response: %v", err)
|
|
}
|
|
|
|
return response.Data.Albums.Items, nil
|
|
}
|