From 278f261bd7de19627074110fbfb24ccb740967ea Mon Sep 17 00:00:00 2001 From: Viktoras Agejevas Date: Sun, 2 Jun 2024 08:25:36 +0300 Subject: [PATCH] Use CSV reader to read stations --- internal/radio/stations.go | 53 ++++++++++++++++++-------------------- internal/radio/version.go | 2 +- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/internal/radio/stations.go b/internal/radio/stations.go index f8104d0..33994b1 100644 --- a/internal/radio/stations.go +++ b/internal/radio/stations.go @@ -1,7 +1,7 @@ package radio import ( - "bufio" + "encoding/csv" "fmt" "io" "io/ioutil" @@ -41,14 +41,20 @@ const defaultStationsCSV = `SomaFM: Secret Agent,https://somafm.com/secretagent1 The Jazz Groove: Mix #1,http://east-mp3-128.streamthejazzgroove.com/stream The Jazz Groove: Mix #2,http://west-mp3-128.streamthejazzgroove.com/stream Jazz24,https://live.amperwave.net/direct/ppm-jazz24aac256-ibc1 - HiRes: City Radio Smooth & Jazz,http://cityradio.ddns.net:8000/cityradio48flac - HiRes: City Radio Pop,http://cityradio.ddns.net:8000/citypop + Linn Radio, http://radio.linn.co.uk:8003/autodj + Linn Jazz,http://radio.linn.co.uk:8000/autodj + Linn Classical,http://radio.linn.co.uk:8004/autodj + Mother Earth Radio,https://motherearth.streamserver24.com/listen/motherearth/motherearth.aac + Mother Earth Jazz,https://motherearth.streamserver24.com/listen/motherearth_jazz/motherearth.jazz.mp4 + Mother Earth Instrumental,https://motherearth.streamserver24.com/listen/motherearth_instrumental/motherearth.instrumental.aac + Mother Earth Klassic,https://motherearth.streamserver24.com:18910/motherearth.klassik.aac + FluxFM: Jazzradio Schwarzenstein,https://streams.fluxfm.de/jazzschwarz/mp3-320/audio/ + FluxFM: Xjazz,https://streams.fluxfm.de/xjazz/mp3-320/audio/ + FluxFM: Chillhop,https://streams.fluxfm.de/Chillhop/mp3-320/streams.fluxfm.de/ HiRes: Radio Paradise,https://stream.radioparadise.com/flacm HiRes: Radio Paradise Mellow,https://stream.radioparadise.com/mellow-flacm HiRes: Radio Paradise Rock,https://stream.radioparadise.com/rock-flacm HiRes: JB Radio2,https://maggie.torontocast.com:8076/flac - HiRes: MaXXima,http://maxxima.mine.nu:8000/maxx.ogg - HiRes: Radio Sputnik Underground!,https://radiosputnik.nl:8443/flac HiRes: Naim Radio,http://mscp3.live-streams.nl:8360/flac.flac HiRes: Naim Jazz,http://mscp3.live-streams.nl:8340/jazz-flac.flac HiRes: Naim Classical,http://mscp3.live-streams.nl:8250/class-flac.flac @@ -59,30 +65,24 @@ const defaultStationsCSV = `SomaFM: Secret Agent,https://somafm.com/secretagent1 HiRes: SuperStereo 5,http://icecast.centaury.cl:7570/SuperStereoHiRes5 HiRes: SuperStereo 6,http://icecast.centaury.cl:7570/SuperStereoHiRes6 HiRes: SuperStereo 7,http://icecast.centaury.cl:7570/SuperStereoHiRes7 - HiRes: ∏ano,https://stream.p-node.org/piano.flac - Linn Radio, http://radio.linn.co.uk:8003/autodj - Linn Jazz,http://radio.linn.co.uk:8000/autodj - Linn Classical,http://radio.linn.co.uk:8004/autodj - Mother Earth Jazz,https://motherearth.streamserver24.com/listen/motherearth_jazz/motherearth.jazz.mp4 - Mother Earth Instrumental,https://motherearth.streamserver24.com/listen/motherearth_instrumental/motherearth.instrumental.aac - Mother Earth Radio,https://motherearth.streamserver24.com/listen/motherearth/motherearth.aac - Mother Earth Klassic,https://motherearth.streamserver24.com:18910/motherearth.klassik.aac` + HiRes: MaXXima,http://maxxima.mine.nu:8000/maxx.ogg + HiRes: Radio Sputnik Underground!,https://radiosputnik.nl:8443/flac` func Stations(sta string) ([]string, []string) { - var scanner *bufio.Scanner + var reader *csv.Reader if sta == "" { if s, err := cachedDefaultStations(); err != nil { - scanner = bufio.NewScanner(strings.NewReader(defaultStationsCSV)) + reader = csv.NewReader(strings.NewReader(defaultStationsCSV)) } else { - scanner = bufio.NewScanner(strings.NewReader(string(s))) + reader = csv.NewReader(strings.NewReader(string(s))) } } else if strings.HasPrefix(sta, "http") { s, err := fetchStations(sta) if err != nil { s = defaultStationsCSV } - scanner = bufio.NewScanner(strings.NewReader(s)) + reader = csv.NewReader(strings.NewReader(s)) } else { file, err := os.Open(sta) if err != nil { @@ -90,24 +90,21 @@ func Stations(sta string) ([]string, []string) { os.Exit(1) } defer file.Close() - scanner = bufio.NewScanner(file) + reader = csv.NewReader(file) } stat := make([]string, 0) urls := make([]string, 0) - for scanner.Scan() { - d := strings.Split(scanner.Text(), ",") - if len(d) != 2 { - log.Println("Wrong stations entry:", scanner.Text()) - continue - } - stat = append(stat, strings.Trim(d[0], " ")) - urls = append(urls, strings.Trim(d[1], " ")) + records, err := reader.ReadAll() + if err != nil { + fmt.Println("Can't parse stations CSV:", err) + os.Exit(1) } - if err := scanner.Err(); err != nil { - panic(err) + for _, r := range records { + stat = append(stat, strings.Trim(r[0], " ")) + urls = append(urls, strings.Trim(r[1], " ")) } return stat, urls diff --git a/internal/radio/version.go b/internal/radio/version.go index 02921a4..78231e1 100644 --- a/internal/radio/version.go +++ b/internal/radio/version.go @@ -5,7 +5,7 @@ import ( "runtime" ) -const Version = "0.3.10" +const Version = "0.3.11" func VersionString() string { return fmt.Sprintf("goradion v%s (%s/%s)", Version, runtime.GOARCH, runtime.GOOS)