Skip to content

Commit d112c6c

Browse files
author
spezifisch
committed
add scrobbling submission timer after audioscrobbler specs
1 parent 785504d commit d112c6c

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

gui.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math"
66
"sort"
77
"strings"
8+
"time"
89

910
"github.com/gdamore/tcell/v2"
1011
"github.com/rivo/tview"
@@ -33,6 +34,7 @@ type Ui struct {
3334
playlists []SubsonicPlaylist
3435
connection *SubsonicConnection
3536
player *Player
37+
scrobbleTimer *time.Timer
3638
}
3739

3840
func (ui *Ui) handleEntitySelected(directoryId string) {
@@ -465,6 +467,12 @@ func createUi(_ *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection *Sub
465467
// Stores the song IDs
466468
var starIdList = map[string]struct{}{}
467469

470+
// create reused timer to scrobble after delay
471+
scrobbleTimer := time.NewTimer(0)
472+
if !scrobbleTimer.Stop() {
473+
<-scrobbleTimer.C
474+
}
475+
468476
ui := Ui{
469477
app: app,
470478
pages: pages,
@@ -484,6 +492,7 @@ func createUi(_ *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection *Sub
484492
playlists: *playlists,
485493
connection: connection,
486494
player: player,
495+
scrobbleTimer: scrobbleTimer,
487496
}
488497

489498
ui.addStarredToList()
@@ -499,6 +508,17 @@ func createUi(_ *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection *Sub
499508
ui.logList.RemoveItem(0)
500509
}
501510
})
511+
512+
case <-scrobbleTimer.C:
513+
// scrobble submission delay elapsed
514+
paused, err := ui.player.IsPaused()
515+
connection.Logger.Printf("scrobbler event: paused %v, err %v, qlen %d", paused, err, len(ui.player.Queue))
516+
isPlaying := err == nil && !paused
517+
if len(ui.player.Queue) > 0 && isPlaying {
518+
// it's still playing, submit it
519+
currentSong := ui.player.Queue[0]
520+
ui.connection.ScrobbleSubmission(currentSong.Id, true)
521+
}
502522
}
503523
}
504524
}()
@@ -917,14 +937,34 @@ func (ui *Ui) handleMpvEvents() {
917937
}
918938
} else if e.Event_Id == mpv.EVENT_START_FILE {
919939
ui.player.ReplaceInProgress = false
920-
ui.startStopStatus.SetText("[::b]stmp: [green]playing " + ui.player.Queue[0].Title)
921940
updateQueueList(ui.player, ui.queueList, ui.starIdList)
922941

923-
if ui.connection.Scrobble {
924-
// scrobble "now playing" event
925-
ui.connection.ScrobbleSubmission(ui.player.Queue[0].Id, false)
926-
// scrobble "submission" event
927-
ui.connection.ScrobbleSubmission(ui.player.Queue[0].Id, true)
942+
if len(ui.player.Queue) > 0 {
943+
currentSong := ui.player.Queue[0]
944+
ui.startStopStatus.SetText("[::b]stmp: [green]playing " + currentSong.Title)
945+
946+
if ui.connection.Scrobble {
947+
// scrobble "now playing" event
948+
ui.connection.ScrobbleSubmission(currentSong.Id, false)
949+
950+
// scrobble "submission" after song has been playing a bit
951+
// see: https://www.last.fm/api/scrobbling
952+
// A track should only be scrobbled when the following conditions have been met:
953+
// The track must be longer than 30 seconds. And the track has been played for
954+
// at least half its duration, or for 4 minutes (whichever occurs earlier.)
955+
if currentSong.Duration > 30 {
956+
scrobbleDelay := currentSong.Duration / 2
957+
if scrobbleDelay > 240 {
958+
scrobbleDelay = 240
959+
}
960+
scrobbleDuration := time.Duration(scrobbleDelay) * time.Second
961+
962+
ui.scrobbleTimer.Reset(scrobbleDuration)
963+
ui.connection.Logger.Printf("scrobbler: timer started, %v", scrobbleDuration)
964+
} else {
965+
ui.connection.Logger.Printf("scrobbler: track too short")
966+
}
967+
}
928968
}
929969
} else if e.Event_Id == mpv.EVENT_IDLE || e.Event_Id == mpv.EVENT_NONE {
930970
continue

0 commit comments

Comments
 (0)