-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (185 loc) · 4.92 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
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
package main
import (
"embed"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"sync"
"time"
"github.com/emmaly/musicstate/winapi"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true // Allow all origins
},
}
type Song struct {
Artist string `json:"artist"`
Song string `json:"song"`
AlbumArt string `json:"albumArt"`
}
type SongUpdate struct {
Type string `json:"type"` // "change" or "stop"
Artist string `json:"artist"` // Only used for "change"
Song string `json:"song"` // Only used for "change"
AlbumArt string `json:"albumArt"` // Only used for "change"
}
type Server struct {
song *Song
connections []*websocket.Conn
mu sync.Mutex
}
//go:embed static/*
var staticFiles embed.FS
func main() {
fileServer := http.FileServer(http.FS(staticFiles))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Rewrite the path to include "static"
r.URL.Path = "/static" + r.URL.Path
fileServer.ServeHTTP(w, r)
})
// Handle WebSocket connections
http.HandleFunc("/ws", wsHandler())
hostAddr := "localhost:52846"
fmt.Printf("SorceressEmmaly's MusicState\nCopyright (C) 2025 emmaly\nSee https://github.com/emmaly/musicstate for documentation, source code, and to file issues.\nThis program is licensed GPLv3; it comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it under certain conditions.\nReview license details at https://github.com/emmaly/musicstate/LICENSE\n\n\nUse http://%s as your browser source overlay URL.\n\n\n", hostAddr)
err := http.ListenAndServe(hostAddr, nil)
if err != nil {
log.Fatal("error starting service: ", err)
}
}
func wsHandler() http.HandlerFunc {
server := Server{}
// Start watching for music changes
go server.watchMusic()
return func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade error:", err)
return
}
defer conn.Close()
server.addConnection(conn)
defer server.removeConnection(conn)
if server.song != nil {
// Send the current song to the new connection
err = conn.WriteJSON(server.song.AsSongUpdate())
if err != nil {
log.Println("Write error:", err)
return
}
}
// Keep connection alive and handle any incoming messages
for {
_, _, err := conn.ReadMessage()
if err != nil {
log.Println("Read error:", err)
break
}
}
}
}
func (s *Song) AsSongUpdate() SongUpdate {
if s != nil {
return SongUpdate{
Type: "change",
Artist: s.Artist,
Song: s.Song,
AlbumArt: s.AlbumArt,
}
}
return SongUpdate{Type: "stop"}
}
func (server *Server) reportMusic(song *Song) {
server.mu.Lock()
defer server.mu.Unlock()
if (server.song == nil && song == nil) ||
(server.song != nil &&
song != nil &&
server.song.Song == song.Song &&
server.song.Artist == song.Artist &&
server.song.AlbumArt == song.AlbumArt) {
return // nothing to do
}
server.song = song
if server.connections == nil {
return
}
update := song.AsSongUpdate()
// Send to all connected clients
for _, conn := range server.connections {
err := conn.WriteJSON(update)
if err != nil {
log.Println("Write error:", err)
}
}
}
func (server *Server) addConnection(conn *websocket.Conn) {
server.mu.Lock()
defer server.mu.Unlock()
if server.connections == nil {
server.connections = make([]*websocket.Conn, 0)
}
server.connections = append(server.connections, conn)
}
func (server *Server) removeConnection(conn *websocket.Conn) {
server.mu.Lock()
defer server.mu.Unlock()
if server.connections == nil {
return
}
for i, c := range server.connections {
if c == conn {
server.connections = append(server.connections[:i], server.connections[i+1:]...)
break
}
}
}
func (server *Server) watchMusic() {
const (
tidal = "TIDAL.exe"
spotify = "Spotify.exe"
)
for {
windows, err := winapi.FindWindowsByProcess(
[]string{tidal, spotify},
winapi.WinVisible(true),
winapi.WinClass("Chrome_WidgetWin_1"),
winapi.WinTitlePattern(*regexp.MustCompile("-")),
)
if err != nil {
log.Fatal(err)
}
var song *Song
for _, w := range windows {
delimiter := " - "
songParts := strings.Split(w.Title, delimiter)
if len(songParts) >= 2 {
if w.ProcessName == tidal {
song = &Song{
Artist: songParts[len(songParts)-1],
Song: strings.Join(songParts[:len(songParts)-1], delimiter),
}
} else if w.ProcessName == spotify {
song = &Song{
Artist: songParts[0],
Song: strings.Join(songParts[1:], delimiter),
}
}
}
if song != nil {
break
}
}
if song != nil && song.Song != "" && song.AlbumArt == "" {
// set the placeholder album art
song.AlbumArt = "http://localhost:52846/images/album.jpg"
}
server.reportMusic(song)
time.Sleep(1 * time.Second)
}
}