Compare commits

...

1 Commits

Author SHA1 Message Date
Greg Gauthier c2508127b4 package updates 2023-12-13 12:00:28 +00:00
1 changed files with 22 additions and 18 deletions

View File

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