forked from cgzirim/seek-tune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketHandlers.go
180 lines (152 loc) · 4.51 KB
/
socketHandlers.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
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"song-recognition/db"
"song-recognition/shazam"
"song-recognition/spotify"
"song-recognition/utils"
"song-recognition/wav"
socketio "github.com/googollee/go-socket.io"
"github.com/mdobak/go-xerrors"
)
func status(statusType, message string) string {
data := map[string]interface{}{"type": statusType, "message": message}
jsonData, err := json.Marshal(data)
if err != nil {
logger := utils.GetLogger()
ctx := context.Background()
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to marshal data", slog.Any("error", err))
return ""
}
return string(jsonData)
}
func handleTotalSongs(socket socketio.Conn) {
logger := utils.GetLogger()
ctx := context.Background()
db, err := db.NewDBClient()
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error connecting to DB", slog.Any("error", err))
return
}
defer db.Close()
totalSongs, err := db.TotalSongs()
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "Log error getting total songs", slog.Any("error", err))
return
}
socket.Emit("totalSongs", totalSongs)
}
func handleSongExists(socket socketio.Conn, phiZoneID string) {
logger := utils.GetLogger()
ctx := context.Background()
db, err := db.NewDBClient()
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error connecting to DB", slog.Any("error", err))
return
}
defer db.Close()
exists, err := db.SongExistsByID(phiZoneID)
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error checking song existence", slog.Any("error", err))
return
}
socket.Emit("songExists", exists)
}
func handleSongsUnsaved(socket socketio.Conn, requestedIDs []string) {
logger := utils.GetLogger()
ctx := context.Background()
db, err := db.NewDBClient()
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error connecting to DB", slog.Any("error", err))
return
}
defer db.Close()
nonExistentIDs, err := db.FindNonExistentSongs(requestedIDs)
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error checking for non-existent songs", slog.Any("error", err))
return
}
socket.Emit("songsUnsaved", nonExistentIDs)
}
func handleSave(socket socketio.Conn, songPath string, title string, artist string, pzID string) {
logger := utils.GetLogger()
ctx := context.Background()
// check if track already exist
db, err := db.NewDBClient()
if err != nil {
logger.ErrorContext(ctx, "error connecting to DB", slog.Any("error", err))
}
defer db.Close()
song, songExists, err := db.GetSongByKey(utils.GenerateSongKey(title, artist))
if err == nil {
if songExists {
statusMsg := fmt.Sprintf(
"'%s' by '%s' already exists in the database (https://www.phi.zone/songs/%s)",
song.Title, song.Artist, song.SongID)
socket.Emit("saveStatus", status("error", statusMsg))
return
}
} else {
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to get song by key", slog.Any("error", err))
}
err = spotify.ProcessAndSaveSong(songPath, title, artist, pzID)
if err != nil {
socket.Emit("saveStatus", status("error", err.Error()))
logger.Info(err.Error())
return
}
statusMsg := ""
statusMsg = fmt.Sprintf("'%s' by '%s' was saved", title, artist)
socket.Emit("saveStatus", status("success", statusMsg))
}
func handleFind(socket socketio.Conn, songFilePath string) {
logger := utils.GetLogger()
wavFilePath, err := wav.ConvertToWAV(songFilePath, 1)
if err != nil {
socket.Emit("findStatus", status("error", err.Error()))
logger.Info(err.Error())
return
}
wavInfo, err := wav.ReadWavInfo(wavFilePath)
if err != nil {
socket.Emit("findStatus", status("error", err.Error()))
logger.Info(err.Error())
return
}
samples, err := wav.WavBytesToSamples(wavInfo.Data)
if err != nil {
socket.Emit("findStatus", status("error", err.Error()))
logger.Info(err.Error())
return
}
matches, searchDuration, err := shazam.FindMatches(samples, wavInfo.Duration, wavInfo.SampleRate)
if err != nil {
socket.Emit("findStatus", status("error", err.Error()))
logger.Info(err.Error())
return
}
var simplifiedMatches []map[string]interface{}
for _, match := range matches {
simplifiedMatches = append(simplifiedMatches, map[string]interface{}{
"id": match.PhiZoneID,
"timestamp": match.Timestamp,
"score": match.Score,
})
}
socket.Emit("findResult", map[string]interface{}{
"matches": simplifiedMatches,
"timeTook": searchDuration.Seconds(),
})
logger.Info("Find matches emitted successfully")
}