2016-03-12 02:49:01 +01:00
|
|
|
package itunesbridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ItunesControl interface {
|
2016-03-23 21:05:41 +01:00
|
|
|
MarkAsPlayed(trackId string, playDate time.Time) error
|
|
|
|
MarkAsSkipped(trackId string, skipDate time.Time) error
|
|
|
|
SetTrackLoved(trackId string, loved bool) error
|
|
|
|
SetAlbumLoved(trackId string, loved bool) error
|
|
|
|
SetTrackRating(trackId string, rating int) error
|
|
|
|
SetAlbumRating(trackId string, rating int) error
|
2016-03-12 02:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewItunesControl() ItunesControl {
|
2016-03-16 22:48:44 +01:00
|
|
|
return &itunesControl{}
|
2016-03-12 02:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type itunesControl struct{}
|
|
|
|
|
2016-03-23 21:05:41 +01:00
|
|
|
func (c *itunesControl) MarkAsPlayed(trackId string, playDate time.Time) error {
|
2016-03-12 02:49:01 +01:00
|
|
|
script := Script{fmt.Sprintf(
|
2016-03-23 21:05:41 +01:00
|
|
|
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),
|
2016-03-12 02:49:01 +01:00
|
|
|
`set c to (get played count of theTrack)`,
|
|
|
|
`tell theTrack`,
|
|
|
|
`set played count to c + 1`,
|
|
|
|
fmt.Sprintf(`set played date to date("%s")`, c.formatDateTime(playDate)),
|
|
|
|
`end tell`}
|
|
|
|
return script.Run()
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:05:41 +01:00
|
|
|
func (c *itunesControl) MarkAsSkipped(trackId string, skipDate time.Time) error {
|
2016-03-17 23:58:09 +01:00
|
|
|
script := Script{fmt.Sprintf(
|
2016-03-23 21:05:41 +01:00
|
|
|
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),
|
2016-03-17 23:58:09 +01:00
|
|
|
`set c to (get skipped count of theTrack)`,
|
|
|
|
`tell theTrack`,
|
|
|
|
`set skipped count to c + 1`,
|
|
|
|
fmt.Sprintf(`set skipped date to date("%s")`, c.formatDateTime(skipDate)),
|
|
|
|
`end tell`}
|
|
|
|
return script.Run()
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:05:41 +01:00
|
|
|
func (c *itunesControl) SetTrackLoved(trackId string, loved bool) error {
|
|
|
|
script := Script{fmt.Sprintf(
|
|
|
|
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),
|
|
|
|
`tell theTrack`,
|
|
|
|
fmt.Sprintf(`set loved to %v`, loved),
|
|
|
|
`end tell`}
|
|
|
|
return script.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *itunesControl) SetAlbumLoved(trackId string, loved bool) error {
|
|
|
|
script := Script{fmt.Sprintf(
|
|
|
|
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),
|
|
|
|
`tell theTrack`,
|
|
|
|
fmt.Sprintf(`set album loved to %v`, loved),
|
|
|
|
`end tell`}
|
|
|
|
return script.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *itunesControl) SetTrackRating(trackId string, rating int) error {
|
|
|
|
script := Script{fmt.Sprintf(
|
|
|
|
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),
|
|
|
|
`tell theTrack`,
|
|
|
|
fmt.Sprintf(`set rating to %d`, rating),
|
|
|
|
`end tell`}
|
|
|
|
return script.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *itunesControl) SetAlbumRating(trackId string, rating int) error {
|
|
|
|
script := Script{fmt.Sprintf(
|
|
|
|
`set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId),
|
|
|
|
`tell theTrack`,
|
|
|
|
fmt.Sprintf(`set album rating to %d`, rating),
|
|
|
|
`end tell`}
|
|
|
|
return script.Run()
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:48:44 +01:00
|
|
|
func (c *itunesControl) formatDateTime(d time.Time) string {
|
2016-03-12 02:49:01 +01:00
|
|
|
return d.Format("Jan _2, 2006 3:04PM")
|
|
|
|
}
|