From 85882f6a40b1a88232b2322f887816165708bb2f Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 23 Mar 2016 16:05:41 -0400 Subject: [PATCH] Using persistent ids to talk to iTunes Also added methods to set loved and rating --- itunesbridge/itunes.go | 52 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/itunesbridge/itunes.go b/itunesbridge/itunes.go index 46838304d..042fbd538 100644 --- a/itunesbridge/itunes.go +++ b/itunesbridge/itunes.go @@ -6,8 +6,12 @@ import ( ) type ItunesControl interface { - MarkAsPlayed(id string, playDate time.Time) error - MarkAsSkipped(id string, skipDate time.Time) error + 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 } func NewItunesControl() ItunesControl { @@ -16,9 +20,9 @@ func NewItunesControl() ItunesControl { type itunesControl struct{} -func (c *itunesControl) MarkAsPlayed(id string, playDate time.Time) error { +func (c *itunesControl) MarkAsPlayed(trackId string, playDate time.Time) error { script := Script{fmt.Sprintf( - `set theTrack to the first item of (every track whose database ID is equal to "%s")`, id), + `set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId), `set c to (get played count of theTrack)`, `tell theTrack`, `set played count to c + 1`, @@ -27,9 +31,9 @@ func (c *itunesControl) MarkAsPlayed(id string, playDate time.Time) error { return script.Run() } -func (c *itunesControl) MarkAsSkipped(id string, skipDate time.Time) error { +func (c *itunesControl) MarkAsSkipped(trackId string, skipDate time.Time) error { script := Script{fmt.Sprintf( - `set theTrack to the first item of (every track whose database ID is equal to "%s")`, id), + `set theTrack to the first item of (every track whose persistent ID is equal to "%s")`, trackId), `set c to (get skipped count of theTrack)`, `tell theTrack`, `set skipped count to c + 1`, @@ -38,6 +42,42 @@ func (c *itunesControl) MarkAsSkipped(id string, skipDate time.Time) error { return script.Run() } +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() +} + func (c *itunesControl) formatDateTime(d time.Time) string { return d.Format("Jan _2, 2006 3:04PM") }