feat: add song link conversion

This commit is contained in:
2026-02-17 16:33:57 +01:00
parent 1504e29919
commit e5a6db72b3
2 changed files with 66 additions and 2 deletions

View File

@@ -33,9 +33,15 @@ func (app *App) Download(url string, outputFolder string, serviceString string)
switch urlType { switch urlType {
case UrlTypeTrack: case UrlTypeTrack:
_, err := app.GetTrackMetadata(url) // metadata, err := app.GetTrackMetadata(url)
// if err != nil {
// return err
// }
// println(metadata.Data.TrackUnion.Id)
_, err := app.ConvertSongUrl(url)
if err != nil { if err != nil {
return err return err
} }
} }

58
lib/songlink.go Normal file
View File

@@ -0,0 +1,58 @@
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 {
Deezer LinkByPlatform `json:"deezer"`
Tidal LinkByPlatform `json:"tidal"`
}
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
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
}