init
This commit is contained in:
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