diff --git a/browser.go b/browser.go index 79d49f9..18ff3d5 100644 --- a/browser.go +++ b/browser.go @@ -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 +} + diff --git a/radiomenu.go b/radiomenu.go new file mode 100644 index 0000000..1110061 --- /dev/null +++ b/radiomenu.go @@ -0,0 +1,3 @@ +package main + + diff --git a/stations.go b/stations.go index 32cd5e3..f119897 100644 --- a/stations.go +++ b/stations.go @@ -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) }