Skip to content

Commit

Permalink
feat: Added it so the radiochannels will be fetched from a external s…
Browse files Browse the repository at this point in the history
…ource

Signed-off-by: Tom Lanser <[email protected]>
  • Loading branch information
Tommylans committed Jun 19, 2023
1 parent 46f0227 commit e8142fb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
24 changes: 11 additions & 13 deletions channels/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package channels

import (
"io"
"log"
)

// TODO: Think about if we just want to return a beep.Streamer or if we want to return a io.ReadCloser
Expand All @@ -14,17 +15,14 @@ type RadioStation interface {
}

var (
RadioChannels = []RadioStation{
&RadioStationHttp{Name: "SlamFM", Url: "https://stream.slam.nl/slam_mp3", DiscordSnowflakeId: "slamfm"},
&RadioStationHttp{Name: "Veronica", Url: "https://playerservices.streamtheworld.com/api/livestream-redirect/VERONICA.mp3"},
&RadioStationHttp{Name: "SkyRadio", Url: "https://playerservices.streamtheworld.com/api/livestream-redirect/SKYRADIO.mp3", DiscordSnowflakeId: "skyradio"},
&RadioStationHttp{Name: "NPO Radio 1", Url: "https://icecast.omroep.nl/radio1-bb-mp3"},
&RadioStationHttp{Name: "NPO Radio 2", Url: "https://icecast.omroep.nl/radio2-bb-mp3", DiscordSnowflakeId: "nporadio2"},
&RadioStationHttp{Name: "NPO Radio 2 Soul & Jazz", Url: "https://icecast.omroep.nl/radio6-bb-mp3", DiscordSnowflakeId: "nporadio2"},
&RadioStationHttp{Name: "NPO 3FM", Url: "https://icecast.omroep.nl/3fm-bb-mp3", DiscordSnowflakeId: "3fm"},
&RadioStationHttp{Name: "NPO FunX", Url: "https://icecast.omroep.nl/funx-bb-mp3"},
&RadioStationHttp{Name: "538", Url: "https://playerservices.streamtheworld.com/api/livestream-redirect/RADIO538.mp3", DiscordSnowflakeId: "538"},
&RadioStationHttp{Name: "538 Verrückte Stunden", Url: "https://playerservices.streamtheworld.com/api/livestream-redirect/TLPSTR21.mp3", DiscordSnowflakeId: "538"},
&RadioStationHttp{Name: "West Friese Piraten Combinatie", Url: "http://82.72.214.143:8000/WFPC", DiscordSnowflakeId: "wfpc"},
}
RadioChannels []RadioStation
)

func init() {
radioChannels, err := FetchExternalRadioChannels()
if err != nil {
log.Fatal("Error fetching external radio channels: ", err)
}

RadioChannels = append(RadioChannels, radioChannels...)
}
72 changes: 72 additions & 0 deletions channels/external_channels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package channels

import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"strconv"
)

type RadioStationType string

const (
ExternalRadioStationsSourceUrl = "https://raw.githubusercontent.com/Tommylans/goradio/master/assets/radio-channels.json"
)

const (
RadioStationTypeHttpMp3 RadioStationType = "http-mp3"
)

type ExternalRadioStation struct {
Type RadioStationType `json:"type"`
Name string `json:"name"`
Url string `json:"url"`
DiscordSnowflakeId string `json:"discordSnowflakeId"`
}

func (r *ExternalRadioStation) ConstructRadioStation() (RadioStation, error) {
switch r.Type {
case RadioStationTypeHttpMp3:
return &RadioStationHttp{Name: r.Name, Url: r.Url, DiscordSnowflakeId: r.DiscordSnowflakeId}, nil
}

return nil, errors.New("unknown radio station type " + string(r.Type))
}

func FetchExternalRadioChannels() ([]RadioStation, error) {
response, err := http.Get(ExternalRadioStationsSourceUrl)
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, errors.New("unexpected status code " + strconv.Itoa(response.StatusCode))
}

allBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}

var externalRadioStations []ExternalRadioStation
err = json.Unmarshal(allBytes, &externalRadioStations)
if err != nil {
return nil, err
}

var radioStations []RadioStation
for _, externalRadioStation := range externalRadioStations {
radioStation, err := externalRadioStation.ConstructRadioStation()
if err != nil {
log.Println("Maybe the radio channel is not compatible with this version: ", err)
continue
}

radioStations = append(radioStations, radioStation)
}

return radioStations, nil
}

0 comments on commit e8142fb

Please sign in to comment.