-
Notifications
You must be signed in to change notification settings - Fork 116
/
socketHandlers.go
214 lines (183 loc) · 6.14 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"song-recognition/db"
"song-recognition/models"
"song-recognition/shazam"
"song-recognition/spotify"
"song-recognition/utils"
"strings"
socketio "github.com/googollee/go-socket.io"
"github.com/mdobak/go-xerrors"
)
func downloadStatus(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 handleSongDownload(socket socketio.Conn, spotifyURL string) {
logger := utils.GetLogger()
ctx := context.Background()
// Handle album download
if strings.Contains(spotifyURL, "album") {
tracksInAlbum, err := spotify.AlbumInfo(spotifyURL)
if err != nil {
fmt.Println("log error: ", err)
if len(err.Error()) <= 25 {
socket.Emit("downloadStatus", downloadStatus("error", err.Error()))
logger.Info(err.Error())
} else {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error getting album info", slog.Any("error", err))
}
return
}
statusMsg := fmt.Sprintf("%v songs found in album.", len(tracksInAlbum))
socket.Emit("downloadStatus", downloadStatus("info", statusMsg))
totalTracksDownloaded, err := spotify.DlAlbum(spotifyURL, SONGS_DIR)
if err != nil {
socket.Emit("downloadStatus", downloadStatus("error", "Couldn't to download album."))
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to download album.", slog.Any("error", err))
return
}
statusMsg = fmt.Sprintf("%d songs downloaded from album", totalTracksDownloaded)
socket.Emit("downloadStatus", downloadStatus("success", statusMsg))
}
// Handle playlist download
if strings.Contains(spotifyURL, "playlist") {
tracksInPL, err := spotify.PlaylistInfo(spotifyURL)
if err != nil {
if len(err.Error()) <= 25 {
socket.Emit("downloadStatus", downloadStatus("error", err.Error()))
logger.Info(err.Error())
} else {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error getting album info", slog.Any("error", err))
}
return
}
statusMsg := fmt.Sprintf("%v songs found in playlist.", len(tracksInPL))
socket.Emit("downloadStatus", downloadStatus("info", statusMsg))
totalTracksDownloaded, err := spotify.DlPlaylist(spotifyURL, SONGS_DIR)
if err != nil {
socket.Emit("downloadStatus", downloadStatus("error", "Couldn't download playlist."))
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to download playlist.", slog.Any("error", err))
return
}
statusMsg = fmt.Sprintf("%d songs downloaded from playlist.", totalTracksDownloaded)
socket.Emit("downloadStatus", downloadStatus("success", statusMsg))
}
// Handle track download
if strings.Contains(spotifyURL, "track") {
trackInfo, err := spotify.TrackInfo(spotifyURL)
if err != nil {
if len(err.Error()) <= 25 {
socket.Emit("downloadStatus", downloadStatus("error", err.Error()))
logger.Info(err.Error())
} else {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error getting album info", slog.Any("error", err))
}
return
}
// check if track already exist
db, err := db.NewDBClient()
if err != nil {
fmt.Errorf("Log - error connecting to DB: %d", err)
}
defer db.Close()
song, songExists, err := db.GetSongByKey(utils.GenerateSongKey(trackInfo.Title, trackInfo.Artist))
if err == nil {
if songExists {
statusMsg := fmt.Sprintf(
"'%s' by '%s' already exists in the database (https://www.youtube.com/watch?v=%s)",
song.Title, song.Artist, song.YouTubeID)
socket.Emit("downloadStatus", downloadStatus("error", statusMsg))
return
}
} else {
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to get song by key.", slog.Any("error", err))
}
totalDownloads, err := spotify.DlSingleTrack(spotifyURL, SONGS_DIR)
if err != nil {
if len(err.Error()) <= 25 {
socket.Emit("downloadStatus", downloadStatus("error", err.Error()))
logger.Info(err.Error())
} else {
err := xerrors.New(err)
logger.ErrorContext(ctx, "error getting album info", slog.Any("error", err))
}
return
}
statusMsg := ""
if totalDownloads != 1 {
statusMsg = fmt.Sprintf("'%s' by '%s' failed to download", trackInfo.Title, trackInfo.Artist)
socket.Emit("downloadStatus", downloadStatus("error", statusMsg))
} else {
statusMsg = fmt.Sprintf("'%s' by '%s' was downloaded", trackInfo.Title, trackInfo.Artist)
socket.Emit("downloadStatus", downloadStatus("success", statusMsg))
}
}
}
func handleNewRecording(socket socketio.Conn, recordData string) {
logger := utils.GetLogger()
ctx := context.Background()
var recData models.RecordData
if err := json.Unmarshal([]byte(recordData), &recData); err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "Failed to unmarshal record data.", slog.Any("error", err))
return
}
samples, err := utils.ProcessRecording(&recData, true)
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "Failed to process recording.", slog.Any("error", err))
return
}
matches, _, err := shazam.FindMatches(samples, recData.Duration, recData.SampleRate)
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to get matches.", slog.Any("error", err))
}
jsonData, err := json.Marshal(matches)
if len(matches) > 10 {
jsonData, _ = json.Marshal(matches[:10])
}
if err != nil {
err := xerrors.New(err)
logger.ErrorContext(ctx, "failed to marshal matches.", slog.Any("error", err))
return
}
socket.Emit("matches", string(jsonData))
}