Skip to content

Commit

Permalink
Infer track URI from context
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 26, 2020
1 parent e70fb43 commit 26818c6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
14 changes: 11 additions & 3 deletions lib/src/main/java/xyz/gianlu/librespot/common/ProtoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.spotify.context.ContextPlayerOptionsOuterClass;
import com.spotify.metadata.Metadata;
import com.spotify.playlist4.Playlist4ApiProto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -28,6 +30,8 @@
* @author Gianlu
*/
public final class ProtoUtils {
private static final Logger LOGGER = LogManager.getLogger(ProtoUtils.class);

private ProtoUtils() {
}

Expand Down Expand Up @@ -61,11 +65,15 @@ private static JsonObject trackToJson(@NotNull Player.ProvidedTrack track) {
}

@NotNull
private static JsonObject trackToJson(@NotNull ContextTrack track) {
private static JsonObject trackToJson(@NotNull ContextTrack track, String uriPrefix) {
JsonObject obj = new JsonObject();
if (track.hasUri() && !track.getUri().isEmpty()) obj.addProperty("uri", track.getUri());
if (track.hasUid()) obj.addProperty("uid", track.getUid());
obj.add("metadata", mapToJson(track.getMetadataMap()));
if (track.hasUri() && !track.getUri().isEmpty())
obj.addProperty("uri", track.getUri());
else if (track.hasGid() && uriPrefix != null)
obj.addProperty("uri", uriPrefix + new String(PlayableId.BASE62.encode(track.getGid().toByteArray(), 22)));

return obj;
}

Expand All @@ -83,7 +91,7 @@ public static JsonObject craftContextStateCombo(@NotNull Player.PlayerStateOrBui
page.addProperty("page_url", "");
page.addProperty("next_page_url", "");
JsonArray tracksJson = new JsonArray(tracks.size());
for (ContextTrack t : tracks) tracksJson.add(trackToJson(t));
for (ContextTrack t : tracks) tracksJson.add(trackToJson(t, PlayableId.inferUriPrefix(ps.getContextUri())));
page.add("tracks", tracksJson);
page.add("metadata", mapToJson(ps.getPageMetadataMap()));
pages.add(page);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xyz.gianlu.librespot.metadata;

import org.jetbrains.annotations.NotNull;
import xyz.gianlu.librespot.common.Base62;
import xyz.gianlu.librespot.common.Utils;

import java.util.regex.Matcher;
Expand All @@ -12,7 +11,6 @@
*/
public final class EpisodeId implements SpotifyId, PlayableId {
static final Pattern PATTERN = Pattern.compile("spotify:episode:(.{22})");
private static final Base62 BASE62 = Base62.createInstanceWithInvertedCharacterSet();
private final String hexId;

private EpisodeId(@NotNull String hex) {
Expand Down
11 changes: 11 additions & 0 deletions lib/src/main/java/xyz/gianlu/librespot/metadata/PlayableId.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.spotify.metadata.Metadata;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.common.Base62;
import xyz.gianlu.librespot.common.Utils;

import java.util.Arrays;
Expand All @@ -17,6 +18,8 @@
* @author Gianlu
*/
public interface PlayableId {
Base62 BASE62 = Base62.createInstanceWithInvertedCharacterSet();

@NotNull
static PlayableId fromUri(@NotNull String uri) {
if (!isSupported(uri)) return new UnsupportedId(uri);
Expand Down Expand Up @@ -80,6 +83,14 @@ static PlayableId from(@NotNull Metadata.Episode episode) {
return EpisodeId.fromHex(Utils.bytesToHex(episode.getGid()));
}

@NotNull
static String inferUriPrefix(@NotNull String contextUri) {
if (contextUri.startsWith("spotify:episode:") || contextUri.startsWith("spotify:show:"))
return "spotify:episode:";
else
return "spotify:track:";
}

@NotNull
String toString();

Expand Down
2 changes: 0 additions & 2 deletions lib/src/main/java/xyz/gianlu/librespot/metadata/TrackId.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xyz.gianlu.librespot.metadata;

import org.jetbrains.annotations.NotNull;
import xyz.gianlu.librespot.common.Base62;
import xyz.gianlu.librespot.common.Utils;

import java.util.regex.Matcher;
Expand All @@ -12,7 +11,6 @@
*/
public final class TrackId implements SpotifyId, PlayableId {
static final Pattern PATTERN = Pattern.compile("spotify:track:(.{22})");
private static final Base62 BASE62 = Base62.createInstanceWithInvertedCharacterSet();
private final String hexId;

private TrackId(@NotNull String hex) {
Expand Down
23 changes: 19 additions & 4 deletions player/src/main/java/xyz/gianlu/librespot/player/PagesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import xyz.gianlu.librespot.mercury.MercuryClient;
import xyz.gianlu.librespot.mercury.MercuryRequests;
import xyz.gianlu.librespot.mercury.RawMercuryRequest;
import xyz.gianlu.librespot.metadata.PlayableId;

import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -46,10 +47,20 @@ public static PagesLoader from(@NotNull Session session, @NotNull Context contex
if (pages.isEmpty()) return from(session, context.getUri());

PagesLoader loader = new PagesLoader(session);
loader.pages.addAll(pages);
loader.putFirstPages(pages, PlayableId.inferUriPrefix(context.getUri()));
return loader;
}

private static void sanitizeTracks(List<ContextTrack> tracks, String uriPrefix) {
for (int i = 0; i < tracks.size(); i++) {
ContextTrack.Builder builder = tracks.get(i).toBuilder();
if (builder.hasUri() || !builder.hasGid()) continue;

builder.setUri(uriPrefix + new String(PlayableId.BASE62.encode(builder.getGid().toByteArray(), 22)));
tracks.set(i, builder.build());
}
}

@NotNull
private List<ContextTrack> fetchTracks(@NotNull String url) throws IOException {
MercuryClient.Response resp = session.mercury().sendSync(RawMercuryRequest.newBuilder()
Expand Down Expand Up @@ -123,13 +134,17 @@ boolean nextPage() throws IOException, MercuryClient.MercuryException {
}
}

void putFirstPages(@NotNull List<ContextPage> pages) {
void putFirstPages(@NotNull List<ContextPage> pages, String contextUri) {
if (currentPage != -1 || !this.pages.isEmpty()) throw new IllegalStateException();
this.pages.addAll(pages);
for (ContextPage page : pages) {
sanitizeTracks(page.getTracksList(), contextUri == null ? null : PlayableId.inferUriPrefix(contextUri));
this.pages.add(page);
}
}

void putFirstPage(@NotNull List<ContextTrack> tracks) {
void putFirstPage(@NotNull List<ContextTrack> tracks, String contextUri) {
if (currentPage != -1 || !pages.isEmpty()) throw new IllegalStateException();
sanitizeTracks(tracks, contextUri == null ? null : PlayableId.inferUriPrefix(contextUri));
pages.add(ContextPage.newBuilder().addAllTracks(tracks).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ String loadContextWithTracks(@NotNull String uri, @NotNull List<ContextTrack> tr
state.setOptions(ContextPlayerOptions.newBuilder().build());

String sessionId = setContext(uri);
pages.putFirstPage(tracks);
pages.putFirstPage(tracks, uri);
tracksKeeper.initializeStart();
setPosition(0);

Expand Down

0 comments on commit 26818c6

Please sign in to comment.