-
Notifications
You must be signed in to change notification settings - Fork 0
/
ym.go
302 lines (261 loc) · 5.37 KB
/
ym.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package ym
import (
"bufio"
"fmt"
"net"
"strings"
"github.com/frizinak/ym/cache"
"github.com/frizinak/ym/command"
"github.com/frizinak/ym/player"
"github.com/frizinak/ym/playlist"
"github.com/frizinak/ym/search"
)
type YM struct {
playlist *playlist.Playlist
search search.Engine
player player.Player
cache *cache.Cache
state string
current search.Result
addr *net.TCPAddr
preflights int
}
func New(
playlist *playlist.Playlist,
search search.Engine,
player player.Player,
cache *cache.Cache,
sock *net.TCPAddr,
downloadPreflights int,
) *YM {
return &YM{
playlist,
search,
player,
cache,
"stop",
nil,
sock,
downloadPreflights,
}
}
func (ym *YM) ExecSearch(q string, amount int) ([]search.Result, error) {
results := make([]search.Result, 0, amount)
page := 0
for {
_results, err := ym.search.Search(q, page)
if err != nil {
return nil, err
}
page++
results = append(results, _results...)
if len(_results) == 0 || len(results) >= amount {
break
}
}
return results, nil
}
func (ym *YM) ExecPage(url string) ([]search.Result, error) {
return ym.search.Page(url)
}
func (ym *YM) Listen() error {
l, err := net.ListenTCP("tcp", ym.addr)
if err != nil {
return err
}
for {
conn, err := l.Accept()
if err != nil {
return err
}
go func(conn net.Conn) {
r := bufio.NewReader(conn)
for {
line, _, err := r.ReadLine()
if err != nil {
break
}
var msg []string
switch string(line) {
case "close":
conn.Close()
return
case "status":
msg = []string{
"volume: -1",
"repeat: 0",
"random: 0",
"single: 0",
"consume: 0",
"playlist: 1",
fmt.Sprintf("playlistlength: %d", ym.playlist.Length()),
"mixrampdb: 0.000000",
fmt.Sprintf("state: %s", ym.state),
}
case "currentsong":
cur := ym.current
if cur == nil {
break
}
info, err := cur.Info()
if err != nil {
break
}
msg = []string{
fmt.Sprintf("file: %s", cur.PageURL().String()),
fmt.Sprintf("Last-Modified: %s", info.Created().String()),
"Artist: -",
fmt.Sprintf("Title: %s", info.Title()),
"Track: 1",
fmt.Sprintf("Date: %d", info.Created().Year()),
"Genre: -",
"Composer: -",
fmt.Sprintf("Time: %d", int(info.Duration().Seconds())),
fmt.Sprintf("duration: %0.3f", info.Duration().Seconds()),
"Pos: 1",
"Id: -",
}
}
if len(msg) != 0 {
fmt.Fprint(conn, strings.Join(msg, "\n"))
}
}
}(conn)
}
}
func (ym *YM) Play(
queue <-chan *command.Command,
current chan<- search.Result,
status chan<- string,
errs chan<- error,
quit <-chan struct{},
) error {
var commands chan player.Command
iq := make(chan *command.Command)
wait := make(chan func())
go func() {
for {
iq <- ym.playlist.Read()
if w := <-wait; w != nil {
w()
}
ym.state = "stop"
status <- "■"
current <- nil
ym.current = nil
}
}()
for {
select {
case <-quit:
if commands != nil {
commands <- player.CmdStop
}
return nil
case c := <-iq:
result := c.Result()
if result == nil {
continue
}
var file string
// TODO
// if c.Cmd() != '!' {
cached := ym.cache.Get(result.ID())
if cached != nil {
file = cached.Path()
}
// }
if file == "" {
u, err := result.DownloadURLs()
if err != nil {
errs <- err
wait <- nil
continue
}
du, err := u.Find(ym.preflights)
if err != nil {
errs <- err
wait <- nil
continue
}
file = du.String()
}
params := []player.Param{player.ParamSilent}
// TODO
// if c.Cmd() != '!' {
params = append(params, player.ParamNoVideo)
// }
var err error
var w func()
commands, w, err = ym.player.Spawn(file, params)
ym.state = "play"
status <- "▶"
current <- result
wait <- w
ym.current = result
if err != nil {
errs <- err
continue
}
case cmd := <-queue:
var c player.Command = player.CmdNil
if choice := cmd.Choice(); choice > 0 {
ym.playlist.SetIndex(choice - 1)
cmd = command.New([]rune{'>'})
}
if cmd.Next() {
ym.playlist.Next(1)
c = player.CmdStop
} else if cmd.Prev() {
ym.playlist.Prev(1)
c = player.CmdStop
} else if from, to := cmd.Move(); from != 0 && to != 0 {
ym.playlist.Move(from-1, to-1)
} else if ints := cmd.Delete(); len(ints) != 0 {
ix := ym.playlist.Index()
for i := range ints {
ints[i]--
if ix == ints[i] {
c = player.CmdStop
}
}
ym.playlist.Del(ints)
} else if cmd.Clear() {
ym.playlist.Truncate()
c = player.CmdStop
} else if cmd.Pause() {
switch ym.state {
case "pause":
status <- "▶"
ym.state = "play"
case "play":
status <- "⏸"
ym.state = "pause"
}
c = player.CmdPause
} else if y := cmd.Scroll(); y != 0 {
ym.playlist.Scroll(y)
} else if ud := cmd.Volume(); ud != 0 {
c = player.CmdVolUp
if ud < 0 {
c = player.CmdVolDown
}
} else if cmd.SeekBack() {
c = player.CmdSeekBackward
} else if cmd.SeekForward() {
c = player.CmdSeekForward
} else if cmd.Rand() {
ym.playlist.ToggleRandom()
}
if c != player.CmdNil {
if commands != nil {
commands <- c
if c == player.CmdStop {
close(commands)
commands = nil
}
}
}
}
}
}