From 16c0ba104efbfd8a1761dd312f9b5a77648a48ef Mon Sep 17 00:00:00 2001 From: jjohnstondev Date: Sun, 8 Jan 2023 21:08:51 -0800 Subject: [PATCH] dont restart song on radio start --- internal/auth/auth.go | 7 +++++++ internal/commands/commands.go | 10 ++++++++-- internal/config/config.go | 3 --- internal/tui/list.go | 2 +- internal/tui/playlisttracks.go | 2 +- main.go | 7 ++++--- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index bd870dc..3d23785 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -25,6 +25,13 @@ var ( ) func GetClient(ctx *gctx.Context) (*spotify.Client, error) { + if config.Values.ClientId == "" || config.Values.ClientSecret == "" { + configDir, _ := os.UserConfigDir() + fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml")) + fmt.Println("GO HERE TO AND MAKE AN APPLICATION: https://developer.spotify.com/dashboard/applications") + fmt.Println("\nclient_id: \"idgoesherelikethis\"\nclient_secret: \"secretgoesherelikethis\"") + return nil, fmt.Errorf("\nINVALID CONFIG") + } auth = spotifyauth.New( spotifyauth.WithClientID(config.Values.ClientId), spotifyauth.WithClientSecret(config.Values.ClientSecret), diff --git a/internal/commands/commands.go b/internal/commands/commands.go index 88998ad..ddfac7d 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -91,7 +91,8 @@ func QueueSong(ctx *gctx.Context, client *spotify.Client, id spotify.ID) error { return nil } -func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song_id spotify.ID) error { +func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song_id spotify.ID, pos int) error { + start := time.Now().UnixMilli() seed := spotify.Seeds{ Tracks: []spotify.ID{song_id}, } @@ -117,11 +118,16 @@ func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song_id spotify.I if err != nil { return err } + delay := time.Now().UnixMilli() - start + if pos != 0 { + pos = pos + int(delay) + } client.PlayOpt(ctx, &spotify.PlayOptions{ PlaybackContext: &radioPlaylist.URI, PlaybackOffset: &spotify.PlaybackOffset{ Position: 0, }, + PositionMs: pos, }) err = client.Repeat(ctx, "context") if err != nil { @@ -178,7 +184,7 @@ func Radio(ctx *gctx.Context, client *spotify.Client) error { seed_song = tracks.Tracks[rand.Intn(len(tracks.Tracks))].SimpleTrack } } - return RadioGivenSong(ctx, client, seed_song.ID) + return RadioGivenSong(ctx, client, seed_song.ID, current_song.Progress) } func RefillRadio(ctx *gctx.Context, client *spotify.Client) error { diff --git a/internal/config/config.go b/internal/config/config.go index 248baaa..26a2009 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,9 +10,6 @@ import ( var Values struct { ClientId string `yaml:"client_id"` ClientSecret string `yaml:"client_secret"` - DeviceId string `yaml:"device_id"` - Port int `yaml:"port"` - DeviceName string `yaml:"device_name"` } func LoadConfig(configDir string) { diff --git a/internal/tui/list.go b/internal/tui/list.go index be266c1..2570ce9 100644 --- a/internal/tui/list.go +++ b/internal/tui/list.go @@ -75,7 +75,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } if msg.String() == "ctrl+r" { track := m.list.SelectedItem() - err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID) + err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID, 0) if err != nil { return m, tea.Quit } diff --git a/internal/tui/playlisttracks.go b/internal/tui/playlisttracks.go index c2b5d72..328d4e5 100644 --- a/internal/tui/playlisttracks.go +++ b/internal/tui/playlisttracks.go @@ -74,7 +74,7 @@ func (m playlistTracksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } if msg.String() == "ctrl+r" { track := m.list.SelectedItem() - err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID) + err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID, 0) if err != nil { return m, tea.Quit } diff --git a/main.go b/main.go index 86fecf2..9f1c504 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,8 @@ import ( ) func init() { - homeDir, _ := os.UserHomeDir() - configDir := filepath.Join(homeDir, ".config/gospt/") + configPath, _ := os.UserConfigDir() + configDir := filepath.Join(configPath, "gospt") config.LoadConfig(configDir) } @@ -25,7 +25,8 @@ func main() { ctx := gctx.NewContext(context.Background()) client, err := auth.GetClient(ctx) if err != nil { - panic(err.Error()) + fmt.Println(err.Error()) + return } currentUser, err := client.CurrentUser(ctx) if err != nil {