Skip to content
Snippets Groups Projects
Commit 24ac7c3e authored by ale's avatar ale
Browse files

record played songs to the playlog

parent 6842a7e9
Branches
No related tags found
No related merge requests found
......@@ -17,7 +17,9 @@ func main() {
flag.Parse()
db := djmpd.NewDJRandomDatabase()
m := mpd.NewMPD(mpd.NewPortAudioPlayer(), db)
player := mpd.NewPortAudioPlayer()
db.SetupPlayRecorder(player)
m := mpd.NewMPD(player, db)
m.HandleURL("djrandom", db)
log.Fatal(m.ListenAndServe(fmt.Sprintf(":%d", *port)))
}
......@@ -2,6 +2,7 @@ package djmpd
import (
"bytes"
"container/list"
"errors"
"flag"
"fmt"
......@@ -251,3 +252,58 @@ func (d *DJRandomDatabase) GetSong(songURL *url.URL) (mpd.Song, error) {
// Convert api.Song to Song.
return newDJRandomSong(&resp, d.client), nil
}
func (d *DJRandomDatabase) SetupPlayRecorder(player mpd.Player) {
rec := newPlayRecorder(d.client)
player.SetSongPlayedCallback(rec.SongPlayed)
}
type PlayRecorder struct {
client *util.HttpClient
played *list.List
count int
size int
lock sync.Mutex
}
func newPlayRecorder(client *util.HttpClient) *PlayRecorder {
return &PlayRecorder{
client: client,
played: list.New(),
size: 5,
}
}
func (r *PlayRecorder) SongPlayed(song mpd.Song) {
s, ok := song.(*DJRandomSong)
if !ok {
return
}
r.lock.Lock()
defer r.lock.Unlock()
// songID := s.Id
r.played.PushBack(s.Id)
if r.count < r.size {
r.count++
} else {
r.played.Remove(r.played.Front())
}
var ids []api.SongID
for e := r.played.Front(); e != nil; e = e.Next() {
ids = append(ids, e.Value.(api.SongID))
}
// Make the HTTP request in a separate goroutine so that we
// don't block on network issues (ignores errors).
go func() {
log.Printf("add_playlog: %v", ids)
req := api.AddPlayLogRequest{Songs: ids}
var resp bool
if err := r.client.Post("/api/add_playlog", &req, &resp); err != nil {
log.Printf("add_playlog error: %v", err)
}
}()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment