a
This commit is contained in:
parent
e6ce4e3204
commit
8f528e4171
@ -2,6 +2,7 @@ package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
@ -936,35 +937,36 @@ func activateDevice(ctx *gctx.Context, client *spotify.Client) error {
|
||||
|
||||
func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) {
|
||||
configDir, _ := os.UserConfigDir()
|
||||
if _, err := os.Stat(filepath.Join(configDir, "gospt/radio.json")); err == nil {
|
||||
playlistFile, err := os.Open(filepath.Join(configDir, "gospt/radio.json"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
playlistFile, err := os.ReadFile(filepath.Join(configDir, "gospt/radio.json"))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return CreateRadioPlaylist(ctx, client)
|
||||
}
|
||||
defer playlistFile.Close()
|
||||
playlistValue, err := ioutil.ReadAll(playlistFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var playlist *spotify.FullPlaylist
|
||||
err = json.Unmarshal(playlistValue, &playlist)
|
||||
err = json.Unmarshal(playlistFile, &playlist)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return playlist, nil
|
||||
}
|
||||
|
||||
func CreateRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullPlaylist, error) {
|
||||
// private flag doesnt work
|
||||
configDir, _ := os.UserConfigDir()
|
||||
playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := json.MarshalIndent(playlist, "", " ")
|
||||
raw, err := json.MarshalIndent(playlist, "", " ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/radio.json"), out, 0o644)
|
||||
err = os.WriteFile(filepath.Join(configDir, "gospt/radio.json"), raw, 0o644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return playlist, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user