From c8184da0edcf81d61d59ee172b51abdbd6843f34 Mon Sep 17 00:00:00 2001 From: Deluan Date: Mon, 28 Mar 2016 17:51:10 -0400 Subject: [PATCH] Skipping now working(?) --- engine/scrobbler.go | 45 ++++++++++++++++++++++++++-------------- engine/scrobbler_test.go | 12 ++++++++++- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/engine/scrobbler.go b/engine/scrobbler.go index 2de62a41..9ebb14f4 100644 --- a/engine/scrobbler.go +++ b/engine/scrobbler.go @@ -11,7 +11,7 @@ import ( ) const ( - minSkipped = time.Duration(3) * time.Second + minSkipped = time.Duration(5) * time.Second maxSkipped = time.Duration(20) * time.Second ) @@ -31,22 +31,37 @@ type scrobbler struct { } func (s *scrobbler) detectSkipped(playerId int, trackId string) { - for { - size, _ := s.npRepo.Count(playerId) - np, err := s.npRepo.Tail(playerId) - if err != nil || np == nil || (size == 1 && np.TrackId != trackId) { - break + size, _ := s.npRepo.Count(playerId) + switch size { + case 0: + return + case 1: + np, _ := s.npRepo.Tail(playerId) + if np.TrackId != trackId { + return } - s.npRepo.Dequeue(playerId) - if np.TrackId == trackId { - break - } - err = s.itunes.MarkAsSkipped(np.TrackId, np.Start.Add(time.Duration(1)*time.Minute)) - if err != nil { - beego.Warn("Error skipping track", np.TrackId) - } else { - beego.Debug("Skipped track", np.TrackId) + default: + prev, _ := s.npRepo.Dequeue(playerId) + for { + if prev.TrackId == trackId { + break + } + np, err := s.npRepo.Dequeue(playerId) + if np == nil || err != nil { + break + } + diff := np.Start.Sub(prev.Start) + if diff < minSkipped || diff > maxSkipped { + prev = np + continue + } + err = s.itunes.MarkAsSkipped(prev.TrackId, prev.Start.Add(time.Duration(1)*time.Minute)) + if err != nil { + beego.Warn("Error skipping track", prev.TrackId) + } else { + beego.Debug("Skipped track", prev.TrackId) + } } } } diff --git a/engine/scrobbler_test.go b/engine/scrobbler_test.go index c9eca94b..e12860b4 100644 --- a/engine/scrobbler_test.go +++ b/engine/scrobbler_test.go @@ -109,7 +109,17 @@ func TestSkipping(t *testing.T) { So(err, ShouldBeNil) }) }) - SkipConvey("And I skip it after 20 seconds", func() { + Convey("And I skip it after 5 seconds", func() { + npRepo.OverrideNow(start.Add(time.Duration(3) * time.Second)) + scrobbler.NowPlaying(1, "DSub", "2", "deluan") + Convey("Then the first song should be marked as skipped", func() { + mf, err := scrobbler.Register(1, "2", start.Add(time.Duration(3)*time.Minute)) + So(mf.Id, ShouldEqual, "2") + So(itCtrl.skipped, ShouldBeEmpty) + So(err, ShouldBeNil) + }) + }) + Convey("And I skip it after 20 seconds", func() { npRepo.OverrideNow(start.Add(time.Duration(30) * time.Second)) scrobbler.NowPlaying(1, "DSub", "2", "deluan") Convey("Then the first song should be marked as skipped", func() {