-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (158 loc) · 4.25 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright Remi Reuvekamp 2021
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"github.com/PuerkitoBio/goquery"
)
func main() {
var (
player = flag.String("player", "/usr/bin/mpv", "full filesystem path to the preferred player")
fileType = flag.String("filetype", "pls", "file format to play from the multiple options the website gives (pls, ram, asx, qtl)")
)
flag.Parse()
streams, err := fetchStreams()
if err != nil {
fmt.Println("Error fetching streams:")
fmt.Println(err)
os.Exit(5)
}
if len(streams) == 0 {
fmt.Println("No streams could be fetched.")
os.Exit(7)
}
printStreams(streams)
chosenStream := chooseStream(streams)
fmt.Printf("\nPlaying %s\n", chosenStream.title)
err = playStream(chosenStream, *fileType, *player)
if err != nil {
fmt.Println("Error playing stream:")
fmt.Println(err)
os.Exit(6)
}
}
type stream struct {
title string
freq string
phone string
location string
streamInfo string
urls map[string]string
}
func printStreams(streams []stream) {
for i, stream := range streams {
fmt.Printf("%d -> %s\n", i, stream.title)
fmt.Printf(" %-15s %s\n", stream.location, stream.freq)
fmt.Printf(" %s\n\n", stream.phone)
}
}
func chooseStream(streams []stream) stream {
var chosenStream *stream
for chosenStream == nil {
fmt.Print("Which would you like to play?\n> ")
var i int
_, err := fmt.Scanf("%d\n", &i)
if err != nil {
fmt.Println(err)
continue
}
if i < len(streams) && i >= 0 {
chosenStream = &streams[i]
}
}
return *chosenStream
}
func playStream(stream stream, preferedFile string, player string) error {
file, ok := stream.urls[preferedFile]
if !ok {
return fmt.Errorf("stream has no file with type: %s", preferedFile)
}
cmd := exec.Command(player, file)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err := cmd.Run()
return err
}
func fetchStreams() ([]stream, error) {
res, err := http.Get("https://geheimezender.com/streamdata-desktop.php")
if err != nil {
return []stream{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return []stream{}, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return []stream{}, err
}
streams := []stream{}
doc.Find(".box").Each(func(i int, s *goquery.Selection) {
streams = append(streams, parseStream(s))
})
return streams, nil
}
func parseStream(s *goquery.Selection) stream {
streamInfo := s.Find("#aantalbezoekers").Text()
streamInfos := strings.SplitN(streamInfo, " ", 2)
streamInfos = trimAndExtend(streamInfos, 2)
infoEl := s.Find(".streamtitle")
info, err := infoEl.Html()
if err != nil {
info = infoEl.Text()
}
infos := strings.Split(info, "<br/>")
infos = trimAndExtend(infos, 3)
urls := parseURLs(s.Find(".images_online a"))
return stream{
strings.Replace(strings.TrimSpace(infos[1]), "&", "&", -1),
strings.TrimSpace(infos[0]),
strings.TrimSpace(infos[2]),
streamInfos[0],
streamInfos[1],
urls}
}
func trimAndExtend(list []string, length int) []string {
output := make([]string, length)
for i, item := range list {
if i >= length {
break
}
output[i] = strings.TrimSpace(item)
}
return output
}
func parseURLs(s *goquery.Selection) map[string]string {
urls := map[string]string{}
s.Each(func(_ int, s *goquery.Selection) {
url, ok := s.Attr("href")
if !ok {
return
}
if !strings.HasPrefix(url, "http") {
return
}
split := strings.Split(url, ".")
extension := split[len(split)-1]
urls[extension] = url
})
return urls
}