mirror of
https://github.com/gesellix/Bose-SoundTouch.git
synced 2026-08-19 00:56:16 +00:00
Add a custom-radio url stream source (#114)
Based on the descriptions at - https://gist.github.com/rody64/98a59990ff60ea962cac72cbe93edf56 - https://github.com/thlucas1/homeassistantcomponent_soundtouchplus/discussions/37 Example usage: ``` go run ./cmd/soundtouch-cli --host 192.... source custom-radio --url https://stream.antenne.de/chillout/stream/aacp --service-url http://soundtouch.local:8000 Selecting custom radio stream from 192....:8090... URL: https://stream.antenne.de/chillout/stream/aacp Proxy: http://soundtouch.local:8000/bmx/custom/v1/playback/aHR0cHM6Ly9zdHJlYW0uYW50ZW5uZS5kZS9jaGlsbG91dC9zdHJlYW0vYWFjcA== ✓ Custom radio stream selected ``` Relates to https://github.com/gesellix/Bose-SoundTouch/issues/94
This commit is contained in:
+28
-23
@@ -280,6 +280,33 @@ func TuneInPlaybackPodcast(podcastID string) (*models.BmxPlaybackResponse, error
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// BuildCustomStreamResponse builds a playback response from streamUrl, imageUrl, and name.
|
||||
func BuildCustomStreamResponse(streamURL, imageURL, name string) (*models.BmxPlaybackResponse, error) {
|
||||
streamList := []models.Stream{
|
||||
{
|
||||
HasPlaylist: true,
|
||||
IsRealtime: true,
|
||||
StreamUrl: streamURL,
|
||||
},
|
||||
}
|
||||
|
||||
audio := models.Audio{
|
||||
HasPlaylist: true,
|
||||
IsRealtime: true,
|
||||
StreamUrl: streamURL,
|
||||
Streams: streamList,
|
||||
}
|
||||
|
||||
response := &models.BmxPlaybackResponse{
|
||||
Audio: audio,
|
||||
ImageUrl: imageURL,
|
||||
Name: name,
|
||||
StreamType: "liveRadio",
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// PlayCustomStream builds a playback response from a base64-encoded JSON blob
|
||||
// with fields streamUrl, imageUrl, and name.
|
||||
func PlayCustomStream(data string) (*models.BmxPlaybackResponse, error) {
|
||||
@@ -302,27 +329,5 @@ func PlayCustomStream(data string) (*models.BmxPlaybackResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
streamList := []models.Stream{
|
||||
{
|
||||
HasPlaylist: true,
|
||||
IsRealtime: true,
|
||||
StreamUrl: jsonObj.StreamURL,
|
||||
},
|
||||
}
|
||||
|
||||
audio := models.Audio{
|
||||
HasPlaylist: true,
|
||||
IsRealtime: true,
|
||||
StreamUrl: jsonObj.StreamURL,
|
||||
Streams: streamList,
|
||||
}
|
||||
|
||||
response := &models.BmxPlaybackResponse{
|
||||
Audio: audio,
|
||||
ImageUrl: jsonObj.ImageURL,
|
||||
Name: jsonObj.Name,
|
||||
StreamType: "liveRadio",
|
||||
}
|
||||
|
||||
return response, nil
|
||||
return BuildCustomStreamResponse(jsonObj.StreamURL, jsonObj.ImageURL, jsonObj.Name)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gesellix/bose-soundtouch/pkg/service/bmx"
|
||||
@@ -94,3 +96,41 @@ func (s *Server) HandleOrionPlayback(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// HandleCustomPlayback returns custom playback information for a given stream URL.
|
||||
func (s *Server) HandleCustomPlayback(w http.ResponseWriter, r *http.Request) {
|
||||
encodedURL := chi.URLParam(r, "encodedURL")
|
||||
imageUrl := r.URL.Query().Get("imageUrl")
|
||||
name := r.URL.Query().Get("name")
|
||||
|
||||
// Decode URL if it's base64 encoded
|
||||
var streamUrl string
|
||||
|
||||
decoded, err := base64.URLEncoding.DecodeString(encodedURL)
|
||||
if err != nil {
|
||||
decoded, err = base64.StdEncoding.DecodeString(encodedURL)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
streamUrl = string(decoded)
|
||||
} else {
|
||||
// Try unescaping if it's not base64
|
||||
streamUrl, err = url.PathUnescape(encodedURL)
|
||||
if err != nil {
|
||||
streamUrl = encodedURL
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := bmx.BuildCustomStreamResponse(streamUrl, imageUrl, name)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -107,3 +108,44 @@ func TestOrionPlayback(t *testing.T) {
|
||||
t.Errorf("Expected name Test Orion, got %v", resp["name"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomPlayback(t *testing.T) {
|
||||
r, _ := setupRouter("http://localhost:8001", nil)
|
||||
|
||||
ts := httptest.NewServer(r)
|
||||
defer ts.Close()
|
||||
|
||||
// Base64 encoded: http://example.com/stream
|
||||
encodedURL := "aHR0cDovL2V4YW1wbGUuY29tL3N0cmVhbQ=="
|
||||
imageUrl := "http://example.com/img.jpg"
|
||||
name := "Test Custom"
|
||||
|
||||
res, err := http.Get(ts.URL + "/bmx/custom/v1/playback/" + encodedURL + "?imageUrl=" + url.QueryEscape(imageUrl) + "&name=" + url.QueryEscape(name))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() { _ = res.Body.Close() }()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Errorf("Expected status OK, got %v", res.Status)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
|
||||
var resp map[string]interface{}
|
||||
_ = json.Unmarshal(body, &resp)
|
||||
|
||||
if resp["name"] != "Test Custom" {
|
||||
t.Errorf("Expected name Test Custom, got %v", resp["name"])
|
||||
}
|
||||
|
||||
audio := resp["audio"].(map[string]interface{})
|
||||
if audio["streamUrl"] != "http://example.com/stream" {
|
||||
t.Errorf("Expected streamUrl http://example.com/stream, got %v", audio["streamUrl"])
|
||||
}
|
||||
|
||||
if resp["imageUrl"] != imageUrl {
|
||||
t.Errorf("Expected imageUrl %s, got %v", imageUrl, resp["imageUrl"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ func setupRouter(targetURL string, ds *datastore.DataStore) (*chi.Mux, *Server)
|
||||
r.Get("/tunein/v1/playback/episodes/{podcastID}", server.HandleTuneInPodcastInfo)
|
||||
r.Get("/tunein/v1/playback/episode/{podcastID}", server.HandleTuneInPlaybackPodcast)
|
||||
r.Post("/orion/v1/playback/station/{data}", server.HandleOrionPlayback)
|
||||
r.Get("/custom/v1/playback/{encodedURL}", server.HandleCustomPlayback)
|
||||
})
|
||||
|
||||
// Legacy or direct domain calls without /bmx prefix
|
||||
@@ -35,6 +36,7 @@ func setupRouter(targetURL string, ds *datastore.DataStore) (*chi.Mux, *Server)
|
||||
r.Get("/tunein/v1/playback/episodes/{podcastID}", server.HandleTuneInPodcastInfo)
|
||||
r.Get("/tunein/v1/playback/episode/{podcastID}", server.HandleTuneInPlaybackPodcast)
|
||||
r.Post("/orion/v1/playback/station/{data}", server.HandleOrionPlayback)
|
||||
r.Get("/custom/v1/playback/{encodedURL}", server.HandleCustomPlayback)
|
||||
|
||||
streamingRoutes := func(r chi.Router) {
|
||||
r.Get("/sourceproviders", server.HandleMargeSourceProviders)
|
||||
|
||||
Reference in New Issue
Block a user