-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
287 lines (237 loc) · 5.65 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"bufio"
"encoding/xml"
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/net/html/charset"
)
// Channel represents a SomaFM channel
type Channel struct {
ID string `xml:"id,attr"`
Title string `xml:"title"`
Description string `xml:"description"`
Genre string `xml:"genre"`
Image string `xml:"image"`
DJ string `xml:"dj"`
Listeners int `xml:"listeners"`
FastPLS string `xml:"fastpls"`
}
// Channels represents the root XML element
type Channels struct {
XMLName xml.Name `xml:"channels"`
Channels []Channel `xml:"channel"`
}
type playerState int
const (
stopped playerState = iota
playing
titleWidth = 30
genreWidth = 25
statsWidth = 10
)
// model represents the application state
type model struct {
channels []Channel
cursor int
selected *Channel
err error
loading bool
playerState playerState
player *exec.Cmd
}
// Define some basic styling
var (
titleStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FF6B6B"))
selectedStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#4ECDC4"))
errorStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FF0000"))
)
func initialModel() model {
return model{
loading: true,
}
}
// Message types
type channelsMsg []Channel
type errMsg struct{ error }
type startPlaybackMsg struct {
player *exec.Cmd
}
type stopPlaybackMsg struct{}
type playbackErrorMsg struct{ error }
func fetchChannels() tea.Msg {
resp, err := http.Get("https://somafm.com/channels.xml")
if err != nil {
return errMsg{err}
}
defer resp.Body.Close()
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReaderLabel
var channels Channels
if err := decoder.Decode(&channels); err != nil {
return errMsg{err}
}
for i := range channels.Channels {
channels.Channels[i].Description = strings.TrimSpace(channels.Channels[i].Description)
if fastPLS := channels.Channels[i].FastPLS; fastPLS != "" {
if idx := strings.Index(fastPLS, "\n"); idx != -1 {
channels.Channels[i].FastPLS = fastPLS[:idx]
}
}
}
return channelsMsg(channels.Channels)
}
// Function to parse PLS and get stream URL
func getStreamURL(plsURL string) (string, error) {
resp, err := http.Get(plsURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
re := regexp.MustCompile(`File1=(.+)`)
for scanner.Scan() {
if matches := re.FindStringSubmatch(scanner.Text()); len(matches) > 1 {
return matches[1], nil
}
}
return "", fmt.Errorf("no stream URL found in PLS file")
}
// Command to start playback using MPV
func startPlayback(streamURL string) tea.Cmd {
return func() tea.Msg {
cmd := exec.Command("mpv", streamURL, "--no-terminal")
if err := cmd.Start(); err != nil {
return playbackErrorMsg{err}
}
return startPlaybackMsg{cmd}
}
}
// Command to stop playback
func stopPlayback(cmd *exec.Cmd) tea.Cmd {
return func() tea.Msg {
if cmd != nil && cmd.Process != nil {
if err := cmd.Process.Kill(); err != nil {
return playbackErrorMsg{err}
}
}
return stopPlaybackMsg{}
}
}
func (m model) Init() tea.Cmd {
return fetchChannels
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
if m.player != nil {
return m, tea.Sequence(
stopPlayback(m.player),
tea.Quit,
)
}
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.channels)-1 {
m.cursor++
}
case "enter", " ":
if m.playerState == playing && m.selected == &m.channels[m.cursor] {
m.selected = nil
return m, stopPlayback(m.player)
}
var cmds []tea.Cmd
if m.player != nil {
cmds = append(cmds, stopPlayback(m.player))
}
m.selected = &m.channels[m.cursor]
streamURL, err := getStreamURL(m.selected.FastPLS)
if err != nil {
return m, func() tea.Msg {
return errMsg{err}
}
}
cmds = append(cmds, startPlayback(streamURL))
return m, tea.Sequence(cmds...)
}
case startPlaybackMsg:
m.playerState = playing
m.player = msg.player
case stopPlaybackMsg:
m.playerState = stopped
m.player = nil
case playbackErrorMsg:
m.err = msg.error
m.playerState = stopped
m.player = nil
case channelsMsg:
m.channels = msg
m.loading = false
case errMsg:
m.err = msg.error
m.loading = false
}
return m, nil
}
func (m model) View() string {
if m.loading {
return "Loading channels...\n"
}
if m.err != nil {
return errorStyle.Render(fmt.Sprintf("Error: %v\n", m.err))
}
s := titleStyle.Render("🎵 SomaFM Channels\n\n")
for i, channel := range m.channels {
cursor := " "
if i == m.cursor {
cursor = "> "
}
title := channel.Title
if len(title) > titleWidth-3 {
title = title[:titleWidth-3] + "..."
}
title = fmt.Sprintf("%-*s", titleWidth, title)
genre := channel.Genre
if len(genre) > genreWidth-3 {
genre = genre[:genreWidth-3] + "..."
}
genre = fmt.Sprintf("%-*s", genreWidth, genre)
line := fmt.Sprintf("%s%s %s [%d]\n",
cursor,
title,
genre,
channel.Listeners)
if i == m.cursor {
line = selectedStyle.Render(line)
}
s += line
}
if m.playerState == playing && m.selected != nil {
s += "\n" + titleStyle.Render(fmt.Sprintf("Now Playing: %s", m.selected.Title))
}
s += "\n(↑/↓) Navigate • (enter) Play/Stop • (q) Quit\n"
return s
}
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}