diff --git a/MeteorLight.go b/MeteorLight.go index 3d00c3a..377337f 100644 --- a/MeteorLight.go +++ b/MeteorLight.go @@ -81,6 +81,8 @@ func main() { queue := NewQueue(config) + var nr *QueueTrackEntry + getRandomTrack := func() *QueueTrackEntry { response, err := http.DefaultClient.Get(config.Queue.RandomSongApi) if err != nil { @@ -104,6 +106,16 @@ func main() { } } + sendNowRandom := func(nr *QueueTrackEntry) { + if config.Queue.NowRandom != "" { + jsonData, _ := json.Marshal(nr.original) + _, err := http.DefaultClient.Post(config.Queue.NowRandom, "application/json; charset=utf-8", bytes.NewReader(jsonData)) + if err != nil { + log.Print(err) + } + } + } + wg.Add(1) go func() { defer wg.Done() @@ -112,8 +124,12 @@ func main() { for { if e := getRandomTrack(); e != nil { queue.QueueEmpty <- e + nr = e + sendNowRandom(nr) } else if e = getFallbackTrack(); e != nil { queue.QueueEmpty <- e + nr = e + sendNowRandom(nr) } } }() @@ -151,6 +167,11 @@ func main() { jsonData, _ := json.Marshal(e.original) writer.Write(jsonData) } + case "nr": + if nr != nil { + jsonData, _ := json.Marshal(nr.original) + writer.Write(jsonData) + } case "queue": if len(pathSegments) == 2 { if request.Method != "POST" { @@ -264,6 +285,8 @@ func main() { writer.Write(jsonData) } } + + writer.Write([]byte{'n', 'u', 'l', 'l'}) }), } diff --git a/README.md b/README.md index a05bc4c..999ee44 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ This project is a Work in Progress. * Normalized channels / sample rates for mounts. * Implements ICY metadata * Uses sample/timed packet buffers, instead of kawa byte buffers, which cause wild differences between endpoints. Mounts usually align within 0.2s of each other, depending on client. +* Implements `queue.nr` and `/nr` (To be Deprecated/Changed) ## Dependencies diff --git a/config.go b/config.go index 11b45f7..dd0b668 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,7 @@ type Config struct { Queue struct { RandomSongApi string `toml:"random_song_api"` NowPlaying string `toml:"np"` + NowRandom string `toml:"nr"` FallbackPath string `toml:"fallback"` BufferLengthInKiB int `toml:"buffer_len"` } `toml:"queue"` diff --git a/example_config.toml b/example_config.toml index e5b80d4..0321f1e 100644 --- a/example_config.toml +++ b/example_config.toml @@ -22,6 +22,10 @@ random_song_api="http://localhost:8012/api/random" # will be identical to the JSON blob in the queue. np="http://localhost:8012/api/np" # +# An HTTP POST is issued to this URL when Kawa fetches a random track. The body +# will be identical to the JSON blob in memory. +nr="http://localhost:8012/api/nr" +# # When no tracks are available for whatever reason (such as external service # outages), this track will be played. fallback="/tmp/in.flac"