mirror of
https://github.com/Superredstone/spotiflac-cli.git
synced 2026-03-08 04:28:07 +01:00
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package lib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
SONGLINK_API_BASE_URL = "https://api.song.link/v1-alpha.1/links?url="
|
|
RATE_LIMITED_RETURN_CODE = 429
|
|
)
|
|
|
|
type SongLinkResponse struct {
|
|
EntityUniqueId string `json:"entityUniqueId"`
|
|
UserCountry string `json:"userCountry"`
|
|
PageUrl string `json:"pageUrl"`
|
|
LinksByPlatform LinksByPlatform `json:"linksByPlatform"`
|
|
}
|
|
|
|
type LinksByPlatform struct {
|
|
Tidal *LinkByPlatform `json:"tidal,omitempty"`
|
|
}
|
|
|
|
type LinkByPlatform struct {
|
|
Country string `json:"country"`
|
|
Url string `json:"url"`
|
|
EntityUniqueId string `json:"entityUniqueId"`
|
|
}
|
|
|
|
func (app *App) ConvertSongUrl(url string) (SongLinkResponse, error) {
|
|
var result SongLinkResponse
|
|
|
|
app.log("Searching " + url)
|
|
|
|
rawResponse, err := http.Get(SONGLINK_API_BASE_URL + url)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
if rawResponse.StatusCode == RATE_LIMITED_RETURN_CODE {
|
|
return result, errors.New("You have been rate limited by song.link, try again later.")
|
|
}
|
|
|
|
defer rawResponse.Body.Close()
|
|
|
|
response, err := io.ReadAll(rawResponse.Body)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
err = json.Unmarshal(response, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|