Skip to content

Commit f627bc8

Browse files
committed
Removed requesting new Spotify analysis data and added info message
1 parent 1c3717a commit f627bc8

File tree

7 files changed

+18
-199
lines changed

7 files changed

+18
-199
lines changed

_web/_includes/go-js.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@
326326
gotTheAnalysis(data);
327327
},
328328
error: function(xhr, textStatus, error) {
329+
if (xhr.responseJSON && typeof xhr.responseJSON.error === "string") {
330+
error = xhr.responseJSON.error;
331+
}
329332
info("Sorry, can't find info for that track: " + error)
330333
}
331334
});
@@ -2251,6 +2254,9 @@
22512254
gotTheAnalysis(data);
22522255
},
22532256
error: function(xhr, textStatus, error) {
2257+
if (xhr.responseJSON && typeof xhr.responseJSON.error === "string") {
2258+
error = xhr.responseJSON.error;
2259+
}
22542260
info("Sorry, can't find info for that track: " + error)
22552261
}
22562262
});

_web/files/styles.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ hr {
9292

9393

9494
#info {
95-
margin-left: 20px;
9695
margin-top: 10px;
9796
margin-bottom:10px;
9897
min-height:28px;

_web/retro_index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@
403403
gotTheAnalysis(data);
404404
},
405405
error: function(xhr, textStatus, error) {
406+
if (xhr.responseJSON && typeof xhr.responseJSON.error === "string") {
407+
error = xhr.responseJSON.error;
408+
}
406409
info("Sorry, can't find info for that track: " + error)
407410
}
408411
});

src/main/kotlin/org/abimon/eternalJukebox/data/analysis/IAnalyser.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.abimon.eternalJukebox.data.analysis
22

33
import org.abimon.eternalJukebox.objects.ClientInfo
44
import org.abimon.eternalJukebox.objects.JukeboxInfo
5-
import org.abimon.eternalJukebox.objects.JukeboxTrack
65

76
interface IAnalyser {
87
/**
@@ -11,13 +10,8 @@ interface IAnalyser {
1110
*/
1211
suspend fun search(query: String, clientInfo: ClientInfo?): Array<JukeboxInfo>
1312

14-
/**
15-
* Analyse the given ID
16-
*/
17-
suspend fun analyse(id: String, clientInfo: ClientInfo?): JukeboxTrack?
18-
1913
/**
2014
* Get track information from an ID
2115
*/
2216
suspend fun getInfo(id: String, clientInfo: ClientInfo?): JukeboxInfo?
23-
}
17+
}

src/main/kotlin/org/abimon/eternalJukebox/data/analysis/SpotifyAnalyser.kt

Lines changed: 3 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import kotlinx.coroutines.*
88
import org.abimon.eternalJukebox.EternalJukebox
99
import org.abimon.eternalJukebox.bearer
1010
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
1414
import org.slf4j.Logger
1515
import org.slf4j.LoggerFactory
16-
import java.util.*
1716
import java.util.concurrent.atomic.AtomicReference
1817

