-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist.go
103 lines (83 loc) · 1.92 KB
/
playlist.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
package main
import (
"html/template"
"net/http"
"go.tmthrgd.dev/spork/dbus"
"go.tmthrgd.dev/spork/web"
"golang.org/x/sync/errgroup"
)
var playlistTmpl = web.NewTemplate("playlist.tmpl", template.FuncMap{
"FormatLength": formatLength,
})
func playlistHandler() http.HandlerFunc {
return web.ErrorHandler(func(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
entries, err := dbus.GetPlaylistLength(ctx)
if err != nil {
return err
}
type playlistEntry struct {
Title, Name, Artist, Album string
Length, Year int32
}
playlist := make([]playlistEntry, entries)
g, gctx := errgroup.WithContext(ctx)
sem := make(chan struct{}, 8)
for entry := uint32(0); entry < uint32(entries); entry++ {
select {
case sem <- struct{}{}:
case <-gctx.Done():
return g.Wait()
}
entry := entry
g.Go(func() error {
defer func() { <-sem }()
title, err := dbus.GetSongTitle(gctx, entry)
if err != nil {
return err
}
length, err := dbus.GetSongLength(gctx, entry)
if err != nil {
return err
}
name, err := dbus.GetSongName(gctx, entry)
if err != nil {
return err
}
artist, err := dbus.GetSongArtist(gctx, entry)
if err != nil {
return err
}
album, err := dbus.GetSongAlbum(gctx, entry)
if err != nil {
return err
}
year, err := dbus.GetSongYear(gctx, entry)
if err != nil {
return err
}
playlist[entry] = playlistEntry{
title, name, artist, album,
length, year,
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
name, err := dbus.GetPlaylistName(ctx)
if err != nil {
return err
}
active, err := dbus.GetPlaylistPosition(ctx)
if err != nil {
return err
}
return web.WriteTemplateResponse(w, playlistTmpl, &struct {
Name string
Entries []playlistEntry
Active uint32
}{name, playlist, active})
})
}