From bf70d32eb4ae58e417b419f57cc75381a2d6bc0a Mon Sep 17 00:00:00 2001 From: TurtleArmyMc <44322335+TurtleArmyMc@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:25:10 -0400 Subject: [PATCH 1/2] Fix SoundcloudPlaylistExtractor: tracks are in correct order --- .../SoundcloudPlaylistExtractor.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index d308eb831..d62522791 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -1,13 +1,9 @@ package org.schabi.newpipe.extractor.services.soundcloud.extractors; -import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL; -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; - import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; - import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.StreamingService; @@ -20,11 +16,15 @@ import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; +import javax.annotation.Nonnull; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Objects; -import javax.annotation.Nonnull; +import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class SoundcloudPlaylistExtractor extends PlaylistExtractor { private static final int STREAMS_PER_REQUESTED_PAGE = 15; @@ -171,9 +171,25 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { try { final JsonArray tracks = JsonParser.array().from(response); + // Response may not contain tracks in the same order as currentIds + final HashMap idToTrack = new HashMap<>(); for (final Object track : tracks) { if (track instanceof JsonObject) { - collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) track)); + final JsonObject o = (JsonObject) track; + idToTrack.put(o.getInt("id"), o); + } + } + for (final String strId : currentIds) { + final int id = Integer.parseInt(strId); + try { + collector.commit(new SoundcloudStreamInfoItemExtractor( + Objects.requireNonNull( + idToTrack.get(id), + "no track with id " + id + " in response" + ) + )); + } catch (final NullPointerException e) { + throw new ParsingException("Could not parse json response", e); } } } catch (final JsonParserException e) { From 02810a7db7f1d5cf017b72b5a8a7809624cb5663 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Mon, 10 Oct 2022 22:22:12 +0200 Subject: [PATCH 2/2] Add a comment --- .../soundcloud/extractors/SoundcloudPlaylistExtractor.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index d62522791..13f1f1400 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -171,7 +171,8 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { try { final JsonArray tracks = JsonParser.array().from(response); - // Response may not contain tracks in the same order as currentIds + // Response may not contain tracks in the same order as currentIds. + // The streams are displayed in the order which is used in currentIds on SoundCloud. final HashMap idToTrack = new HashMap<>(); for (final Object track : tracks) { if (track instanceof JsonObject) {