/
icy.go
58 lines (49 loc) · 1.28 KB
/
icy.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
package sources
import (
"birnenlabs.com/go/lib/icy"
"context"
"github.com/golang/glog"
"strings"
"time"
)
type icySource struct {
}
func newIcy() SongSource {
return &icySource{}
}
const timeout = time.Hour * 6
func (s *icySource) Start(ctx context.Context, conf SourceJob, song chan<- Song) error {
// channel accepted by the icy listener
title := make(chan string, 10)
// Thread that is listening to icy stream and pushing data into title channel
go s.startStreaming(title, song, conf)
// Thread that is parsing title channel and putting it into songs channel.
go s.monitorTitleChannel(ctx, title, song, conf)
return nil
}
func (s *icySource) startStreaming(title chan string, song chan<- Song, conf SourceJob) {
defer close(title)
err := icy.OpenWithTimeout(conf.SourceUrl, title, timeout)
glog.V(1).Infof("%v", err)
song <- Song{
Error: err,
}
}
func (s *icySource) monitorTitleChannel(ctx context.Context, title <-chan string, song chan<- Song, conf SourceJob) {
defer close(song)
var t string
ok := true
for ok {
t, ok = <-title
if len(t) > 5 {
for substr, replacement := range conf.SubstrMap {
t = strings.Replace(t, substr, replacement, -1)
}
glog.V(2).Infof("Song found: %q", t)
song <- Song{
ArtistTitle: t,
Error: nil,
}
}
}
}