mirror of
https://github.com/Superredstone/spotiflac-cli.git
synced 2026-03-07 20:18:07 +01:00
refactor: split metadata into a new file
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -15,43 +14,6 @@ const (
|
||||
DEFAULT_DOWNLOAD_OUTPUT_FOLDER = "."
|
||||
)
|
||||
|
||||
type MetadataSong struct {
|
||||
Track MetadataTrack `json:"track"`
|
||||
}
|
||||
|
||||
type MetadataTrack struct {
|
||||
SpotifyID string `json:"spotify_id"`
|
||||
Artists string `json:"artists"`
|
||||
Name string `json:"name"`
|
||||
AlbumName string `json:"album_name"`
|
||||
AlbumArtist string `json:"album_artist"`
|
||||
DurationMS int `json:"duration_ms"`
|
||||
Images string `json:"images"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
TrackNumber int `json:"track_number"`
|
||||
TotalTracks int `json:"total_tracks"`
|
||||
DiscNumber int `json:"disc_number"`
|
||||
TotalDiscs int `json:"total_discs"`
|
||||
ExternalURLs string `json:"external_urls"`
|
||||
Copyright string `json:"copyright"`
|
||||
Publisher string `json:"publisher"`
|
||||
Plays string `json:"plays"`
|
||||
IsExplicit bool `json:"is_explicit"`
|
||||
}
|
||||
|
||||
type MetadataPlaylist struct {
|
||||
TrackList []MetadataTrack `json:"track_list"`
|
||||
Info MetadataPlaylistInfo `json:"playlist_info"`
|
||||
}
|
||||
|
||||
type MetadataPlaylistInfo struct {
|
||||
Owner MetadataPlaylistOwner `json:"owner"`
|
||||
}
|
||||
|
||||
type MetadataPlaylistOwner struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func Download(application *app.App, url string, output_folder string) error {
|
||||
if output_folder == "" {
|
||||
output_folder = DEFAULT_DOWNLOAD_OUTPUT_FOLDER
|
||||
@@ -110,25 +72,3 @@ func Download(application *app.App, url string, output_folder string) error {
|
||||
|
||||
return errors.New("Invalid Spotify URL.")
|
||||
}
|
||||
|
||||
func GetMetadata[T MetadataPlaylist | MetadataSong](application *app.App, url string) (T, error) {
|
||||
var result T
|
||||
|
||||
metadataRequest := app.SpotifyMetadataRequest{
|
||||
URL: url,
|
||||
Delay: 0,
|
||||
Timeout: 5,
|
||||
}
|
||||
|
||||
metadata, err := application.GetSpotifyMetadata(metadataRequest)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(metadata), &result)
|
||||
if err != nil {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
67
pkg/metadata.go
Normal file
67
pkg/metadata.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/Superredstone/spotiflac-cli/app"
|
||||
)
|
||||
|
||||
type MetadataSong struct {
|
||||
Track MetadataTrack `json:"track"`
|
||||
}
|
||||
|
||||
type MetadataTrack struct {
|
||||
SpotifyID string `json:"spotify_id"`
|
||||
Artists string `json:"artists"`
|
||||
Name string `json:"name"`
|
||||
AlbumName string `json:"album_name"`
|
||||
AlbumArtist string `json:"album_artist"`
|
||||
DurationMS int `json:"duration_ms"`
|
||||
Images string `json:"images"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
TrackNumber int `json:"track_number"`
|
||||
TotalTracks int `json:"total_tracks"`
|
||||
DiscNumber int `json:"disc_number"`
|
||||
TotalDiscs int `json:"total_discs"`
|
||||
ExternalURLs string `json:"external_urls"`
|
||||
Copyright string `json:"copyright"`
|
||||
Publisher string `json:"publisher"`
|
||||
Plays string `json:"plays"`
|
||||
IsExplicit bool `json:"is_explicit"`
|
||||
}
|
||||
|
||||
type MetadataPlaylist struct {
|
||||
TrackList []MetadataTrack `json:"track_list"`
|
||||
Info MetadataPlaylistInfo `json:"playlist_info"`
|
||||
}
|
||||
|
||||
type MetadataPlaylistInfo struct {
|
||||
Owner MetadataPlaylistOwner `json:"owner"`
|
||||
}
|
||||
|
||||
type MetadataPlaylistOwner struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func GetMetadata[T MetadataPlaylist | MetadataSong](application *app.App, url string) (T, error) {
|
||||
var result T
|
||||
|
||||
metadataRequest := app.SpotifyMetadataRequest{
|
||||
URL: url,
|
||||
Delay: 0,
|
||||
Timeout: 5,
|
||||
}
|
||||
|
||||
metadata, err := application.GetSpotifyMetadata(metadataRequest)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(metadata), &result)
|
||||
if err != nil {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user