Compare commits

..

No commits in common. "master" and "v0.2-Linux-d0a8fdfb11d6372eece119f8f2a67e2184de97cd" have entirely different histories.

1 changed files with 18 additions and 22 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"math/rand" "math/rand"
"net" "net"
@ -13,17 +12,17 @@ import (
) )
type stationRecord struct { type stationRecord struct {
Name string `json:"name"` Name string `json:"name"`
Codec string `json:"codec"` Codec string `json:"codec"`
Bitrate json.Number `json:"bitrate"` Bitrate json.Number `json:"bitrate"`
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:"lastcheckok"` Lastcheck int `json:"lastcheckok"`
} }
func RandomIP(iplist []net.IP) net.IP { func RandomIP(iplist []net.IP) net.IP {
rand.NewSource(time.Now().Unix()) rand.Seed(time.Now().Unix())
randomIndex := rand.Intn(len(iplist)) randomIndex := rand.Intn(len(iplist))
return iplist[randomIndex] return iplist[randomIndex]
} }
@ -45,18 +44,13 @@ func GetApiHost() string {
return apiHost return apiHost
} }
func GetStations(qstring string) ([]stationRecord, error) { func GetStations(qstring string) ([]stationRecord, error){
urlstr := fmt.Sprintf("https://%s/json/stations/search?%s&limit=%d", GetApiHost(), qstring, maxitems()) 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.Print(err.Error()) log.Print(err.Error())
} }
defer func(Body io.ReadCloser) { defer resp.Body.Close()
err := Body.Close()
if err != nil {
log.Print(err.Error())
}
}(resp.Body)
var data []stationRecord var data []stationRecord
err = json.NewDecoder(resp.Body).Decode(&data) err = json.NewDecoder(resp.Body).Decode(&data)
@ -78,26 +72,28 @@ func pruneStations(stations []stationRecord) []stationRecord {
func StationSearch(name string, country string, state string, tags string, notok bool) ([]stationRecord, error) { func StationSearch(name string, country string, state string, tags string, notok bool) ([]stationRecord, error) {
params := url.Values{} params := url.Values{}
if name != "" { if name != ""{
params.Add("name", name) params.Add("name", name)
} }
if country != "" { if country != "" {
params.Add("country", country) params.Add("country", country)
} }
if state != "" { if state != ""{
params.Add("state", state) params.Add("state", state)
} }
if tags != "" { if tags != ""{
params.Add("tag", tags) params.Add("tag",tags)
} }
stations, err := GetStations(params.Encode()) stations, err := GetStations(params.Encode())
if err != nil { if err != nil{
return nil, err return nil, err
} }
if notok { if notok{
return stations, err return stations, err
} // otherwise prune the list } // otherwise prune the list
prunedStations := pruneStations(stations) // eliminate stations that are reporting down. prunedStations := pruneStations(stations) // eliminate stations that are reporting down.
return prunedStations, err return prunedStations, err
} }