aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2024-02-11 10:31:43 +0100
committerPřemysl Eric Janouch <p@janouch.name>2024-02-11 10:31:43 +0100
commit91db4c7d846608482ef45d1432ab3f653cda73ed (patch)
tree1960df4fb3525101902ed8aba976d9b4334262f9
parent035776c50416a497df3182ac5fe55177573ae9bd (diff)
downloadbbc-on-ice-91db4c7d846608482ef45d1432ab3f653cda73ed.tar.gz
bbc-on-ice-91db4c7d846608482ef45d1432ab3f653cda73ed.tar.xz
bbc-on-ice-91db4c7d846608482ef45d1432ab3f653cda73ed.zip
Try harder to find titles for BBC services
-rw-r--r--main.go52
1 files changed, 42 insertions, 10 deletions
diff --git a/main.go b/main.go
index e5da2ef..529d24a 100644
--- a/main.go
+++ b/main.go
@@ -23,18 +23,13 @@ import (
const (
targetURI = "http://as-hls-%s-live.akamaized.net/pool_904/live/%s/" +
"%s/%s.isml/%s-audio%%3d%s.norewind.m3u8"
- networksURI = "https://rms.api.bbc.co.uk/radio/networks.json"
- metaURI = "https://rms.api.bbc.co.uk/v2/services/%s/segments/latest"
+ networksURI1 = "https://rms.api.bbc.co.uk/radio/networks.json"
+ networksURI2 = "https://rms.api.bbc.co.uk/v2/networks/%s"
+ metaURI = "https://rms.api.bbc.co.uk/v2/services/%s/segments/latest"
)
-type meta struct {
- title string // what's playing right now
- timeout uint // timeout for the next poll in ms
-}
-
-// getServiceTitle returns a human-friendly identifier for a BBC service ID.
-func getServiceTitle(name string) (string, error) {
- resp, err := http.Get(networksURI)
+func getServiceTitle1(name string) (string, error) {
+ resp, err := http.Get(networksURI1)
if resp != nil {
defer resp.Body.Close()
}
@@ -66,6 +61,43 @@ func getServiceTitle(name string) (string, error) {
return name, errors.New("unknown service")
}
+// getServiceTitle returns a human-friendly identifier for a BBC service ID.
+func getServiceTitle(name string) (string, error) {
+ // This endpoint is incomplete,
+ // but it contains the kind of service titles we want.
+ title, err := getServiceTitle1(name)
+ if err == nil {
+ return title, nil
+ }
+
+ // Network IDs tend to coincide with service IDs.
+ resp, err := http.Get(fmt.Sprintf(networksURI2, name))
+ if resp != nil {
+ defer resp.Body.Close()
+ }
+ if err != nil {
+ return name, err
+ }
+ b, err := ioutil.ReadAll(resp.Body)
+
+ var v struct {
+ LongTitle string `json:"long_title"`
+ }
+ err = json.Unmarshal(b, &v)
+ if err != nil {
+ return name, errors.New("invalid metadata response")
+ }
+ if v.LongTitle == "" {
+ return name, errors.New("unknown service")
+ }
+ return v.LongTitle, nil
+}
+
+type meta struct {
+ title string // what's playing right now
+ timeout uint // timeout for the next poll in ms
+}
+
var errNoSong = errors.New("no song is playing")
// getMeta retrieves and decodes metadata info from an independent webservice.