This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (77 loc) · 1.91 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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/imroc/req"
)
// Artwork structure
type Artwork struct {
Url string `json:"url"`
}
// PlaybackInfo structure
type PlaybackInfo struct {
Info struct {
Artwork Artwork `json:"artwork"`
} `json:"info"`
}
var (
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
lastArtworkUrl string
)
func main() {
fmt.Println("Starting server...")
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.LoadHTMLGlob("templates/*")
fmt.Println("Server started, listening on port 3000")
fmt.Println("Attach this instance to NZXT CAM by providing the following URL: http://localhost:3000 and hitting the arrow button")
fmt.Println("NOTE: Don't have CAM? Download it here: https://www.nzxt.com/camapp")
// Websocket route
r.GET("/ws", func(c *gin.Context) {
conn, _ := upgrader.Upgrade(c.Writer, c.Request, nil)
go func() {
for {
playbackInfo := fetchPlaybackInfo()
artwork := playbackInfo.Info.Artwork.Url
if artwork != "" && artwork != lastArtworkUrl {
artwork = replace(artwork, "{w}", "600")
artwork = replace(artwork, "{h}", "600")
if err := conn.WriteMessage(websocket.TextMessage, []byte(artwork)); err != nil {
return
}
lastArtworkUrl = artwork
}
time.Sleep(1 * time.Second)
}
}()
})
// Home route
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{})
})
r.Run(":3000")
}
func fetchPlaybackInfo() PlaybackInfo {
header := req.Header{
"Accept": "application/json",
}
url := "http://localhost:10769/currentPlayingSong"
response, err := req.Get(url, header)
if err != nil {
log.Fatal(err)
}
playbackInfo := PlaybackInfo{}
response.ToJSON(&playbackInfo)
return playbackInfo
}
func replace(src, old, new string) string {
return strings.Replace(src, old, new, -1)
}