navidrome/engine/scrobbler.go

102 lines
2.6 KiB
Go
Raw Normal View History

package engine
import (
2020-01-09 02:45:07 +01:00
"context"
"errors"
"fmt"
"time"
2017-04-01 15:47:14 +02:00
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/itunesbridge"
2020-01-09 02:45:07 +01:00
"github.com/cloudsonic/sonic-server/log"
)
const (
2016-04-21 16:44:27 +02:00
minSkipped = 3 * time.Second
maxSkipped = 20 * time.Second
)
type Scrobbler interface {
2020-01-09 02:45:07 +01:00
Register(ctx context.Context, playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
NowPlaying(ctx context.Context, playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
}
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr domain.NowPlayingRepository) Scrobbler {
2016-03-17 01:51:03 +01:00
return &scrobbler{itunes, mr, npr}
}
type scrobbler struct {
itunes itunesbridge.ItunesControl
mfRepo domain.MediaFileRepository
npRepo domain.NowPlayingRepository
}
2020-01-09 02:45:07 +01:00
func (s *scrobbler) detectSkipped(ctx context.Context, 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 {
2016-03-28 23:51:10 +02:00
return
}
s.npRepo.Dequeue(playerId)
2016-03-28 23:51:10 +02:00
default:
prev, _ := s.npRepo.Dequeue(playerId)
for {
if prev.TrackID == trackId {
2016-03-28 23:51:10 +02:00
break
}
np, err := s.npRepo.Dequeue(playerId)
if np == nil || err != nil {
break
}
diff := np.Start.Sub(prev.Start)
if diff < minSkipped || diff > maxSkipped {
log.Debug(ctx, fmt.Sprintf("-- Playtime for track %s was %v. Not skipping.", prev.TrackID, diff))
2016-03-29 19:54:08 +02:00
prev = np
2016-03-28 23:51:10 +02:00
continue
}
err = s.itunes.MarkAsSkipped(prev.TrackID, prev.Start.Add(1*time.Minute))
2016-03-28 23:51:10 +02:00
if err != nil {
log.Warn(ctx, "Error skipping track", "id", prev.TrackID)
2016-03-28 23:51:10 +02:00
} else {
log.Debug(ctx, "-- Skipped track "+prev.TrackID)
2016-03-28 23:51:10 +02:00
}
}
2016-03-17 23:58:09 +01:00
}
}
2020-01-09 02:45:07 +01:00
func (s *scrobbler) Register(ctx context.Context, playerId int, trackId string, playTime time.Time) (*domain.MediaFile, error) {
s.detectSkipped(ctx, playerId, trackId)
2016-03-17 23:58:09 +01:00
mf, err := s.mfRepo.Get(trackId)
if err != nil {
return nil, err
}
if mf == nil {
return nil, errors.New(fmt.Sprintf(`ID "%s" not found`, trackId))
}
if err := s.itunes.MarkAsPlayed(trackId, playTime); err != nil {
2016-03-17 01:45:08 +01:00
return nil, err
}
return mf, nil
}
2016-03-17 01:51:03 +01:00
2020-01-09 02:45:07 +01:00
func (s *scrobbler) NowPlaying(ctx context.Context, playerId int, playerName, trackId, username string) (*domain.MediaFile, error) {
mf, err := s.mfRepo.Get(trackId)
2016-03-17 02:04:41 +01:00
if err != nil {
return nil, err
}
if mf == nil {
return nil, errors.New(fmt.Sprintf(`ID "%s" not found`, trackId))
2016-03-17 02:04:41 +01:00
}
info := &domain.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
}