Skip to content

Commit

Permalink
Do not auto-update default stations
Browse files Browse the repository at this point in the history
  • Loading branch information
agejevasv committed May 26, 2024
1 parent 2feb40f commit 2814c4a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Keyboard Control
*
Toggle playing a random station.
a-z
a-z and A-Z
Toggle playing a station marked with a given letter.
Enter and Space
Expand Down
9 changes: 8 additions & 1 deletion goradion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

var cfg = flag.String("s", "", "A link or a path to a stations.csv file")
var ver = flag.Bool("v", false, "Show the version number and quit")
var dbg = flag.Bool("d", false, "Enable debug log (file)")
var dbg = flag.Bool("d", false, "Enable debug log (goradion.log file in a current dir)")
var upd = flag.Bool("u", false, "Update default playlist")

func main() {
flag.Parse()
Expand All @@ -20,6 +21,12 @@ func main() {
os.Exit(0)
}

if *upd {
if err := radio.CacheDefaultStations(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}

radio.InitLog(*dbg)

stations, urls := radio.Stations(*cfg)
Expand Down
2 changes: 1 addition & 1 deletion internal/radio/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const helpString = `Keyboard Control
[green]*[default]
Toggle playing a random station.
[green]a[default]-[green]z[default]
[green]a[default]-[green]z[default] and [green]A[default]-[green]Z[default]
Toggle playing a station marked with a given letter.
[green]Enter[default] and [green]Space[default]
Expand Down
13 changes: 8 additions & 5 deletions internal/radio/stations.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func Stations(sta string) ([]string, []string) {
var scanner *bufio.Scanner

if sta == "" {
go cacheDefaultStations()
if s, err := cachedDefaultStations(); err != nil {
scanner = bufio.NewScanner(strings.NewReader(defaultStationsCSV))
} else {
Expand Down Expand Up @@ -115,15 +114,14 @@ func Stations(sta string) ([]string, []string) {
return stat, urls
}

func cacheDefaultStations() {
func CacheDefaultStations() error {
s, err := fetchStations(defaultStationsURL)

if err != nil {
log.Println(err)
return
return err
}

ioutil.WriteFile(cacheFileName(), []byte(s), 0644)
return ioutil.WriteFile(cacheFileName(), []byte(s), 0644)
}

func cacheFileName() string {
Expand All @@ -142,11 +140,16 @@ func cachedDefaultStations() ([]byte, error) {

func fetchStations(url string) (string, error) {
resp, err := http.Get(url)

if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return "", fmt.Errorf("Failed to GET %s, status code: %d", url, resp.StatusCode)
}

data, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
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.7"
const Version = "0.3.8"

func VersionString() string {
return fmt.Sprintf("goradion v%s (%s/%s)", Version, runtime.GOARCH, runtime.GOOS)
Expand Down

0 comments on commit 2814c4a

Please sign in to comment.