2016-03-16 22:48:44 +01:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2016-03-23 15:06:26 +01:00
|
|
|
"github.com/astaxie/beego"
|
2016-03-16 22:48:44 +01:00
|
|
|
"github.com/deluan/gosonic/domain"
|
|
|
|
"github.com/deluan/gosonic/itunesbridge"
|
|
|
|
)
|
|
|
|
|
2016-03-24 22:14:13 +01:00
|
|
|
const (
|
2016-03-28 23:51:10 +02:00
|
|
|
minSkipped = time.Duration(5) * time.Second
|
2016-03-24 22:14:13 +01:00
|
|
|
maxSkipped = time.Duration(20) * time.Second
|
|
|
|
)
|
|
|
|
|
2016-03-16 22:48:44 +01:00
|
|
|
type Scrobbler interface {
|
2016-03-17 23:58:09 +01:00
|
|
|
Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
|
2016-03-23 15:06:26 +01:00
|
|
|
NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
|
2016-03-16 22:48:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 01:27:48 +01:00
|
|
|
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler {
|
2016-03-17 01:51:03 +01:00
|
|
|
return &scrobbler{itunes, mr, npr}
|
2016-03-16 22:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type scrobbler struct {
|
|
|
|
itunes itunesbridge.ItunesControl
|
|
|
|
mfRepo domain.MediaFileRepository
|
2016-03-17 01:27:48 +01:00
|
|
|
npRepo NowPlayingRepository
|
2016-03-16 22:48:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 18:51:02 +01:00
|
|
|
func (s *scrobbler) detectSkipped(playerId int, trackId string) {
|
2016-03-28 23:51:10 +02:00
|
|
|
size, _ := s.npRepo.Count(playerId)
|
|
|
|
switch size {
|
|
|
|
case 0:
|
|
|
|
return
|
|
|
|
case 1:
|
|
|
|
np, _ := s.npRepo.Tail(playerId)
|
|
|
|
if np.TrackId != trackId {
|
|
|
|
return
|
2016-03-23 18:51:02 +01:00
|
|
|
}
|
|
|
|
s.npRepo.Dequeue(playerId)
|
2016-03-28 23:51:10 +02:00
|
|
|
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
|
2016-03-29 01:14:21 +02:00
|
|
|
beego.Debug(fmt.Sprintf("-- Playtime for track %s was %v. Not skipping.", prev.TrackId, diff))
|
2016-03-28 23:51:10 +02:00
|
|
|
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 {
|
2016-03-29 01:14:21 +02:00
|
|
|
beego.Debug("-- Skipped track", prev.TrackId)
|
2016-03-28 23:51:10 +02:00
|
|
|
}
|
2016-03-23 15:06:26 +01:00
|
|
|
}
|
2016-03-17 23:58:09 +01:00
|
|
|
}
|
2016-03-23 18:51:02 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 22:14:13 +01:00
|
|
|
func (s *scrobbler) Register(playerId int, trackId string, playTime time.Time) (*domain.MediaFile, error) {
|
2016-03-23 18:51:02 +01:00
|
|
|
s.detectSkipped(playerId, trackId)
|
2016-03-17 23:58:09 +01:00
|
|
|
|
2016-03-23 15:06:26 +01:00
|
|
|
mf, err := s.mfRepo.Get(trackId)
|
2016-03-16 22:48:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mf == nil {
|
2016-03-23 15:06:26 +01:00
|
|
|
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
2016-03-16 22:48:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 22:14:13 +01:00
|
|
|
if err := s.itunes.MarkAsPlayed(trackId, playTime); err != nil {
|
2016-03-17 01:45:08 +01:00
|
|
|
return nil, err
|
2016-03-16 22:48:44 +01:00
|
|
|
}
|
|
|
|
return mf, nil
|
|
|
|
}
|
2016-03-17 01:51:03 +01:00
|
|
|
|
2016-03-23 15:06:26 +01:00
|
|
|
func (s *scrobbler) NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error) {
|
2016-03-17 14:34:32 +01:00
|
|
|
mf, err := s.mfRepo.Get(trackId)
|
2016-03-17 02:04:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mf == nil {
|
2016-03-17 14:34:32 +01:00
|
|
|
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
2016-03-17 02:04:41 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 22:14:13 +01:00
|
|
|
info := &NowPlayingInfo{TrackId: trackId, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName}
|
|
|
|
return mf, s.npRepo.Enqueue(info)
|
2016-03-17 01:51:03 +01:00
|
|
|
}
|