@@ -8,12 +8,11 @@ import kotlinx.coroutines.*
8
8
import org.abimon.eternalJukebox.EternalJukebox
9
9
import org.abimon.eternalJukebox.bearer
10
10
import org.abimon.eternalJukebox.exponentiallyBackoff
11
- import org.abimon.eternalJukebox.objects.*
12
- import org.abimon.eternalJukebox.tryReadValue
13
- import org.abimon.visi.io.ByteArrayDataSource
11
+ import org.abimon.eternalJukebox.objects.ClientInfo
12
+ import org.abimon.eternalJukebox.objects.JukeboxInfo
13
+ import org.abimon.eternalJukebox.objects.SpotifyError
14
14
import org.slf4j.Logger
15
15
import org.slf4j.LoggerFactory
16
- import java.util.*
17
16
import java.util.concurrent.atomic.AtomicReference
18
17
19
18
@OptIn(DelicateCoroutinesApi ::class )
@@ -111,164 +110,6 @@ object SpotifyAnalyser : IAnalyser {
111
110
return array.toTypedArray()
112
111
}
113
112
114
- override suspend fun analyse (id : String , clientInfo : ClientInfo ? ): JukeboxTrack ? {
115
- var error: SpotifyError ? = null
116
- var track: JukeboxTrack ? = null
117
- val info = getInfo(id, clientInfo)
118
-
119
- if (info == null ) {
120
- logger.warn(" [{}] Failed to analyse {} on Spotify; track info was null" , clientInfo?.userUID, id)
121
- return null
122
- }
123
-
124
- val success = exponentiallyBackoff(16000 , 8 ) {
125
- logger.trace(" [{}] Attempting to analyse {} on Spotify" , clientInfo?.userUID, id)
126
- val (_, response, _) = Fuel .get(" https://api.spotify.com/v1/audio-analysis/$id " )
127
- .bearer(token.get())
128
- .awaitStringResponseResult()
129
- val mapResponse =
130
- withContext(Dispatchers .IO ) { EternalJukebox .jsonMapper.tryReadValue(response.data, Map ::class ) }
131
-
132
- when (response.statusCode) {
133
- 200 -> {
134
- if (mapResponse == null ) {
135
- val name = " SPOTIFY-RESPONSE-200-${UUID .randomUUID()} .txt"
136
- if (EternalJukebox .storage.shouldStore(EnumStorageType .LOG ) && EternalJukebox .storage.store(
137
- name,
138
- EnumStorageType .LOG ,
139
- ByteArrayDataSource (response.data),
140
- " text/plain" ,
141
- clientInfo
142
- )
143
- ) {
144
- logger.warn(
145
- " [{}] Got back response code 200; invalid response body however; saved as {}" ,
146
- clientInfo?.userUID,
147
- name
148
- )
149
- return @exponentiallyBackoff true
150
- } else {
151
- logger.warn(
152
- " [{}] Got back response code 200; invalid response body however; did not save due to an error or log saving being disabled" ,
153
- clientInfo?.userUID
154
- )
155
- return @exponentiallyBackoff true
156
- }
157
- }
158
-
159
- val obj = JsonObject (mapResponse.mapKeys { (key) -> " $key " })
160
- track = JukeboxTrack (
161
- info,
162
- withContext(Dispatchers .IO ) {
163
- JukeboxAnalysis (
164
- EternalJukebox .jsonMapper.readValue(
165
- obj.getJsonArray(" sections" ).toString(),
166
- Array <SpotifyAudioSection >::class .java
167
- ),
168
- EternalJukebox .jsonMapper.readValue(
169
- obj.getJsonArray(" bars" ).toString(),
170
- Array <SpotifyAudioBar >::class .java
171
- ),
172
- EternalJukebox .jsonMapper.readValue(
173
- obj.getJsonArray(" beats" ).toString(),
174
- Array <SpotifyAudioBeat >::class .java
175
- ),
176
- EternalJukebox .jsonMapper.readValue(
177
- obj.getJsonArray(" tatums" ).toString(),
178
- Array <SpotifyAudioTatum >::class .java
179
- ),
180
- EternalJukebox .jsonMapper.readValue(
181
- obj.getJsonArray(" segments" ).toString(),
182
- Array <SpotifyAudioSegment >::class .java
183
- )
184
- )
185
- },
186
- JukeboxSummary ((mapResponse[" track" ] as Map <* , * >)[" duration" ] as Double )
187
- )
188
-
189
- return @exponentiallyBackoff false
190
- }
191
- 400 -> {
192
- if (mapResponse == null ) {
193
- val name = " SPOTIFY-RESPONSE-400-${UUID .randomUUID()} .txt"
194
- if (EternalJukebox .storage.shouldStore(EnumStorageType .LOG ) && EternalJukebox .storage.store(
195
- name,
196
- EnumStorageType .LOG ,
197
- ByteArrayDataSource (response.data),
198
- " text/plain" ,
199
- clientInfo
200
- )
201
- ) {
202
- logger.warn(
203
- " [{}] Got back response code 400; invalid response body however; saved as {}" ,
204
- clientInfo?.userUID,
205
- name
206
- )
207
- return @exponentiallyBackoff true
208
- } else {
209
- logger.warn(
210
- " [{}] Got back response code 400; invalid response body however; did not save due to an error or log saving being disabled" ,
211
- clientInfo?.userUID
212
- )
213
- return @exponentiallyBackoff true
214
- }
215
- }
216
-
217
- if (((mapResponse[" error" ] as Map <* , * >)[" message" ] as String ) == " Only valid bearer authentication supported" ) {
218
- logger.error(
219
- " [{}] Got back response code 400 with error \" Only valid bearer authentication supported\" ; reloading token, backing off, and trying again" ,
220
- clientInfo?.userUID
221
- )
222
- reload()
223
- return @exponentiallyBackoff true
224
- } else {
225
- logger.error(
226
- " [{}] Got back response code 400 with data \" {}\" ; returning INVALID_SEARCH_DATA" ,
227
- clientInfo?.userUID,
228
- response.body().asString(response.header(" Content-Type" ).firstOrNull())
229
- )
230
- error = SpotifyError .INVALID_SEARCH_DATA
231
- return @exponentiallyBackoff false
232
- }
233
- }
234
- 401 -> {
235
- logger.error(
236
- " [{}] Got back response code 401 with data \" {}\" ; reloading token, backing off, and trying again" ,
237
- clientInfo?.userUID,
238
- response.body().asString(response.header(" Content-Type" ).firstOrNull())
239
- )
240
- reload()
241
- return @exponentiallyBackoff true
242
- }
243
- 429 -> {
244
- val backoff = response.header(" Retry-After" ).firstOrNull()?.toIntOrNull() ? : 4
245
- logger.warn(
246
- " [{}] Got back response code 429; waiting {} seconds before trying again" ,
247
- clientInfo?.userUID, backoff
248
- )
249
- delay(backoff * 1000L )
250
- return @exponentiallyBackoff true
251
- }
252
- else -> {
253
- logger.warn(
254
- " [{}] Got back response code {} with data \" {}\" ; backing off and trying again" ,
255
- clientInfo?.userUID,
256
- response.statusCode,
257
- response.body().asString(response.header(" Content-Type" ).firstOrNull())
258
- )
259
- return @exponentiallyBackoff true
260
- }
261
- }
262
- } && error == null
263
-
264
- if (success)
265
- logger.trace(" [{}] Successfully analysed {} from Spotify" , clientInfo?.userUID, id)
266
- else
267
- logger.warn(" [{}] Failed to analyse {}. Error: {}" , clientInfo?.userUID, id, error)
268
-
269
- return track
270
- }
271
-
272
113
override suspend fun getInfo (id : String , clientInfo : ClientInfo ? ): JukeboxInfo ? {
273
114
var error: SpotifyError ? = null
274
115
var track: JukeboxInfo ? = null
0 commit comments