-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_radio.go
92 lines (82 loc) · 2.12 KB
/
service_radio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"time"
"github.com/gotk3/gotk3/gtk"
)
func (radio *RadioTab) SetRadioDirectoryServerIP(host string) error {
addrs, err := net.LookupHost(host)
var servers []string
if err != nil {
return err
}
for _, addr := range addrs {
ip := net.ParseIP(addr)
if ip != nil && ip.To4() != nil {
servers = append(servers, ip.String())
}
}
rand.Seed(time.Now().UnixNano())
num := rand.Intn(len(servers))
radio.directoryServerIp = servers[num]
return nil
}
func (radio *RadioTab) AdvancedStationSearch(name string, countryCode string, limit int) []Station {
if limit == -1 {
limit = 10
}
url := "http://" + radio.directoryServerIp + "/json/stations/search"
data := requestData{Name: name, CountryCode: countryCode, Limit: limit}
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return nil
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return nil
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/plain")
// send the HTTP request and print the response
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return nil
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil
}
stations, err := ParseJsonToRadios(body)
if err != nil {
panic(err)
}
return stations
}
func (radio *RadioTab) AddFoundStation(station Station) error {
stationRow, _ := gtk.ListBoxRowNew()
hbox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
vbox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
favicon := ImgDownload(station.Favicon, Conf.ICON_SIZE*2)
station.FaviconImage = favicon
hbox.Add(favicon)
nameLabel, _ := gtk.LabelNew(station.Name)
vbox.Add(nameLabel)
hbox.Add(vbox)
stationRow.Add(hbox)
radio.listbox.Add(stationRow)
radio.foundStations = append(radio.foundStations, station)
radio.listbox.ShowAll()
return nil
}