1918
@OptIn(DelicateCoroutinesApi::class)
@@ -111,164 +110,6 @@ object SpotifyAnalyser : IAnalyser {
111110
return array.toTypedArray()
112111
}
113112

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-
272113
override suspend fun getInfo(id: String, clientInfo: ClientInfo?): JukeboxInfo? {
273114
var error: SpotifyError? = null
274115
var track: JukeboxInfo? = null

src/main/kotlin/org/abimon/eternalJukebox/handlers/api/AnalysisAPI.kt

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package org.abimon.eternalJukebox.handlers.api
33
import io.vertx.core.json.JsonArray
44
import io.vertx.ext.web.Router
55
import io.vertx.ext.web.RoutingContext
6-
import kotlinx.coroutines.Dispatchers
7-
import kotlinx.coroutines.withContext
86
import org.abimon.eternalJukebox.*
97
import org.abimon.eternalJukebox.objects.EnumStorageType
108
import org.abimon.eternalJukebox.objects.JukeboxInfo
11-
import org.abimon.visi.io.ByteArrayDataSource
129
import org.slf4j.Logger
1310
import org.slf4j.LoggerFactory
1411

@@ -24,8 +21,7 @@ object AnalysisAPI : IAPI {
2421
private suspend fun analyseSpotify(context: RoutingContext) {
2522
if (EternalJukebox.storage.shouldStore(EnumStorageType.ANALYSIS)) {
2623
val id = context.pathParam("id")
27-
val update = context.request().getParam("update")?.toBoolean() ?: false
28-
if (EternalJukebox.storage.isStored("$id.json", EnumStorageType.ANALYSIS) && !update) {
24+
if (EternalJukebox.storage.isStored("$id.json", EnumStorageType.ANALYSIS)) {
2925
if (EternalJukebox.storage.provide("$id.json", EnumStorageType.ANALYSIS, context, context.clientInfo))
3026
return
3127

@@ -34,29 +30,10 @@ object AnalysisAPI : IAPI {
3430
return context.response().putHeader("X-Client-UID", context.clientInfo.userUID).end(data, "application/json")
3531
}
3632

37-
if (update)
38-
logger.info("[{}] {} is requesting an update for {}", context.clientInfo.userUID, context.clientInfo.remoteAddress, id)
39-
40-
val track = EternalJukebox.spotify.analyse(id, context.clientInfo)
41-
42-
if (track == null)
43-
context.response().putHeader("X-Client-UID", context.clientInfo.userUID).setStatusCode(400).end(jsonObjectOf(
44-
"error" to "Track object is null",
45-
"client_uid" to context.clientInfo.userUID
46-
))
47-
else {
48-
context.response().putHeader("X-Client-UID", context.clientInfo.userUID).end(track.toJsonObject())
49-
50-
withContext(Dispatchers.IO) {
51-
EternalJukebox.storage.store(
52-
"$id.json",
53-
EnumStorageType.ANALYSIS,
54-
ByteArrayDataSource(track.toJsonObject().toString().toByteArray(Charsets.UTF_8)),
55-
"application/json",
56-
context.clientInfo
57-
)
58-
}
59-
}
33+
context.response().putHeader("X-Client-UID", context.clientInfo.userUID).setStatusCode(400).end(jsonObjectOf(
34+
"error" to "It is not possible to get new analysis data from Spotify. Please check the subreddit linked under 'Social' in the navigation bar for more information.",
35+
"client_uid" to context.clientInfo.userUID
36+
))
6037
} else {
6138
context.response().putHeader("X-Client-UID", context.clientInfo.userUID).setStatusCode(501).end(jsonObjectOf(
6239
"error" to "Configured storage method does not support storing ANALYSIS",

src/main/kotlin/org/abimon/eternalJukebox/objects/EmptyDataAPI.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ object EmptyDataAPI: IAnalyser, IAudioSource, IDatabase, IStorage, IAnalyticsSto
1414
override fun shouldStore(type: EnumStorageType): Boolean = false
1515
override suspend fun search(query: String, clientInfo: ClientInfo?): Array<JukeboxInfo> = emptyArray()
1616
override suspend fun provide(info: JukeboxInfo, clientInfo: ClientInfo?): DataSource? = null
17-
override suspend fun analyse(id: String, clientInfo: ClientInfo?): JukeboxTrack? = null
1817
override suspend fun store(name: String, type: EnumStorageType, data: DataSource, mimeType: String, clientInfo: ClientInfo?): Boolean = false
1918
override suspend fun getInfo(id: String, clientInfo: ClientInfo?): JukeboxInfo? = null
2019
override suspend fun provide(name: String, type: EnumStorageType, clientInfo: ClientInfo?): DataSource? = null

0 commit comments

Comments
 (0)