Skip to content

Commit

Permalink
add scrobbling submission timer after audioscrobbler specs
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Oct 24, 2023
1 parent 785504d commit d112c6c
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"sort"
"strings"
"time"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
Expand Down Expand Up @@ -33,6 +34,7 @@ type Ui struct {
playlists []SubsonicPlaylist
connection *SubsonicConnection
player *Player
scrobbleTimer *time.Timer
}

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

// create reused timer to scrobble after delay
scrobbleTimer := time.NewTimer(0)
if !scrobbleTimer.Stop() {
<-scrobbleTimer.C
}

ui := Ui{
app: app,
pages: pages,
Expand All @@ -484,6 +492,7 @@ func createUi(_ *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection *Sub
playlists: *playlists,
connection: connection,
player: player,
scrobbleTimer: scrobbleTimer,
}

ui.addStarredToList()
Expand All @@ -499,6 +508,17 @@ func createUi(_ *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection *Sub
ui.logList.RemoveItem(0)
}
})

case <-scrobbleTimer.C:
// scrobble submission delay elapsed
paused, err := ui.player.IsPaused()
connection.Logger.Printf("scrobbler event: paused %v, err %v, qlen %d", paused, err, len(ui.player.Queue))
isPlaying := err == nil && !paused
if len(ui.player.Queue) > 0 && isPlaying {
// it's still playing, submit it
currentSong := ui.player.Queue[0]
ui.connection.ScrobbleSubmission(currentSong.Id, true)
}
}
}
}()
Expand Down Expand Up @@ -917,14 +937,34 @@ func (ui *Ui) handleMpvEvents() {
}
} else if e.Event_Id == mpv.EVENT_START_FILE {
ui.player.ReplaceInProgress = false
ui.startStopStatus.SetText("[::b]stmp: [green]playing " + ui.player.Queue[0].Title)
updateQueueList(ui.player, ui.queueList, ui.starIdList)

if ui.connection.Scrobble {
// scrobble "now playing" event
ui.connection.ScrobbleSubmission(ui.player.Queue[0].Id, false)
// scrobble "submission" event
ui.connection.ScrobbleSubmission(ui.player.Queue[0].Id, true)
if len(ui.player.Queue) > 0 {
currentSong := ui.player.Queue[0]
ui.startStopStatus.SetText("[::b]stmp: [green]playing " + currentSong.Title)

if ui.connection.Scrobble {
// scrobble "now playing" event
ui.connection.ScrobbleSubmission(currentSong.Id, false)

// scrobble "submission" after song has been playing a bit
// see: https://www.last.fm/api/scrobbling
// A track should only be scrobbled when the following conditions have been met:
// The track must be longer than 30 seconds. And the track has been played for
// at least half its duration, or for 4 minutes (whichever occurs earlier.)
if currentSong.Duration > 30 {
scrobbleDelay := currentSong.Duration / 2
if scrobbleDelay > 240 {
scrobbleDelay = 240
}
scrobbleDuration := time.Duration(scrobbleDelay) * time.Second

ui.scrobbleTimer.Reset(scrobbleDuration)
ui.connection.Logger.Printf("scrobbler: timer started, %v", scrobbleDuration)
} else {
ui.connection.Logger.Printf("scrobbler: track too short")
}
}
}
} else if e.Event_Id == mpv.EVENT_IDLE || e.Event_Id == mpv.EVENT_NONE {
continue
Expand Down

0 comments on commit d112c6c

Please sign in to comment.