2021-03-16 13:09:27 +00:00
|
|
|
package main
|
|
|
|
|
2021-03-16 14:46:28 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-03-16 19:27:52 +00:00
|
|
|
"net/url"
|
2021-03-16 14:46:28 +00:00
|
|
|
"time"
|
|
|
|
)
|
2021-03-16 13:09:27 +00:00
|
|
|
|
2021-03-16 16:42:30 +00:00
|
|
|
type stationRecord struct {
|
|
|
|
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"`
|
2021-03-16 19:27:52 +00:00
|
|
|
Lastcheck int `json:"lastcheckok"`
|
2021-03-16 14:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RandomIP(iplist []net.IP) net.IP {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
randomIndex := rand.Intn(len(iplist))
|
|
|
|
return iplist[randomIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
func nslookup(hostname string) net.IP {
|
|
|
|
iprecords, _ := net.LookupIP(hostname)
|
|
|
|
randomIp := RandomIP(iprecords)
|
|
|
|
return randomIp
|
|
|
|
}
|
|
|
|
|
|
|
|
func reverseLookup(ipAddr net.IP) string {
|
|
|
|
ptr, _ := net.LookupAddr(ipAddr.String())
|
|
|
|
return ptr[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetApiHost() string {
|
|
|
|
appHostIp := nslookup(api())
|
|
|
|
apiHost := reverseLookup(appHostIp)
|
|
|
|
return apiHost
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:42:30 +00:00
|
|
|
func GetStations(qstring string) ([]stationRecord, error){
|
2021-03-16 19:27:52 +00:00
|
|
|
urlstr := fmt.Sprintf("https://%s/json/stations/search?%s&limit=%d",GetApiHost(),qstring,maxitems())
|
2021-03-16 14:46:28 +00:00
|
|
|
resp, err := http.Get(urlstr)
|
|
|
|
if err != nil {
|
2021-03-17 13:45:54 +00:00
|
|
|
log.Print(err.Error())
|
2021-03-16 14:46:28 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2021-03-16 16:42:30 +00:00
|
|
|
|
|
|
|
var data []stationRecord
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:27:52 +00:00
|
|
|
func pruneStations(stations []stationRecord) []stationRecord {
|
|
|
|
filteredStations := stations[:0]
|
|
|
|
for _, station := range stations {
|
|
|
|
if station.Lastcheck == 1 {
|
|
|
|
filteredStations = append(filteredStations, station)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredStations
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:17:10 +00:00
|
|
|
func StationSearch(name string, country string, state string, tags string, notok bool) ([]stationRecord, error) {
|
2021-03-16 19:27:52 +00:00
|
|
|
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
|
|
|
|
}
|
2021-03-16 20:17:10 +00:00
|
|
|
if notok{
|
|
|
|
return stations, err
|
|
|
|
} // otherwise prune the list
|
2021-03-16 19:27:52 +00:00
|
|
|
prunedStations := pruneStations(stations) // eliminate stations that are reporting down.
|
|
|
|
return prunedStations, err
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:42:30 +00:00
|
|
|
|