Skip to content

Commit

Permalink
Use CSV reader to read stations
Browse files Browse the repository at this point in the history
  • Loading branch information
agejevasv committed Jun 2, 2024
1 parent f63b236 commit 278f261
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
53 changes: 25 additions & 28 deletions internal/radio/stations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package radio

import (
"bufio"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand All @@ -59,55 +65,46 @@ 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 {
fmt.Println(err)
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
Expand Down
2 changes: 1 addition & 1 deletion internal/radio/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 278f261

Please sign in to comment.