init
This commit is contained in:
68
monochrome/search.go
Normal file
68
monochrome/search.go
Normal file
@@ -0,0 +1,68 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user