createPlaylist

This commit is contained in:
Deluan
2016-03-24 12:06:39 -04:00
parent 5b2ecc39ca
commit 52850c6ef0
4 changed files with 48 additions and 2 deletions
+19
View File
@@ -2,6 +2,7 @@ package itunesbridge
import (
"fmt"
"strings"
"time"
)
@@ -12,6 +13,7 @@ type ItunesControl interface {
SetAlbumLoved(trackId string, loved bool) error
SetTrackRating(trackId string, rating int) error
SetAlbumRating(trackId string, rating int) error
CreatePlaylist(name string, ids []string) (string, error)
}
func NewItunesControl() ItunesControl {
@@ -20,6 +22,23 @@ func NewItunesControl() ItunesControl {
type itunesControl struct{}
func (c *itunesControl) CreatePlaylist(name string, ids []string) (string, error) {
pids := `"` + strings.Join(ids, `","`) + `"`
script := Script{
fmt.Sprintf(`set pls to (make new user playlist with properties {name:"%s"})`, name),
fmt.Sprintf(`set pids to {%s}`, pids),
`repeat with trackPID in pids`,
` set myTrack to the first item of (every track whose persistent ID is equal to trackPID)`,
` duplicate myTrack to pls`,
`end repeat`,
`persistent ID of pls`}
pid, err := script.OutputString()
if err != nil {
return "", err
}
return strings.TrimSuffix(pid, "\n"), nil
}
func (c *itunesControl) MarkAsPlayed(trackId string, playDate time.Time) error {
script := Script{fmt.Sprintf(
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),