Skip to content

Commit

Permalink
Fix youtube track parsing by URL using also optional path
Browse files Browse the repository at this point in the history
  • Loading branch information
s-frei committed Mar 26, 2024
1 parent 3506cd4 commit 8c588c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ protected SoundCloudTrack extractSoundCloudTrack(final String json,
final StreamURLFunction<SoundCloudTrack> streamUrlFunction)
throws SoundCloudException {

return JsonElement.readTreeCatching(MAPPER, json)
.orElseThrow(() -> new SoundCloudException("Cannot parse SoundCloud track JSON"))
.mapCatching(MAPPER, SoundCloudTrack.SoundCloudTrackBuilder.class)
final JsonElement trackJsonElement = JsonElement.readTreeCatching(MAPPER, json)
.orElseThrow(() -> new SoundCloudException("Cannot parse SoundCloud track JSON"));

return trackJsonElement.mapCatching(MAPPER, SoundCloudTrack.SoundCloudTrackBuilder.class)
.streamUrlFunction(streamUrlFunction)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ private static String wrap(String functionContent) {
protected YouTubeTrack extractYouTubeTrack(final String json, final StreamURLFunction<YouTubeTrack> streamUrlFunction)
throws YouTubeException {

return JsonElement.readTreeCatching(MAPPER, json)
.orElseThrow(() -> new YouTubeException("Cannot parse YouTubeTrack JSON"))
.elementAtIndex(2).path("playerResponse")
final JsonElement trackJsonElement = JsonElement.readTreeCatching(MAPPER, json)
.orElseThrow(() -> new YouTubeException("Cannot parse YouTubeTrack JSON"));

return playerResponseFromTrackJSON(trackJsonElement)
.mapCatching(MAPPER, YouTubeTrack.URLYouTubeTrackBuilder.class).getBuilder()
.streamUrlFunction(streamUrlFunction)
.build();
Expand Down Expand Up @@ -199,10 +200,7 @@ protected YouTubeTrackInfo extractTrackInfo(final String json, final String trac
.path("streamingData");

} else {
final JsonElement playerResponse = jsonElement.elementAtIndex(2)
.path("playerResponse")
.orElse(jsonElement)
.path("playerResponse");
final JsonElement playerResponse = playerResponseFromTrackJSON(jsonElement);

streamingData = playerResponse.asUnresolved().path("streamingData");
}
Expand Down Expand Up @@ -236,6 +234,13 @@ protected YouTubeTrackInfo extractTrackInfo(final String json, final String trac
}
}

private static JsonElement playerResponseFromTrackJSON(JsonElement jsonElement) {
return jsonElement.elementAtIndex(2)
.path("playerResponse")
.orElse(jsonElement)
.path("playerResponse");
}

private Stream<YouTubeTrackFormat> getFormatsFromStream(final Stream<JsonElement> formats) {
return formats.map(format -> {
final String mimeType = format.asString("mimeType");
Expand Down

0 comments on commit 8c588c7

Please sign in to comment.