-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (91 loc) · 2.02 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
package main
import (
_ "embed"
"encoding/json"
"log"
"net/http"
"os"
"os/exec"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/getlantern/systray"
"github.com/shirou/gopsutil/v3/process"
)
//go:embed lofi.ico
var icon []byte
type beats struct {
cmd *exec.Cmd
isPlaying bool
}
func (b *beats) quit() {
pid, err := process.NewProcess(int32(b.cmd.Process.Pid))
if err != nil {
log.Fatal(err)
}
children, _ := pid.Children()
if len(children) != 0 {
for _, c := range children {
err = c.Kill()
if err != nil {
log.Fatal(err)
}
}
}
b.cmd.Process.Kill()
}
func parseYT() (streamURL string) {
type StreamPayload struct {
StreamingData struct {
HlsManifestUrl string
ExpiresInSeconds string
}
}
resp, err := http.Get("https://www.youtube.com/watch?v=jfKfPfyJRdk")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
doc.Find("script").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), "var ytInitialPlayerResponse") {
jSSlice := strings.Split(s.Text(), "var ytInitialPlayerResponse = ")
jsonBytes := []byte(strings.TrimSuffix(jSSlice[1], ";"))
var stream = &StreamPayload{}
err := json.Unmarshal(jsonBytes, stream)
if err != nil {
log.Fatal(err)
}
streamURL = stream.StreamingData.HlsManifestUrl
}
})
return streamURL
}
func (b *beats) onReady() {
systray.SetIcon(icon)
systray.SetTooltip("pipe lofi beats from the menubar")
go func() {
mPlay := systray.AddMenuItem("Play/Pause", "play/pause")
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit")
for {
select {
case <-mPlay.ClickedCh:
url := parseYT()
b.playPause(url)
case <-mQuit.ClickedCh:
b.quit()
systray.Quit()
}
}
}()
}
func (b *beats) onExit() {}
func main() {
p := os.Getenv("PATH")
os.Setenv("PATH","/usr/local/bin:/opt/homebrew/bin:"+p)
b := beats{isPlaying: false}
systray.Run(b.onReady, b.onExit)
}