init
This commit is contained in:
62
monochrome/album.go
Normal file
62
monochrome/album.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package monochrome
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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"`
|
||||
Items []AlbumTrack `json:"items"`
|
||||
}
|
||||
|
||||
type AlbumInfoResponse struct {
|
||||
Data AlbumInfo `json:"data"`
|
||||
}
|
||||
|
||||
func (c *Client) AlbumInfo(id int) (*AlbumInfo, error) {
|
||||
req, err := http.NewRequest("GET", c.config.ApiURL+"/album", nil)
|
||||
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)
|
||||
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 {
|
||||
return nil, fmt.Errorf("failed to decode json response: %v", err)
|
||||
}
|
||||
|
||||
return &response.Data, nil
|
||||
}
|
||||
15
monochrome/client.go
Normal file
15
monochrome/client.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package monochrome
|
||||
|
||||
type ClientConfig struct {
|
||||
ApiURL string
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
config ClientConfig
|
||||
}
|
||||
|
||||
func NewClient(config ClientConfig) *Client {
|
||||
return &Client{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
72
monochrome/tracks.go
Normal file
72
monochrome/tracks.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package monochrome
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TrackInfo struct {
|
||||
TrackId int `json:"trackId"`
|
||||
Manifest string `json:"manifest"`
|
||||
}
|
||||
|
||||
type TrackInfoResponse struct {
|
||||
Data TrackInfo `json:"data"`
|
||||
}
|
||||
|
||||
func (c *Client) TrackInfo(id int, quality AudioQuality) (*TrackInfo, error) {
|
||||
req, err := http.NewRequest("GET", c.config.ApiURL+"/track", nil)
|
||||
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)
|
||||
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 {
|
||||
return nil, fmt.Errorf("failed to decode json response: %v", err)
|
||||
}
|
||||
|
||||
return &response.Data, nil
|
||||
}
|
||||
|
||||
type TrackManifestEncryption = string
|
||||
|
||||
var (
|
||||
TrackManifestEncryptionNone TrackManifestEncryption = "NONE"
|
||||
)
|
||||
|
||||
type TrackManifest struct {
|
||||
MimeType string `json:"mimeType"`
|
||||
Codecs string `json:"codecs"`
|
||||
EncryptionType TrackManifestEncryption `json:"encryptionType"`
|
||||
URLs []string `json:"urls"`
|
||||
}
|
||||
|
||||
func (c *Client) DecodeManifest(manifest string) (*TrackManifest, error) {
|
||||
m, err := base64.StdEncoding.DecodeString(manifest)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode track manifest: %v", err)
|
||||
}
|
||||
|
||||
mData := &TrackManifest{}
|
||||
if err := json.NewDecoder(bytes.NewReader(m)).Decode(mData); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode track manifest: %v", err)
|
||||
}
|
||||
|
||||
return mData, nil
|
||||
}
|
||||
Reference in New Issue
Block a user