Skipping now working(?)

This commit is contained in:
Deluan 2016-03-28 17:51:10 -04:00
parent 1e17efc729
commit c8184da0ed
2 changed files with 41 additions and 16 deletions

View File

@ -11,7 +11,7 @@ import (
) )
const ( const (
minSkipped = time.Duration(3) * time.Second minSkipped = time.Duration(5) * time.Second
maxSkipped = time.Duration(20) * time.Second maxSkipped = time.Duration(20) * time.Second
) )
@ -31,22 +31,37 @@ type scrobbler struct {
} }
func (s *scrobbler) detectSkipped(playerId int, trackId string) { func (s *scrobbler) detectSkipped(playerId int, trackId string) {
for { size, _ := s.npRepo.Count(playerId)
size, _ := s.npRepo.Count(playerId) switch size {
np, err := s.npRepo.Tail(playerId) case 0:
if err != nil || np == nil || (size == 1 && np.TrackId != trackId) { return
break case 1:
np, _ := s.npRepo.Tail(playerId)
if np.TrackId != trackId {
return
} }
s.npRepo.Dequeue(playerId) s.npRepo.Dequeue(playerId)
if np.TrackId == trackId { default:
break prev, _ := s.npRepo.Dequeue(playerId)
} for {
err = s.itunes.MarkAsSkipped(np.TrackId, np.Start.Add(time.Duration(1)*time.Minute)) if prev.TrackId == trackId {
if err != nil { break
beego.Warn("Error skipping track", np.TrackId) }
} else { np, err := s.npRepo.Dequeue(playerId)
beego.Debug("Skipped track", np.TrackId) 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)
}
} }
} }
} }

View File

@ -109,7 +109,17 @@ func TestSkipping(t *testing.T) {
So(err, ShouldBeNil) 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)) npRepo.OverrideNow(start.Add(time.Duration(30) * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan") scrobbler.NowPlaying(1, "DSub", "2", "deluan")
Convey("Then the first song should be marked as skipped", func() { Convey("Then the first song should be marked as skipped", func() {