clean up the help display; fine-tune the parsing; prepare for the menu

This commit is contained in:
Greg Gauthier 2021-03-16 19:27:52 +00:00
parent a06bc91576
commit 16e0fdd9de
3 changed files with 50 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"math/rand"
"net"
"net/http"
"net/url"
"time"
)
@ -17,10 +18,9 @@ type stationRecord struct {
Countrycode string `json:"countrycode"`
Tags string `json:"tags"`
Url string `json:"url"`
Lastcheck int `json:"lastcheck"`
Lastcheck int `json:"lastcheckok"`
}
func RandomIP(iplist []net.IP) net.IP {
rand.Seed(time.Now().Unix())
randomIndex := rand.Intn(len(iplist))
@ -45,7 +45,7 @@ func GetApiHost() string {
}
func GetStations(qstring string) ([]stationRecord, error){
urlstr := fmt.Sprintf("https://%s/json/stations/search?%s&limit=100000",GetApiHost(),qstring)
urlstr := fmt.Sprintf("https://%s/json/stations/search?%s&limit=%d",GetApiHost(),qstring,maxitems())
resp, err := http.Get(urlstr)
if err != nil {
log.Printf(err.Error())
@ -60,4 +60,37 @@ func GetStations(qstring string) ([]stationRecord, error){
return data, err
}
func pruneStations(stations []stationRecord) []stationRecord {
filteredStations := stations[:0]
for _, station := range stations {
if station.Lastcheck == 1 {
filteredStations = append(filteredStations, station)
}
}
return filteredStations
}
func StationSearch(name string, country string, state string, tags string) ([]stationRecord, error) {
params := url.Values{}
if name != ""{
params.Add("name", name)
}
if country != "" {
params.Add("country", country)
}
if state != ""{
params.Add("state", state)
}
if tags != ""{
params.Add("tag",tags)
}
stations, err := GetStations(params.Encode())
if err != nil{
return nil, err
}
prunedStations := pruneStations(stations) // eliminate stations that are reporting down.
return prunedStations, err
}

3
radiomenu.go Normal file
View File

@ -0,0 +1,3 @@
package main

View File

@ -15,7 +15,14 @@ func main(){
state string
tags string
)
flag.Usage = func() {
fmt.Printf("Usage: \n")
fmt.Printf(" gostations ")
fmt.Printf(" [-n \"name\"] [-c \"home country\"] [-s \"home state\"] [-t \"ordered,tag,list\"]\n")
flag.PrintDefaults()
fmt.Printf(" -h (or none)\n")
fmt.Printf("\tThis help message\n")
}
flag.StringVar(&name, "n", "", "Station name (or identifier).")
flag.StringVar(&country, "c", "", "Home country.")
flag.StringVar(&state, "s", "", "Home state (if in the United States).")
@ -26,12 +33,10 @@ func main(){
flag.Usage()
}
stations, err := GetStations("tag=chicago")
if err != nil{
fmt.Println(err.Error())
}
stations, _ := StationSearch(name, country, state, tags)
fmt.Println(len(stations))
for _, station := range stations {
fmt.Printf("\"%s\", %s, %s, %s\n", station.Name, station.Codec, station.Bitrate, station.Url)
fmt.Printf("%+v\n", station)
}