Skip to content

Commit aa4074f

Browse files
committed
Added endpoint to retrieve current track (#149)
1 parent 7b6fbb7 commit aa4074f

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This module depends on `librespot-core` and provides an API to interact with the
1414
- `POST \player\set-volume` Set volume to a given `volume` value from 0 to 65536.
1515
- `POST \player\volume-up` Up the volume a little bit.
1616
- `POST \player\volume-down` Lower the volume a little bit.
17+
- `POST \player\current` Retrieve information about the current track.
1718

1819
### Metadata
1920
- `POST \metadata\{type}\{uri}` Retrieve metadata. `type` can be one of `episode`, `track`, `album`, `show`, `artist`, `uri` is the standard Spotify uri.

api/src/main/java/xyz/gianlu/librespot/api/Utils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ public static void invalidParameter(@NotNull HttpServerExchange exchange, @NotNu
6060
}
6161

6262
public static void internalError(@NotNull HttpServerExchange exchange, @NotNull Exception ex) {
63+
internalError(exchange, ex.getMessage());
64+
}
65+
66+
public static void internalError(@NotNull HttpServerExchange exchange, @NotNull String reason) {
6367
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
64-
exchange.getResponseSender().send(String.format(INTERNAL_ERROR_BODY, ex.getMessage()));
68+
exchange.getResponseSender().send(String.format(INTERNAL_ERROR_BODY, reason));
6569
}
6670
}

api/src/main/java/xyz/gianlu/librespot/api/handlers/PlayerHandler.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package xyz.gianlu.librespot.api.handlers;
22

3+
import com.google.gson.JsonObject;
4+
import com.spotify.metadata.proto.Metadata;
35
import io.undertow.server.HttpHandler;
46
import io.undertow.server.HttpServerExchange;
57
import org.jetbrains.annotations.NotNull;
68
import org.jetbrains.annotations.Nullable;
79
import xyz.gianlu.librespot.api.Utils;
10+
import xyz.gianlu.librespot.common.ProtobufToJson;
811
import xyz.gianlu.librespot.core.Session;
12+
import xyz.gianlu.librespot.mercury.model.EpisodeId;
13+
import xyz.gianlu.librespot.mercury.model.PlayableId;
14+
import xyz.gianlu.librespot.mercury.model.TrackId;
915
import xyz.gianlu.librespot.player.PlayerRunner;
1016

1117
import java.util.Deque;
@@ -50,6 +56,36 @@ private void load(HttpServerExchange exchange, @Nullable String uri, boolean pla
5056
session.player().load(uri, play);
5157
}
5258

59+
private void current(HttpServerExchange exchange) {
60+
PlayableId id = session.player().currentPlayableId();
61+
62+
JsonObject obj;
63+
if (id instanceof TrackId) {
64+
Metadata.Track track = session.player().currentTrack();
65+
if (track == null) {
66+
Utils.internalError(exchange, "Missing track metadata. Try again.");
67+
return;
68+
}
69+
70+
obj = ProtobufToJson.convert(track);
71+
obj.addProperty("uri", id.toSpotifyUri());
72+
} else if (id instanceof EpisodeId) {
73+
Metadata.Episode episode = session.player().currentEpisode();
74+
if (episode == null) {
75+
Utils.internalError(exchange, "Missing episode metadata. Try again.");
76+
return;
77+
}
78+
79+
obj = ProtobufToJson.convert(episode);
80+
obj.addProperty("uri", id.toSpotifyUri());
81+
} else {
82+
Utils.internalError(exchange, "Invalid PlayableId: " + id);
83+
return;
84+
}
85+
86+
exchange.getResponseSender().send(obj.toString());
87+
}
88+
5389
@Override
5490
public void handleRequest(HttpServerExchange exchange) throws Exception {
5591
exchange.startBlocking();
@@ -72,6 +108,9 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
72108
}
73109

74110
switch (cmd) {
111+
case CURRENT:
112+
current(exchange);
113+
return;
75114
case SET_VOLUME:
76115
setVolume(exchange, Utils.getFirstString(params, "volume"));
77116
return;
@@ -104,7 +143,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
104143
private enum Command {
105144
LOAD("load"), PAUSE("pause"), RESUME("resume"),
106145
NEXT("next"), PREV("prev"), SET_VOLUME("set-volume"),
107-
VOLUME_UP("volume-up"), VOLUME_DOWN("volume-down");
146+
VOLUME_UP("volume-up"), VOLUME_DOWN("volume-down"), CURRENT("current");
108147

109148
private String name;
110149

0 commit comments

Comments
 (0)