Try harder to find titles for BBC services

This commit is contained in:
Přemysl Eric Janouch 2024-02-11 10:31:43 +01:00
parent 035776c504
commit 91db4c7d84
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 42 additions and 10 deletions

52
main.go
View File

@ -23,18 +23,13 @@ import (
const ( const (
targetURI = "http://as-hls-%s-live.akamaized.net/pool_904/live/%s/" + targetURI = "http://as-hls-%s-live.akamaized.net/pool_904/live/%s/" +
"%s/%s.isml/%s-audio%%3d%s.norewind.m3u8" "%s/%s.isml/%s-audio%%3d%s.norewind.m3u8"
networksURI = "https://rms.api.bbc.co.uk/radio/networks.json" networksURI1 = "https://rms.api.bbc.co.uk/radio/networks.json"
metaURI = "https://rms.api.bbc.co.uk/v2/services/%s/segments/latest" 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 { func getServiceTitle1(name string) (string, error) {
title string // what's playing right now resp, err := http.Get(networksURI1)
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)
if resp != nil { if resp != nil {
defer resp.Body.Close() defer resp.Body.Close()
} }
@ -66,6 +61,43 @@ func getServiceTitle(name string) (string, error) {
return name, errors.New("unknown service") 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") var errNoSong = errors.New("no song is playing")
// getMeta retrieves and decodes metadata info from an independent webservice. // getMeta retrieves and decodes metadata info from an independent webservice.