mirror of
https://github.com/Superredstone/spotiflac-cli.git
synced 2026-03-07 20:18:07 +01:00
feat: add filename extension and verbose flag
This commit is contained in:
@@ -3,11 +3,13 @@ package lib
|
||||
type App struct {
|
||||
UserAgent string // User agent used for scraping requests
|
||||
SelectedTidalApiUrl string
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
func NewApp() App {
|
||||
return App{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
|
||||
Verbose: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,12 @@ func (app *App) Download(url string, outputFile string, serviceString string, qu
|
||||
return err
|
||||
}
|
||||
|
||||
outputFile, err = BuildFileOutput(outputFile, fileName, metadata)
|
||||
extension, err := GetFormatFromQuality(quality)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outputFile, err = BuildFileOutput(outputFile, fileName, extension, metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -73,6 +78,8 @@ func (app *App) Download(url string, outputFile string, serviceString string, qu
|
||||
}
|
||||
|
||||
func (app *App) DownloadFromUrl(url string, outputFilePath string) error {
|
||||
app.log("Downloading " + outputFilePath)
|
||||
|
||||
outputFile, err := os.Create(outputFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -90,5 +97,7 @@ func (app *App) DownloadFromUrl(url string, outputFilePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
app.log("Download completed")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
)
|
||||
|
||||
func (app *App) GetTrackMetadata(url string) (TrackMetadata, error) {
|
||||
app.log("Getting metadata for " + url)
|
||||
|
||||
client := NewSpotifyClient()
|
||||
var result TrackMetadata
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ type LinkByPlatform struct {
|
||||
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
|
||||
|
||||
19
lib/tidal.go
19
lib/tidal.go
@@ -44,25 +44,6 @@ func (app *App) GetAvailableApis() []string {
|
||||
}
|
||||
}
|
||||
|
||||
func (app *App) DownloadFromTidal(tidalId string) error {
|
||||
// url, err := app.GetTidalDownloadUrl(tidalId)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// rawResponse, err := http.Get(tidalUrl)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// defer rawResponse.Body.Close()
|
||||
//
|
||||
// _, err = io.ReadAll(rawResponse.Body)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type TidalAPIResponseV2 struct {
|
||||
Version string `json:"version"`
|
||||
Data struct {
|
||||
|
||||
31
lib/utils.go
31
lib/utils.go
@@ -41,7 +41,7 @@ func ParseTrackId(url string) (string, error) {
|
||||
return tmp2[0], nil
|
||||
}
|
||||
|
||||
func BuildFileName(metadata TrackMetadata) (string, error) {
|
||||
func BuildFileName(metadata TrackMetadata, extension string) (string, error) {
|
||||
var result string
|
||||
var artists string
|
||||
|
||||
@@ -51,19 +51,19 @@ func BuildFileName(metadata TrackMetadata) (string, error) {
|
||||
}
|
||||
artists = metadata.Data.TrackUnion.FirstArtist.Items[firstArtistLen-1].Profile.Name
|
||||
|
||||
for _, artist := range(metadata.Data.TrackUnion.OtherArtists.Items) {
|
||||
for _, artist := range metadata.Data.TrackUnion.OtherArtists.Items {
|
||||
artists += ", " + artist.Profile.Name
|
||||
}
|
||||
|
||||
result = fmt.Sprintf("%s - %s", metadata.Data.TrackUnion.Name, artists)
|
||||
result = fmt.Sprintf("%s - %s.%s", metadata.Data.TrackUnion.Name, artists, extension)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func BuildFileOutput(outputFile string, fileName string, metadata TrackMetadata) (string, error) {
|
||||
func BuildFileOutput(outputFile string, fileName string, extension string, metadata TrackMetadata) (string, error) {
|
||||
var result string
|
||||
|
||||
fileName, err := BuildFileName(metadata)
|
||||
fileName, err := BuildFileName(metadata, extension)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -76,3 +76,24 @@ func BuildFileOutput(outputFile string, fileName string, metadata TrackMetadata)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (app *App) log(message string) {
|
||||
if app.Verbose {
|
||||
fmt.Println(message)
|
||||
}
|
||||
}
|
||||
|
||||
func GetFormatFromQuality(quality string) (string, error) {
|
||||
switch quality {
|
||||
case "LOW":
|
||||
return "aac", nil
|
||||
case "HIGH":
|
||||
return "aac", nil
|
||||
case "LOSSLESS":
|
||||
return "flac", nil
|
||||
case "HI_RES_LOSSLESS":
|
||||
return "flac", nil
|
||||
default:
|
||||
return "", errors.New("Invalid quality.")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user