-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.go
97 lines (76 loc) · 1.58 KB
/
metadata.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"html"
"os/exec"
"time"
)
func watchPlayerMetaData(player, data, placeholder string) {
infoChannel := make(chan string)
go watchMetaData(player, data, infoChannel)
go watchMetaDataStatus(player, infoChannel)
s := status(player)
if placeholder != "" {
info := Info{
Class: s,
Text: html.EscapeString(placeholder),
}
b, err := json.Marshal(&info)
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
for val := range infoChannel {
var text string
if val == "STATUSCHANGED" {
s = status(player)
time.Sleep(100 * time.Millisecond)
text = metadata(player, data)
} else {
text = val
}
if text == "" {
text = placeholder
}
info := Info{
Class: s,
Text: html.EscapeString(text),
}
b, err := json.Marshal(&info)
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
}
func watchMetaData(player, data string, infoChannel chan string) {
cmd := exec.Command("playerctl", fmt.Sprintf("--player=%s", player), "metadata", data, "-F")
pipe, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(pipe)
go func() {
for scanner.Scan() {
infoChannel <- scanner.Text()
}
}()
cmd.Start()
}
func watchMetaDataStatus(player string, infoChannel chan string) {
cmd := exec.Command("playerctl", fmt.Sprintf("--player=%s", player), "status", "-F")
pipe, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(pipe)
go func() {
for scanner.Scan() {
infoChannel <- "STATUSCHANGED"
}
}()
cmd.Start()
}