clean up the help display; fine-tune the parsing; prepare for the menu
This commit is contained in:
parent
a06bc91576
commit
16e0fdd9de
39
browser.go
39
browser.go
@ -7,6 +7,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,10 +18,9 @@ type stationRecord struct {
|
|||||||
Countrycode string `json:"countrycode"`
|
Countrycode string `json:"countrycode"`
|
||||||
Tags string `json:"tags"`
|
Tags string `json:"tags"`
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
Lastcheck int `json:"lastcheck"`
|
Lastcheck int `json:"lastcheckok"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func RandomIP(iplist []net.IP) net.IP {
|
func RandomIP(iplist []net.IP) net.IP {
|
||||||
rand.Seed(time.Now().Unix())
|
rand.Seed(time.Now().Unix())
|
||||||
randomIndex := rand.Intn(len(iplist))
|
randomIndex := rand.Intn(len(iplist))
|
||||||
@ -45,7 +45,7 @@ func GetApiHost() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetStations(qstring string) ([]stationRecord, error){
|
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)
|
resp, err := http.Get(urlstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(err.Error())
|
log.Printf(err.Error())
|
||||||
@ -60,4 +60,37 @@ func GetStations(qstring string) ([]stationRecord, error){
|
|||||||
return data, err
|
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
3
radiomenu.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
|
17
stations.go
17
stations.go
@ -15,7 +15,14 @@ func main(){
|
|||||||
state string
|
state string
|
||||||
tags 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(&name, "n", "", "Station name (or identifier).")
|
||||||
flag.StringVar(&country, "c", "", "Home country.")
|
flag.StringVar(&country, "c", "", "Home country.")
|
||||||
flag.StringVar(&state, "s", "", "Home state (if in the United States).")
|
flag.StringVar(&state, "s", "", "Home state (if in the United States).")
|
||||||
@ -26,12 +33,10 @@ func main(){
|
|||||||
flag.Usage()
|
flag.Usage()
|
||||||
}
|
}
|
||||||
|
|
||||||
stations, err := GetStations("tag=chicago")
|
stations, _ := StationSearch(name, country, state, tags)
|
||||||
if err != nil{
|
fmt.Println(len(stations))
|
||||||
fmt.Println(err.Error())
|
|
||||||
}
|
|
||||||
for _, station := range 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user