radio and refillreadio and clearradio
This commit is contained in:
parent
01d3958de0
commit
2c0543383b
@ -32,6 +32,10 @@ func Run(ctx *gctx.Context, client *spotify.Client, args []string) error {
|
|||||||
return commands.Shuffle(ctx, client)
|
return commands.Shuffle(ctx, client)
|
||||||
case "radio":
|
case "radio":
|
||||||
return commands.Radio(ctx, client)
|
return commands.Radio(ctx, client)
|
||||||
|
case "clearradio":
|
||||||
|
return commands.ClearRadio(ctx, client)
|
||||||
|
case "refillradio":
|
||||||
|
return commands.RefillRadio(ctx, client)
|
||||||
case "tracks":
|
case "tracks":
|
||||||
return tui.DisplayList(ctx, client)
|
return tui.DisplayList(ctx, client)
|
||||||
case "status":
|
case "status":
|
||||||
|
@ -18,9 +18,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
auth *spotifyauth.Authenticator
|
auth *spotifyauth.Authenticator
|
||||||
ch = make(chan *spotify.Client)
|
ch = make(chan *spotify.Client)
|
||||||
state = "abc123"
|
state = "abc123"
|
||||||
|
configDir, _ = os.UserConfigDir()
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
||||||
@ -48,9 +49,8 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
|||||||
spotifyauth.ScopeStreaming,
|
spotifyauth.ScopeStreaming,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
homdir, _ := os.UserHomeDir()
|
if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil {
|
||||||
if _, err := os.Stat(filepath.Join(homdir, ".config/gospt/auth.json")); err == nil {
|
authFile, err := os.Open(filepath.Join(configDir, "gospt/auth.json"))
|
||||||
authFile, err := os.Open(filepath.Join(homdir, ".config/gospt/auth.json"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -74,8 +74,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
homdir, _ := os.UserHomeDir()
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
||||||
err = ioutil.WriteFile(filepath.Join(homdir, ".config/gospt/auth.json"), out, 0o644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("FAILED TO SAVE AUTH")
|
panic("FAILED TO SAVE AUTH")
|
||||||
}
|
}
|
||||||
@ -84,8 +83,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
homdir, _ := os.UserHomeDir()
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
||||||
err = ioutil.WriteFile(filepath.Join(homdir, ".config/gospt/auth.json"), out, 0o644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("FAILED TO SAVE AUTH")
|
panic("FAILED TO SAVE AUTH")
|
||||||
}
|
}
|
||||||
@ -131,8 +129,7 @@ func completeAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
homdir, _ := os.UserHomeDir()
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
||||||
err = ioutil.WriteFile(filepath.Join(homdir, ".config/gospt/auth.json"), out, 0o644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("FAILED TO SAVE AUTH")
|
panic("FAILED TO SAVE AUTH")
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,14 @@ func QueueSong(ctx *gctx.Context, client *spotify.Client, id spotify.ID) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Radio(ctx *gctx.Context, client *spotify.Client) error {
|
func Radio(ctx *gctx.Context, client *spotify.Client) error {
|
||||||
rand.Seed(time.Now().Unix())
|
err := ClearRadio(ctx, client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
radioPlaylist, err := GetRadioPlaylist(ctx, client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
current_song, err := client.PlayerCurrentlyPlaying(ctx)
|
current_song, err := client.PlayerCurrentlyPlaying(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -91,27 +98,122 @@ func Radio(ctx *gctx.Context, client *spotify.Client) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
recomendationIds := []spotify.ID{}
|
||||||
for _, song := range recomendations.Tracks {
|
for _, song := range recomendations.Tracks {
|
||||||
fmt.Println(song.Name)
|
recomendationIds = append(recomendationIds, song.ID)
|
||||||
}
|
}
|
||||||
fmt.Println("QUEUED")
|
_, err = client.AddTracksToPlaylist(ctx, radioPlaylist.ID, recomendationIds...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client.PlayOpt(ctx, &spotify.PlayOptions{
|
||||||
|
PlaybackContext: &radioPlaylist.URI,
|
||||||
|
PlaybackOffset: &spotify.PlaybackOffset{
|
||||||
|
Position: 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
err = client.Repeat(ctx, "context")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
seed_track := recomendations.Tracks[3]
|
id := rand.Intn(len(recomendationIds)-2) + 1
|
||||||
seed := spotify.Seeds{
|
seed := spotify.Seeds{
|
||||||
Tracks: []spotify.ID{seed_track.ID},
|
Tracks: []spotify.ID{recomendationIds[id]},
|
||||||
}
|
}
|
||||||
recomendations, err = client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(100))
|
additional_recs, err := client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(100))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, song := range recomendations.Tracks {
|
fmt.Println(len(additional_recs.Tracks))
|
||||||
fmt.Println(song.Name)
|
additionalRecsIds := []spotify.ID{}
|
||||||
|
for _, song := range additional_recs.Tracks {
|
||||||
|
additionalRecsIds = append(additionalRecsIds, song.ID)
|
||||||
|
}
|
||||||
|
fmt.Println(len(additionalRecsIds))
|
||||||
|
_, err = client.AddTracksToPlaylist(ctx, radioPlaylist.ID, additionalRecsIds...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println("QUEUED")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RefillRadio(ctx *gctx.Context, client *spotify.Client) error {
|
||||||
|
status, err := client.PlayerCurrentlyPlaying(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("PLAYING", status.PlaybackContext.URI)
|
||||||
|
to_remove := []spotify.ID{}
|
||||||
|
radioPlaylist, err := GetRadioPlaylist(ctx, client)
|
||||||
|
found := false
|
||||||
|
page := 0
|
||||||
|
for !found {
|
||||||
|
tracks, err := client.GetPlaylistItems(ctx, radioPlaylist.ID, spotify.Limit(50), spotify.Offset(page*50))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, track := range tracks.Items {
|
||||||
|
fmt.Println("CHECKING", track.Track.Track.Name)
|
||||||
|
if track.Track.Track.ID == status.Item.ID {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
to_remove = append(to_remove, track.Track.Track.ID)
|
||||||
|
}
|
||||||
|
page++
|
||||||
|
}
|
||||||
|
recomendationIds := []spotify.ID{}
|
||||||
|
if len(to_remove) > 0 {
|
||||||
|
fmt.Println("REPLENISHING", len(to_remove), "SONGS")
|
||||||
|
_, err = client.RemoveTracksFromPlaylist(ctx, radioPlaylist.ID, to_remove...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
current_song, err := client.PlayerCurrentlyPlaying(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
seed := spotify.Seeds{
|
||||||
|
Tracks: []spotify.ID{current_song.Item.ID},
|
||||||
|
}
|
||||||
|
recomendations, err := client.GetRecommendations(ctx, seed, &spotify.TrackAttributes{}, spotify.Limit(100))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for idx, song := range recomendations.Tracks {
|
||||||
|
if idx >= len(to_remove) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
recomendationIds = append(recomendationIds, song.ID)
|
||||||
|
}
|
||||||
|
_, err = client.AddTracksToPlaylist(ctx, radioPlaylist.ID, recomendationIds...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearRadio(ctx *gctx.Context, client *spotify.Client) error {
|
||||||
|
radioPlaylist, err := GetRadioPlaylist(ctx, client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = client.UnfollowPlaylist(ctx, radioPlaylist.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
configDir, _ := os.UserConfigDir()
|
||||||
|
os.Remove(filepath.Join(configDir, "gospt/radio.json"))
|
||||||
|
client.Pause(ctx)
|
||||||
|
fmt.Println("Radio emptied")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Devices(ctx *gctx.Context, client *spotify.Client) error {
|
func Devices(ctx *gctx.Context, client *spotify.Client) error {
|
||||||
devices, err := client.PlayerDevices(ctx)
|
devices, err := client.PlayerDevices(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -266,3 +368,60 @@ func queueWithTransfer(ctx *gctx.Context, client *spotify.Client, track_id spoti
|
|||||||
ctx.Println("Playing!")
|
ctx.Println("Playing!")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func activateDevice(ctx *gctx.Context, client *spotify.Client) error {
|
||||||
|
configDir, _ := os.UserConfigDir()
|
||||||
|
deviceFile, err := os.Open(filepath.Join(configDir, "gospt/device.json"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer deviceFile.Close()
|
||||||
|
deviceValue, err := ioutil.ReadAll(deviceFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var device *spotify.PlayerDevice
|
||||||
|
err = json.Unmarshal(deviceValue, &device)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = client.TransferPlayback(ctx, device.ID, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
defer playlistFile.Close()
|
||||||
|
playlistValue, err := ioutil.ReadAll(playlistFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var playlist *spotify.FullPlaylist
|
||||||
|
err = json.Unmarshal(playlistValue, &playlist)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return playlist, nil
|
||||||
|
}
|
||||||
|
playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "gosptRADIO", "This is an automanaged playlist for the custom radio of gospt", false, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out, err := json.MarshalIndent(playlist, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/radio.json"), out, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return playlist, nil
|
||||||
|
}
|
||||||
|
@ -9,7 +9,8 @@ import (
|
|||||||
type Context struct {
|
type Context struct {
|
||||||
context.Context
|
context.Context
|
||||||
*log.Logger
|
*log.Logger
|
||||||
Debug *log.Logger
|
Debug *log.Logger
|
||||||
|
UserId string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext(ctx context.Context) *Context {
|
func NewContext(ctx context.Context) *Context {
|
||||||
@ -17,6 +18,7 @@ func NewContext(ctx context.Context) *Context {
|
|||||||
ctx,
|
ctx,
|
||||||
log.New(os.Stdout, "LOG:", 0),
|
log.New(os.Stdout, "LOG:", 0),
|
||||||
log.New(os.Stdout, "DEBUG:", 1),
|
log.New(os.Stdout, "DEBUG:", 1),
|
||||||
|
"",
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
5
main.go
5
main.go
@ -27,6 +27,11 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
currentUser, err := client.CurrentUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
ctx.UserId = currentUser.ID
|
||||||
err = api.Run(ctx, client, os.Args[1:])
|
err = api.Run(ctx, client, os.Args[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user